[C#][Tutorial] Basics of creating a script PART 2
-
Part 2 of the creating a script tutorial!
NOTE: You must've completed the first tutorial to follow this one!
In this tutorial, we will cover the following:
Native calling.
Vectors and car spawning
Ini handling with build-in classes
Vehicle handling(requested)In the last tutorial, we left off at a reset wanted level script. In this script, I will cover the different features of SHVDN and how to use them.
1) Native Calling
SHVDN is basically a wrapper for the C++ ScriptHook. It calls the natives to do things in the game. But there are a few things that are not in SHVDN and for those we go the old school manual native calling.
To call a native, we use this:
We can easily set a blackout using World.SetBlackout(bool status) but if we want to do it manually, this is the code we use:
There are a lot of natives, around 5000, and Alexander Blade has made an awesome website for researching natives.
Native DB
Some natives, however are not that simple.
For example, the native
_WORLD3D_TO_SCREEN2D
dont return a Vector2 or a position. They use something called OutputArguments. We will discuss them in a later tutorial.2) Vectors and car spawning
To determine a position, GTA uses Vectors. There are two types of Vectors. Vector 2 and Vector 3. The Vector3 is very commonly used. Vector3 consists of three floats, x, y, and z. These are positions in 3d space. It's hard to explain them, but just know that they determine a position. Vector 2 is the same thing, it only doesnt have the 3D element to it, and only has x and y. Those positions are the screen positions. But lets focus on Vector3s for now. For example, I want to spawn a car at a set location, 5 units front of the player. For spawning a car, we use the static World.CreateVehicle method and a Vector3 for the position:
Now there is a lot going on here. I'll try to explain everything.
Vector3 position - this is declaring a new Vector3 classGame.Player.Character - this may be confusing. Remember, Game.Player is the actual Player class and Game.Player.Character is a Ped class. If you want to do anything playerish like wantedlevels, it will be in Game.Player. If you want to do tasks to the player, it is in Game.Player.Character. I will explain this later on.
Game.Player.Character.Position - self explanatory, this returns a Vector3 coordinate of the player's ped.
Game.Player.Character.ForwardVector - it returns the vector you are facing.
Game.Player.Character.Position + Game.Player.Character.ForwardVector - basic rule of vectors, if you add a vector to another vector, you get a direction. In this cause we get a direction relative to our position and our heading.
Game.Player.Character.Position + Game.Player.Character.ForwardVector * 5 - this adds to the direction so it is 5 units.
Hope this makes sense
Ini handling
If you are a hardcore C# programmer, you probably wouldn't like these classes, but these are ok for simple Inis. SHVDN uses the class ScriptSettings. This is how you load an ini:
Do note this will throw an exception if the path doesnt exist. The string path should be easy to understand, since i said you require the basics of C#.
To get values from an ini:
Easy to understand, "SECTION" is the section, "NAME" is the name of the key, and "DEFAULTVALUE" is something that the class would default to, if it didnt find the key.
This is how the ini should look like:[SECTION]
NAME=VALUETo set values, we use nearly the same thing:
Vehicle Handling
As requested, I am doing this also.Opening and closing doors
The first parameter is simple, it is an enum with all the doors.
The second parameter is if you want it to be loose, again self-explanatory
The third parameter is if you want it to open instantlyYes, its as simple as that.
Again, simple.
Second parameter is if you want it to close instantly.indicators
Simple, right?Colors
Simple again.In the next tutorial, we will look into the Tick event and the OutputArguments stuff. Until then, bye!
-
@TobsiCred you can make a key definition like this:
This is my whole onKeyDown event. in this one, if you press H or K, it creates an Adder 5 units front of you. But if you pressed H, the vehicle would have its indicators on. If you pressed K, it will have the front left(driver side) door open. For the ZModeler, I have quite figured it out, but if i need help i will contact youBasically, this whole tutorial was info on how to use it. In the last tutorial, I showed you how to do key events so I just put info here on how to use SHVDN
-
im still confuse about this native function :_north_yantok
error is like they dont have any name as _north_yantok