Pages

Saturday, March 2, 2013

[TUT-IV] First script - Interacting with peds/vehicles/objects

Let's create our first script? Step by step how to create an visual studio project to be used to edit and compile an .net script for GTA IV using VB.NET language :)

You can download the sample project here.


Lets start our first script. First of all lets start an new project in Visual Basic:

Open Visual Basic, click in New Project, in the next window select Class Library and click in Ok:


Now you will see the "empty" script, lets fill it with the basic script, clear the actual script text and access this Pastebin page and copy the Raw text and paste it in the Visual Basic script:


Lets save our project, click in File > Save All, set the Location folder where the files will be saved and the name of the project and click in Save:


You will notice some blue and red lines in the script text, they indicate error in our code, they happen at this moment because we didn't have set the References for the script.
The basic reference is to the ScriptHookDotNet dll, download it here, inside the ZIP, open the folder "scripts", then open folder "for Developers", then folder "Bin", extract the dll "ScriptHookDotNet.dll" to an folder, we will use this dll as reference for each new project, so save it in an good place to avoid mistakenly deleting it.

 Now let's add the dll as reference, in the right side, right click our project name (Script_base) and click in Add Reference:


in the new window, click in Browse and select the folder where the dll is, select it and click in Ok:


We also will need references for the Imports used, actually we have:

Imports System
Imports GTA
Imports System.Drawing
Imports System.Windows.Forms

We don't need to care about System import now, GTA was already referenced by the dll, we need to add reference for System.Drawing and System.Windows.Forms,  right click the project name on the right and click in Add Reference, in the new window click in .NET and then select in the list the name of the imports, you can select more than one holding control and clicking in the item:


Ok, now we are ready to start ^^

Before all, lets test if everything is ok with our script, right click the project name and click in Properties, change Assembly name to Script_base.net and clear Root namescape:


Click in Compile and in the Build output path set the path of your GTA Scripts folder:


Click in the menu Build > Build Script_base, if everything is Ok the text Build succeeded will appear in the left bottom and the script dll will be in the GTA Scripts folder:



Lets start detecting and interacting with game objects like Pedestrians, Vehicles (cars, bikes, trains, boats, helicopters, trucks, etc) and Objects, For example, lets get all peds surrounding player and push then:

In the keyDown event lets check if the player pressed numpad9, then get some peds, do some checks and push then:

What is happening in each line:

   If e.Key = Keys.NumPad9 Then
here we are checking if the key pressed is numpad9

   Dim peds As Ped()
here we declare an object of type Ped array, will be our list of peds

   Dim tmpPed As Ped
here we declare our temporary ped, it will be an item of the peds list

   peds = World.GetPeds(Player.Character.Position, 30)
here we get all peds that are at max distance of 30 from player position and store then in the array

   For Each tmpPed In peds
here we start running the list for the first time, putting the peds in Ragdoll state, this will make the push more interesting, tmpPed will receive the actual ped in the For iteration

   If Exists(tmpPed) AndAlso (tmpPed <> Player.Character) AndAlso Not Exists(tmpPed.CurrentVehicle) Then
here we check if the ped exists, if is different from player and if it are not inside an vehicle

   tmpPed.PreventRagdoll = False
here we avoid that the ped prevent an ragdoll state

   tmpPed.ForceRagdoll(2000, True)
here we force an ragdoll state for the ped

And in the second For:

   tmpPed.ApplyForce(Vector3.Normalize(tmpPed.Position - Player.Character.Position) * 15 + Vector3.WorldUp * 2, Vector3.WorldNorth)
here we apply force to the ped pushing him away from player:

   "Vector3.Normalize(tmpPed.Position - Player.Character.Position) * 15" will return the direction of the push and multiply by 15
   " + Vector3.WorldUp * 2" will make the force move ped Up too
   ", Vector3.WorldNorth" will add an rotation to this force


We can do the same with vehicles just changing the method that will return the vehicles and the types of the objects:


Noticed how i did this different? I didn't have to declare all variables separately, i can do all in the For declaration, this is interesting to do when you don't need to run the list more than one time.

To do the same with objects we just change types and methods, like we did with vehicles and also we detach the objects because great part of them are attached somewhere, also we need to use GTA.Object as type instead of just Object because the word Object is part of the vb.net language:


The most important methods used in those examples was: World.GetPeds, World.GetVehicles and World.GetAllObjects, we also can use World.GetAllPeds and World.GetAllVehicles, but they will return all vehicles/peds and we will have more lag.

An interesting detail about World.GetPeds is the third param that will limit the number of peds found

With World.GetAllPeds we can specify as parameter the model of the pedestrian that we are searching, for example if we want to search for fat cops only:

   peds = World.GetAllPeds("m_m_fatcop_01")

the same we can do with World.GetVehicles and World.GetAllVehicles:

   For Each v As Vehicle In World.GetVehicles(Player.Character.Position, 30, "taxi")

   For Each v As Vehicle In World.GetAllVehicles("taxi")


Download the final script here, you can just put it in the GTA Scripts folder and test it, you don't need an compiled dll to run scripts :)


Tip: Run gta in window mode using commandline.txt with line -windowed or with an shortcut to the LaunchGTAIV.exe with param -windowed

To test the script, press " to see the console window and then type the command reloadscripts, if everything is ok, your script name will appear in the console like this: