Pages

Tuesday, May 14, 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.