Friday, November 29, 2013

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

Let's produce our 1st script? Step by step how to create an visual studio project to be employed to edit and compile an .net script for GTA IV employing VB.NET language :)

You can download the sample project here.

Lets commence our 1st script. First of all lets begin an new project in Visual Fundamental:

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 standard script, clear the actual script text and access this Pastebin page and copy the Raw text and paste it in the Visual Fundamental 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 take place at this moment since we did not have set the References for the script.
The simple reference is to the ScriptHookDotNet dll, download it right 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 every new project, so save it in an good spot to keep away from mistakenly deleting it.

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

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

We also will need references for the Imports utilized, truly we have:

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

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

Ok, now we are prepared to begin ^^

Just before all, lets test if every thing is ok with our script, appropriate click the project name and click in Properties, change Assembly name to and clear Root namescape:

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

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

Lets begin detecting and interacting with game objects like Pedestrians, Autos (vehicles, bikes, trains, boats, helicopters, trucks, and so on) 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 taking place in each and every line:

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

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

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

   peds = Globe.GetPeds(Player.Character.Position, 30)
right 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, placing the peds in Ragdoll state, this will make the push more intriguing, tmpPed will get the actual ped in the For iteration

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

   tmpPed.PreventRagdoll = False
right here we keep away from that the ped stop an ragdoll state

   tmpPed.ForceRagdoll(2000, True)
right 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 * two" will make the force move ped Up too
   ", Vector3.WorldNorth" will add an rotation to this force

We can do the exact same with automobiles just altering the strategy that will return the automobiles and the types of the objects:

Noticed how i did this distinct? I didn't have to declare all variables separately, i can do all in the For declaration, this is exciting to do when you don't require to run the list far more than a single time.

To do the identical with objects we just adjust kinds and methods, like we did with automobiles and also we detach the objects since fantastic component of them are attached somewhere, also we need to have to use GTA.Object as type alternatively of just Object because the word Object is part of the language:

The most important strategies employed in those examples was: World.GetPeds, World.GetVehicles and World.GetAllObjects, we also can use World.GetAllPeds and Globe.GetAllVehicles, but they will return all automobiles/peds and we will have much more lag.

An intriguing detail about Globe.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 looking, for example if we want to search for fat cops only:

   peds = Planet.GetAllPeds("m_m_fatcop_01")

the identical we can do with World.GetVehicles and Planet.GetAllVehicles:

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

   For Every v As Car In Globe.GetAllVehicles("taxi")

Download the final script here, you can just put it in the GTA Scripts folder and test it, you do not want 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 sort the command reloadscripts, if every thing is ok, your script name will appear in the console like this:

[TUT-IV] First script - Interacting with peds/vehicles/objects
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.