Tuesday, 26 February 2013

[TIP-IV] Basic scripting - Script structure and main events [GTA IV vb.net]

Considering that you know the basic of programming (what is variable, object, methods, loops, conditions, etc.) i will start showing the basic script structure and the main events that are very usefull when creating an script.


  • Imports area: here we will declare the additional libraries/classes/whatever its called things that we will need in the script, the basic is:

Imports System
Imports GTA

another interesting imports are:

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections.Generic
Imports System.IO


  • Class header: here the main class starts, here we can set the name of the script that will appear in the console window when the script is loaded

Public Class myBasicScript
    Inherits Script


  • Variables/Constants/Classes/Enumerations area: here we can declare the variables that we will need along the script, also declare classes, constants:


Private bScriptOn As Boolean = False
Private sMessage As string


Private Class TDarts
        Public dart As GTA.Object = Nothing
        Public launched As Boolean = False
        Public target As Ped = Nothing
end class


  • Then comes the "event" that will happen when the script is loaded by the scripthook:

Sub New()
       me.Interval = 10

       sMessage = "Script loaded :)"
end sub

Here we can initiate variables/objects, load textures, sounds, whatever you need to do only one time in the script execution.


  • The keydown event: this event will happen each time that the player press an key or click with mouse

Private Sub keyDown(ByVal sender As Object, ByVal e As GTA.KeyEventArgs) Handles MyBase.KeyDown

the object "e" will receive the info about the keypress, it can have the name that you want, for example you can use "k As GTA.KeyEventArgs" instead of "e As GTA.KeyEventArgs",  through this we can identify what key was pressed through the element e.Key, for example, if we wish to identify when user press key "A":


Private Sub keyDown(ByVal sender As Object, ByVal e As GTA.KeyEventArgs) Handles MyBase.KeyDown
        if e.key = keys.A then 
        end if
end sub


the keyUp event has the same parameters and will happen when the key is released:


Private Sub keyUp(ByVal sender As Object, ByVal e As GTA.KeyEventArgs) Handles MyBase.KeyUp


  • The Console event: this will happen when user input some text in the console window and press enter:

Private Sub console_CMD(ByVal sender As Object, ByVal cmd As ConsoleEventArgs) Handles MyBase.ConsoleCommand

through element Command we can identify the command typed by the user, and through the element ParameterCount we can see if there is parameters in the command and through element Parameter we can read the parameters, they are inside the object cmd:

Private Sub console_CMD(ByVal sender As Object, ByVal cmd  s ConsoleEventArgs) Handles MyBase.ConsoleCommand
        If cmd.Command = "die" Then
            If cmd.ParameterCount > 0 Then
                If cmd.Parameter(0).ToLower = "now" Then
                    Player.Character.Die()
                ElseIf Int16.Parse(cmd.Parameter(0)) > 0 Then
                    Wait(Int16.Parse(cmd.Parameter(0)))
                    Player.Character.Die()
                End If
            End If
        End If
 End Sub


through method Game.Console.Print we can display an message on the console:

Private Sub console_CMD(ByVal sender As Object, ByVal cmd As ConsoleEventArgs) Handles MyBase.ConsoleCommand
        Game.Console.Print("You typed an console command..." & cmd.Command)
 End Sub



  • Tick event: this happen each X miliseconds, based on variable me.Interval:

Private Sub general_tick(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Tick

for example, if we want to heal player life every 10 miliseconds (me.Interval):

Private Sub general_tick(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Tick
        player.character.health += 1
end sub


  • PerFrameDrawing event: this will happen at each frame of the game, you must be carefull when using this event

Private Sub GraphicsEventHandler(ByVal sender As Object, ByVal g As GTA.GraphicsEventArgs) Handles MyBase.PerFrameDrawing

through element g.Graphics we can draw texts, rectangles, lines and textues:

Private Sub GraphicsEventHandler(ByVal sender As Object, ByVal e As GTA.GraphicsEventArgs) Handles MyBase.PerFrameDrawing
        e.Graphics.DrawText("my text inside the PerFrameDrawing event", 10, 10)
' draw the text starting at 10, 10 (X, Y screen position)

        e.Graphics.DrawLine(10, 20, 100, 20, 1, Color.Red)
' draw an line starting at 10, 20, ending at 100, 20, with 1of width and red color

        e.Graphics.DrawRectangle(100, 100, 200, 200, color.FromArgb(100, 255, 255, 255))
' draw an rectangle with center at 100, 100, with 200 of width, 200 of height and with semi transparent white color

        e.Graphics.DrawSprite(textureTest, 300, 300, 100, 100, 0)
' draw an texture with center at 300, 300, with 100 of widht, 100 of height and 0 of rotation
end sub


an additional method that i like to use when developing is:

Private Sub msg(ByVal sMsg As String, ByVal time As Int32)
        Native.Function.Call("PRINT_STRING_WITH_LITERAL_STRING_NOW", "STRING", sMsg, time, 1)
End Sub

this just show an message in the botton center of the screen, very usefull to see variable values

  • the main Class ending: here the main class of the script ends

End Class



an important thing about the events is the text "Handles", this part will determine what the event will handle, for example:

Handles MyBase.KeyDown
Handles MyBase.KeyUp
Handles MyBase.ConsoleCommand
Handles MyBase.PerFrameDrawing

another important part is the element that will receive the events methods/properties, for example, here we used:

e As GTA.KeyEventArgs in the keydown event
cmd  As ConsoleEventArgs in the console event
e As GTA.GraphicsEventArgs in the PerFrameDrawing


you can download an sample script here.




0 comments:

Post a Comment