Pages

Monday, December 2, 2013

[TUT] Using custom Classes

I use classes essentially when i need to add properties or approaches to an game object, for instance in Iron Man IV i use classes to manage all the enemies or allies behavior (flight, shoot, target locking, and so on.), in Heli Combat i use classes to make the Nightvision impact, it's very beneficial.

For instance, let's say that we want get peds damaged by player and make them float for a even though, each and every shoot will make them float for 1 second. We can generate an easy class that holds the Ped, the time of float state, an trigger to begin/boost the float state and the technique that will make them float:


Here we have:
Public p as Ped = Nothing - this is the ped, i set = absolutely nothing to make certain that begins with absolutely nothing for the ped object, this is a Public property, it indicates that we can access it outdoors the class
Private float_time As Double = - this is how much time the ped will float, an counter, its Private due to the fact i never want to access it outside the class
Public Sub New(tPed As Ped)
    p = tPed
Finish Sub 
Right here i set the method that will be referred to as when a new instance of this class is created, i obtain as param the Ped that will float
Public Sub startFloat()
    float_time += 1000
End Sub
 
Here i set the method that will enhance the float time when the ped was broken by player
Public Sub Tick()
    If float_time > Then
        float_time -= myInterval
        p.Velocity = Vector3.WorldUp
    End If
Finish Sub
 
This is the Tick of the class, will be known as at each tick of the script to make the float takes place

One crucial 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 in a position to use it inside the class:


So to use an technique or variable that is outdoors the custom Class we require to declare this technique or variable as Shared alternatively of Public or Private.

Now to make it function i need to verify for peds around player, verify if they was broken by player then add they to the List of objects of the custom class TFloatingPed, this list i known as floatingPeds:


So in the tick of the script i will use an time counter to refresh the peds list at every single 500 ms, then check if one of them was broken 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 produce, change colour, scale and destroy when we want just accessing the class instance, every class instance will have an blip object.

We can use this thought of classes in numerous situations, for example we can generate an Rocket class, that will have an object that is the rocket, an Fly strategy that will make it fly and verify for collisions, and we can generate a lot of rockets and shoot them all and the tick of each and every class object will do the job (fly and explode on collision), i do this in Iron Man IV and Heli Combat, it is really useful.

I utilised this class idea in chainsaw script as well, to create the blood drips on screen. Download the source code of chainsaw script right here.

Download the project of this tutorial right here.
[TUT] Using custom Classes
9out of 10 based on 10 ratings. 9 user reviews.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.