Tuesday, 14 May 2013

[TUT] Using custom Classes

I use classes basically when i need to add properties or methods to an game object, for example in Iron Man IV i use classes to control all the enemies or allies behavior (flight, shoot, target locking, etc.), in Heli Combat i use classes to make the Nightvision effect, it's very useful.

For example, let's say that we want get peds damaged by player and make them float for a while, each shoot will make them float for 1 second. We can create an simple class that holds the Ped, the time of float state, an trigger to start/increase the float state and the method that will make them float:


Here we have:
Public p as Ped = Nothing - this is the ped, i set = nothing to make sure that starts with nothing for the ped object, this is a Public property, it means that we can access it outside the class
Private float_time As Double = 0 - this is how much time the ped will float, an counter, its Private because i don't need to access it outside the class
Public Sub New(tPed As Ped)
    p = tPed
End Sub 
Here i set the method that will be called when a new instance of this class is created, i receive as param the Ped that will float
Public Sub startFloat()
    float_time += 1000
End Sub
 
Here i set the method that will increase the float time when the ped was damaged by player
Public Sub Tick()
    If float_time > 0 Then
        float_time -= myInterval
        p.Velocity = Vector3.WorldUp
    End If
End Sub
 
This is the Tick of the class, will be called at each tick of the script to make the float happens

One important detail, inside the Tick method i use the variable  myInterval, i had to declare this variable as an Shared variable in the script to be able to use it inside the class:


So to use an method or variable that is outside the custom Class we need to declare this method or variable as Shared instead of Public or Private.

Now to make it work i need to check for peds around player, check if they was damaged by player then add they to the List of objects of the custom class TFloatingPed, this list i called floatingPeds:


So in the tick of the script i will use an time counter to refresh the peds list at each 500 ms, then check if one of them was damaged by player and add to the list of floating peds, run the floating peds list and make them float:


With this class we can add an Blip object to it so we can create, change color, scale and destroy when we want just accessing the class instance, each class instance will have an blip object.

We can use this idea of classes in many cases, for example we can create an Rocket class, that will have an object that is the rocket, an Fly method that will make it fly and check for collisions, and we can create a lot of rockets and shoot them all and the tick of each class object will do the job (fly and explode on collision), i do this in Iron Man IV and Heli Combat, it's very useful.

I used this class idea in chainsaw script too, to create the blood drips on screen. Download the source code of chainsaw script here.

Download the project of this tutorial here.

Related Posts:

  • [Update] Heli Combat v1.2Download: GTA 4 Mods (GTA Garage, MOD Db)News for Heli Combat script:Added customizable hotkeys, now you can set the keys for:Shoot - hkShoot=LButtonSwitch actual gun - hkSwitchGun=QSwitch camera - hkSwitchCam=XShoo… Read More
  • [Update] AC-130 v1.1This is the new version of my AC-130 script, not big changes, i don't know what more i can do for this scriptWhat i did:Performance improvement using new methods to find targets and create the night vision effectAdded XBox 36… Read More
  • [Script] Heli CombatWhat about heavy armored choppers on Liberty City? This is what this mod brings to you, Heli Combat (Beta)Download GTA 4 Mods (GTA Garage, MOD Db)V1.1 - Train Derail + flares + fixesFeatures Spawn menu: Se… Read More
  • [TUT] Adding vehicles(without replace game vehicles)Today i wanna show how you can add vehicles to GTA IV without replace any game vehicle.This tutorial is based on this video.Easy wayDownload this file (press ctrl + s to save), extract the files to this folder:Open the&n… Read More
  • [TUT] Heli Combat - Creating custom helicopter configIn this video i try to explain with my bad english :( how to create an custom config for an helicopter in Heli Combat script.Resuming:Download the helicopter, install it, you can try to follow this tutorial to add the he… Read More

0 comments:

Post a Comment