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:

  • [TUT] Playing native sounds and external soundsOne interesting feature in scripts its the sounds, some effects become incomplete without the proper sound effect.*** Check new method to play external sounds simultaneously here :) ***Playing game soundsBasic requirements:-O… Read More
  • [Script] Air Combat IV v1.5 - XBox 360 Controller supportAir combat IV v1.5Added XBox control supportAdded skin (livery) change, you can change ingame using Subtract key (numpad) or in the .ini of the jet or acquirement pointSome small fixesXBox controls:Hold left and right Shoulde… Read More
  • Iron Man IV - Intense air combat [W.I.P.]This will be very interesting, flying enemies with minigun and rocket launchers to hunt down Iron Man, also now we have flying ally to help Iron Man fight those enemies :)*** The script was released, take a look here&nbs… Read More
  • [TUT-IV] Detecting Targets (peds, vehicles and objects)Today i will show the idea that i use to detect targeted things in the game, i basically calc the distance between each possible target and the center of aim position.Download the sample project here, let's use this proj… Read More
  • [TUT] Script + XBox 360 controllerOne of the frequently asked feature is the possibility of use xbox 360 controller with the script, i will show two ways of obtaining xbox control key presses.First of all i wanna say thanks to pedro2555 for providing inf… Read More

0 comments:

Post a Comment