Nov 7 2014, 06:05 PM Post #1 | |||||||||||||||||||||||||||||||||||||
Ameteur Member Posts: 42 Joined: 3-November 14 | Introduction Everybody wanted to learn scripting gta iv but they give it up as there are very few tutorials for that, maybe only 2 and they are from a long time ago and not in full detail as many things have changed since. If you really want then you should listen to each tip here. Firstly you should have some knowledge about the basics of programming and c# you can buy a c# book or read more at sites, a good site i know for begginers is dotnetperls.com. Secondly I recommend you to decompile and look at other people's code and see how they work, this is the way i have learn scripting gta iv .net. Thirdly you should Practice more and more and began with easy scripts like creating a car and etc not like you have read this tutorial and you go directly and create Super Man mod. If you are an SCM scripter then you would understand more, it is still easy if you don't. I am just saying this because i was an SCM scripter before i go to GTA IV scripting. NOTE: all functions in c# end with semicolon ";" Things You Would need Well you ofcourse would need GTA IV. You have to choices. use Notepad++ as a text editor and cmd.exe(usin csc) as a compiler or simply use Visual Studio. I highly recommend Visual Studio. You can download the Express version because it is free as we all have express. You need to know the basics of Programming and c# as well as we are going to script. .NetScripthook Simple Native Trainer(not required) You can use it just for getting proper offset and coords. However you can get the coords using .NetScripthook. Go to the coords you want to get and press "~" and then type "ShowPosition" Without the quotes. then Alt-Tab and open Scripthookdotnet.log and at the last line you would see the X,Y,Z positions and Heading(angle). For more commands type "Help" without the quotes. any gxt editor , preferably X GXT Editor V2.2 Note: It does'nt matter it is uppercase or lowercase. it can be sHoWpOsItIoN or HeLp , etc. Some Effort. Basics {},(),; each function in c# ends with semicolon ";" if it involves {} then we don't use semicolon the {} are like then and end. if i translate this CODE if(nohealth == true) { Player.Character.Die(); } then you would understand it better CODE if nohealth is true then Kill Player end some functions can't be directly end with ; but it needs () before the semicolon. inside those two brackets there are parameters. some codes does'nt have parameters but the brackets are required. Those are called voids. Types CODE _________________________________________ |Valuetype | Category | |bool | Boolean | |byte | Unsigned, numeric, integral| |char | Unsigned, numeric, integral| |decimal | Numeric, decimal | |double | Numeric, floating-point | |enum | Enumeration | |float | Numeric, floating-point | |int | Signed, numeric, integral | |long | Signed, numeric, integral | |sbyte | Signed, numeric, integral | |short | Signed, numeric, integral | |struct | User-defined structure | |uint | Unsigned, numeric, integral| |ulong | Unsigned, numeric, integral| |ushort | Unsigned, numeric, integral| |__________|____________________________| it is'nt so detailed here. If you don't understand then you should read about c# more. uint and ulong and ushort are Positive Intergers. It means it can be 10,20,50 or 100 but not -1,-10 or -999999999999 1.int Int is Integer. it is whole number only it means. it can be 10,20,50,100 but not 1.5,3.3,5.7 if it is divided and the output is float it will find the nearest whole number or leave the remainder. example of int CODE int x = 13 + 7; the x is stored as a variable in the scope and we can use it after it is declared. CODE x + 8; int y = 17; int z = 38; int w = 46; int u = 69; int m = x + z - y / w * u; Console.Writeline(m) //Output: 170 2.float float is floating numbers like positions , prices , etc. it can be also defined as interger but must end with "f" example: CODE float radius = 20f; floating decimals are like 1.5 , 2.7 , 3.333333333 , 0.5 , etc. example CODE float X = 120.28 float Y = 379.68; 3.string it is a list of unicode characters it is written between quotes like " " or ' '. example CODE string M = "Mora"; string N = "Niko"; string D = "Deji"; Console.Writeline(("ID 1 =") + (M) + ("ID 2 =") + (N) + ("ID 3 =") + (D); //Output : ID 1 = Mora ID 2 = Niko ID 3 = Deji 4.bool it declares true or false conditions. by default it is false. example: CODE bool YouReadThisTutorial; if(User.ReadThisTutorial) { YouReadThisTutorial = true; } if(YouReadThisTutorial == true) { YouRock(); } else if(YouReadThisTutorial == false) { YouSuck(); } Operators Every programming language uses operators, through which we can perform different actions on the data. Let’s take a look at the operators in C# and see what they are for and how they are used.
As now we had some info about c# lets now move to scripting GTA IV GUI Using Visual Studio coding will be more easy. When accessing a namespace or etc it will show you the following contents you add example: Also When Opening a Bracket it shows you the parameters of a void also at each comma it shows you in which parameter you are also the compiler shows you all errors and colors codes. There are many other advantages when scripting in visual studio. Scripting GTA IV First now we will have to run visual studio its directory is 32 bit C:\Program Files\Visual Studio XX.X(version)\Common7\IDE 64 bit C:\Program Files(x86)\Visual Studio XX.X(version)\Common7\IDE All you had to do is run the damn software i am using Visual studio 2013. You may be using 2010 or 2005 it does'nt matter. When you start visual studio you will find something like this then click on " New Project " then select Visual C# not Visual Basic! and Then Select Class Library and Click "OK" then you would see something like this Download this File and put it anywhere in your PC. then you would right click the project name and then select "Add" and then Reference like this or Right Click "References" and then Click "Add Reference" like this and Then Click The "Browse" Tab and then the "Browse" Button like this And then go to the directory where you have put the file and select it. now you have gta iv snippets. now add this below "using System.Threading.Tasks;" CODE Using GTA; Using GTA.Native; now find this line CODE public class Class1 and make it like this CODE public class Class1 : Script note: "Class1" is the Class Name you can change it to anything. Now you are ready Event Handlers They handle the Events and code execution in a code. Tick Eventhandler it Executes at the interval of seconds example: CODE This.Interval = 5000 the time form is in millseconds so the tick executes every 5 seconds. now we've added the interval now there is the tick itself. CODE This.Tick += new EventHandler(anything) now let me explain. CODE This.Tick it is the type of the handler and we chose the tick. it can also be CODE Base.Tick CODE += It is very hard to explain but you should use them in all eventhandlers CODE new Eventhandler before "Eventhandler" there must be "new" because it must be declared after new. it is like the tick but in tick we use "EventHandler" in Console Events we use "ConsoleEventHandler" and many bla bla bla. CODE (anything) it is the void where the tick takes place. the void name should be the same. These is just a random name CODE private void anything(object sender, EventArgs e) { //bla bla some code } now let me explain CODE private void now we've declared the void. the name "private" is the void constructor. It can be "private" or "public" or "static" or "any son of a bitch you want" QUOTE A private constructor cannot be externally called. It is used to ensure higher-quality code bases on complex projects. A private constructor forces the class to provide a controlled and unified access pattern. Private Example New keyword, constructor invocation Inside the private constructor, a field is initialized to a constant number. The private constructor ensures that it is impossible for the class to be instantiated directly from external classes. It provides more compile-time checking for the validity of your program. This improves the long-term validity of the code base—without impairing performance or clarity. This program uses a singleton design pattern. Program that uses private constructor: C# CODE using System; public sealed class Test { public static readonly Test Instance = new Test(); // Singleton pattern public int A; // Instance field private Test() // This is the private constructor { this.A = 5; } } class Program { static void Main() { // We can access an instance of this object that was created. // ... The private constructor was used. Test test = Test.Instance; // These statements show that the class is usable. Console.WriteLine(test.A); test.A++; Console.WriteLine(test.A); } } Output 5 6 Squares The Test class has a private constructor and two fields. And the Program class introduces the Main entry point. The private constructor initializes the public int field A to be equal to 5 at the start of the program's execution. Class Modifiers, Syntax and Concepts Restricting access to object creation. A private constructor is similar to a public constructor in that it uses the type name followed by a formal parameter list. The formal parameter list in this private constructor shown is empty. Note:The private constructor simply makes it impossible for external code to instantiate the class freely without using reflection. Reflection and System.Reflection This can enforce the singleton design concept. The private constructor is a way of writing code that enforces your intent for the class. This essentially allows the compiler to police the project. Compile-Time Error Singleton One: 1 Let's consider the singleton design pattern. This is an easily-understood, widely-adopted design pattern. In the C# language, you can use a private constructor to ensure that external code never incorrectly creates a singleton instance. Singleton ClassProgramming tip Note:A private constructor restricts access to the constructor. It ensures the object can only be created by a member in the type. Therefore:The private constructor is a desirable part of the singleton pattern implementation in the C# language. Summary C# programming language We looked at a private constructor in a singleton design pattern. The private accessibility modifier enables greater control over the project-wide usage of your class, without impairing any other aspects of the class. And:When using a private constructor, patterns such as the factory pattern or the singleton pattern can return instance ________________________________________________________________________________ ________________________________________________________________________________ _ ________ C# Public Constructor Public constructors are most useful and common. They allow a class to be instantiated from an external location in your program. Most constructors will be in the public accessibility domain. We use private constructors for singletons. Private Constructor Note:Public constructors are used to create class instances from outside the class. Example This program introduces two classes: the Test class, and also the Program class. The Program class is used to contain the Main method, which is where the program begins execution. New keyword, constructor invocation The Test class shows how to use a custom constructor that receives a parameter. Also, the Test class constructor requires that it receives a non-zero int, demonstrating parameter validation. ArgumentException Program that uses public constructor: C# CODE using System; class Test { public Test(int a) { if (a == 0) { throw new ArgumentException("Error", "a"); } } } class Program { static void Main() { Test test = new Test(5); } } Result (The class is instantiated.) Summary The C# programming language Public constructors are the most commonly used ones. Classes without a specified public constructor will have an implicit public constructor. Private constructors, meanwhile, are most useful for protecting a class from improper use. Tip:This has been referred to as information hiding or binary barricading. These fancy terms refer to similar concepts. C# Static Constructor A static constructor initializes static fields. It runs at an indeterminate time before those fields are used. Static constructors on a type impose substantial overhead. I investigate. Info:A static constructor is sometimes called a type initializer. It initializes fields before accesses. Note:Instances are not the same as types. An instance is a specific object of the type. Type vs. Instance Example Let us consider two classes. The first one has a static constructor: it initializes its field to 1. The second class has no static constructor. In testing, the one with a static constructor has slower field accesses. However:Both classes will result in the field being set to 1 before it is ever accessed by other code. Program that demonstrates static constructor: C# CODE /// <summary> /// This type has a static constructor. /// </summary> static class HasStaticConstructor { /// <summary> /// Public field. /// </summary> public static int _test; /// <summary> /// Static constructor initializes public field. /// </summary> static HasStaticConstructor() { _test = 1; } } /// <summary> /// This type has no static constructor. /// </summary> static class NoStaticConstructor { /// <summary> /// Public field initialized inline. /// </summary> public static int _test = 1; } class Program { static void Main() { System.Console.WriteLine(HasStaticConstructor._test); System.Console.WriteLine(NoStaticConstructor._test); } } Output 1 1 Int In this example, we compare a class that has a static (type) constructor against one that does not. In the first class, which has the static constructor, the static int field _test is initialized in the constructor. Int Next:In the second class, which has no static constructor, the _test field is initialized inline. And:This means the assignment is placed directly in the declaration. This has the same effect on behavior. Performance Two We consider next the performance of the two classes. I wrote a test program (not shown) that uses each class. In the program, the value 2 is stored in the _test field in each iteration. And:The results were that static constructors do cause a slowdown of all accesses to the type. Benchmark results HasStaticConstructor: 2.89 ns NoStaticConstructor: 0.32 ns Exclamation mark Performance analysis is not simple. And with static constructors, it is complicated. It is possible to have code that accesses a field in a class with a static constructor, with no performance penalty. And:This seems to depend on how early in the program the field is first accessed. Thus:It is best to avoid static constructors for maximum performance. Bogdan Potor wrote in with a benchmark. Benchmark Summary Framework: NET Static constructors might be convenient, but they are slow. The runtime is not smart enough to optimize them in the same way it can optimize inline assignments. They should be used with caution. Tip:It is sometimes appropriate to use a lazy instantiation pattern. Check a field for null when needed, and initialize it lazily. Lazy from dotnetperls.com this page also explains what is a constructor now lets continue CODE anything Clearly, it is the name of the method. CODE (object sender, EventArgs e) every void must contain a bracket even empty "object sender" just shutup and don't mess with it "EventArgs" it is from GTA namespace. each handler uses different args. Key Events use KeyEventArgs and many bla bla bla "e" we use it as a namespace for the event. it does'nt have to be e. it can also be a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y or z. But we won't use anything from it in Tick. But we do in Key and Console Events. KEY DOWN EVENT HANDLER It is the Event that occurs when we press a Key. CODE this.KeyDown += new KeyEventHandler(anykeydown); i am gonna explain a little because most of the things was explained above CODE this.KeyDown This is the Eventhandler Name. Here it is the Keydown. CODE new KeyEventHandler Like Explained above Each "Eventhandler" uses different Parameters. Here it is a Key Event that is why it is "KeyEventHandler" CODE (anykeydown) As explained above it is the method name! it is the method we will bind to! ___________ CODE private void anykeydown(object sender, KeyEventArgs e) { } as if you notice above. It is "KeyEventArgs" not "EventArgs" now that e is optinal as said above. it can be any letter. i am choosing e . CODE e.Key if you choose for example g. then it will be CODE g.Key now i want to create an event for a key down. i want if i pressed E the player dies so anywhere above under in the class add a bool CODE Public bool anyname; anyname can be changed to anything you want. If you are using the Bool in other classes make sure it is public i choose the name key1 CODE Public bool key1 and now choose the key you want pressed in your KeyDown method and then make it true. We said that we will make if the key is pressed the player will die. i already chose E CODE if(Keys.E == e.Key) { key1 = true; { and in your Tick check if the key1 is true CODE if(key1 == true) { Player.Character.Die; } There are a bunch of Event Handlers. You Can use see them by accessing this. in the void under the class. If you don't know how to use them , then post in the topic and i will explain it. PEDS In .NetScripthook we don't have to load models like in SCM and SCO. But we have to load animations and Particle effects. First we have to define the Ped CODE Ped anyname anyname is the ped handle, you can change it to anything. i will name the handle mora CODE Ped Mora now i have to create the ped CODE Mora = World.CreatePed(//Model Name,//Vector3 Position) You can use Simple Native Trainer to see how models look like or u can use open IV. You can find all models here i am choosing "M_M_TENNIS" so first define the model CODE Model PedModel = "M_M_TENNIS"; PedModel is the variable name. now the vector3 postion i want the ped to spawn 10 feets away from the player so we use this NOTE: the letter "f" means float not feet!!! CODE Player.Character.Position.Around(10f) now the code would look like CODE Mora = World.CreatePed(PedModel, Player.Character.Position.Around(10f)); Editing The Ped first write the Ped Variable then add period(.) and a dropbox would popup and choose what you want to interact with the ped. i want to put the ped somewhere. So in Game go to the place you want to put the ped at and then hit the "~" key and write "ShowPosition" and then Alt-Tab and open Scripthookdotnet.log and scroll down until you find where you did the input. You would find the X position and Y position and Z position and Heading. my ped variable is Mora Now lets change his positon CODE Mora.Position.X = //The X Position in the log Mora.Position.Y = //The Y Position in the log Mora.Position.Z = //The Z position in the log Mora.Heading = //The Heading in the log Weapons CODE Mora.Weapons.Select(Weapon.Handgun_DesertEagle); Mora is the ped variable as mentioned above You can change "Handgun_DesertEagle" to any weapon you want write Weapon and add period(.) and a dropbox will popup with a list of weapon, lets change the ammo CODE Mora.Weapons.AnyHandgun.Ammo = 100; AnyHandgun is the weapon class if you are using Assault Rifle then it should be CODE Mora.Weapons.AnyAssaultRifle.Ammo = 100; and like this for every weapon. PUT IN VEHICLE CODE Mora.WarpIntoVehicle(//vehicle variable, //vehicle seat); it will warp the ped into the car instantly. for seats type VehicleSeat and add period(.) and a dropbox will pop up with a list of Seats. we still did'nt learn how to create vehicles. scroll down to see how. Vehicles First you have to define it CODE Vehicle myvehicle; myvehicle is the variable^^ and now spawn it CODE myvehicle = World.CreateVehicle(//MODEL,//Vector 3 Position); to apply attributes to it when using Visual Studio type the vehicle variable and then add period(.) and a dropbox will come showing what you can assign to the vehicle simple huh? to create a train CODE World.CreateMissionTrain(//Position); change //position to the Vector3 Position you want for the train to spawn PickUps Money Pickup CODE Pickup.CreateMoneyPickup(//Position,//Money Amount); change //Position with the Vector3 Position you want for the money to take place at. Change //Money Amount to the amount of money in the bundle or that will come to you when picked up. //Money Amount should be integer Pickup CODE Pickup.CreatePickup(//Position,//Model,//PickupType,//Rotation); Change //Position to the Vector3 Position you want the pickup to spawn. Change //Model to the Model you want to apply to the pickup. Type "Model" then add Period (.) and a drop-box would pop up showing available models(not added ones). //Pickup Type, there are two Pickup Types. They are CODE PickupType.None CODE PickupType.Weapon Weapon Pickup CODE Pickup.CreateWeaponPickup(//Position,//Weapon,//Ammo); Change //Position with the Vector3 Position you want. Change //Weapon to the weapon you want, type "Weapon" then add Period (.) and a dropbox would pop up showing a list of weapons(not added ones). Change //Ammo to the amount ammo you want in the weapon and when picked up. it is an integer. Explosions and Fires Explosions CODE World.AddExplosion(//Position,//ExplosionType,//Power,//PlaySoundW,//NoVisuals,//Camerashake); change //Position to the Vector 3 Position you want the Explosion to take place at. for //ExplosionType there are 3 explosion types they are CODE ExplosionType.Default ExplosionType.Molotov ExplosionType.Rocket Change //Power to the Power of the Explosion you want. It is a float. Change //PlaySoundW to "true" if you want the explosion to have sound or Change it to "false" if you want it to have sound. Change //NoVisuals to "true" if you want the explosion to have visuals and change it to "false" if you want it to have no visuals. Change //CameraShake to "true" if you want the game camera to shake when the explosion happens or change it to "false" if you don't want it to shake. Fire CODE World.StartFire(//Position); Change //Position to the Vector3 Position you want the fire to take place at. there are two extra parameters they are unknown Text and Drawings Read This Tutorial it is the best tutorial about drawing in game by JulioNIB and also see this Topic if you want to know how to make 3D Drawings inside the GTA World. Text CODE Function.Call("PRINT_STRING_WITH_LITERAL_STRING_NOW","STRING",//Message to Display,//Time In Milliseconds,//Wait) Explanation: CODE "STRING" It should always be string - Change //Message to display to the message you want to be printed in game. It is a string - eg: "Kill Dimitri". Change //Time in Milliseconds to the amount of time you want the text to stay in the screen. it is an integer and it should be in milliseconds. 1 sec = 1000. Change //Wait to true if you want to Parameter 3 "//Time in Milliseconds" to be executed or change it to false if you want it to not get executed. Coloring Your Text A List of entries
to color text add any of the entries before your text , For example coloring an enemy name. CODE "Kill ~r~Dimitri~s~ before he escapes ~r~ makes the text red. and ~s~ resets it back to the default color. Text Box First Download and install any GXT editor not FXT editor preferably X GXT Editor because i am using it in this tutorial from here Open X GXT Editor. Then File>Open and then go to the folder (Your GTA IV Folder)\common\text and choose American.gxt. Choose the "MAIN" table (first coloumn), and right click any entry of the "MAIN" Table then Right Click>Add New Entry and a box will pop up. Name the title as you wish but make it upper case because the title will be used in our c# code. before you type the text that you want it to appear in the textbox add ~s~before it. now in your c# code CODE Native.Function.Call("PRINT_HELP", "MYOWNHELP"); If you want it to keep displaying forever until using a code for removing use CODE Native.Function.Call("PRINT_HELP_FOREVER", "MYOWNHELP"); if you want to remove it use CODE Native.Function.Call("CLEAR_HELP"); "MYOWNHELP" is the title of the gxt i want to display, Replace it with the GXT title you applied for your entry Bonus Source Codes This is some source codes of gta iv scripts as a bonus for reading this tutorial. If you have a source code and you want to post it here PM me or Make a post here. Call For Rick Roll - By Mora Hannover http://pastebin.com/rpf6DuhQ Sad Violin Script - By Mora Hannover http://pastebin.com/Eiuw5P1z Choir Music Script - By Mora Hannover http://pastebin.com/7t447Fyt VStyle Air Music Script - Buggy and Unstable - By Mora Hannover http://pastebin.com/Lx1jdHhC Menu Example - By ByteMe420 and Mora Hannover http://pastebin.com/aATAX1cw Tutorial would be updated in meanwhile. If there is any missing thing please inform me and i would add it All Questions are Appreciated. Don't Hesitate to ask any question. If you notice any mistake or error in the tutorial it would be nice if you inform me People with credits for informing about errors in the tutorial and correcting them neoh4x0r Deji badboy SideWalk Surfer This post has been edited by Mora Hannover: Dec 22 2014, 04:22 PM -------------------- Rest in Peace Yeardley Diamond (Dec. 21, 1985 - Aug. 20, 2014) Rest in Peace Kitty0706 (1994 - Jan. 25, 2015) | ||||||||||||||||||||||||||||||||||||
Nov 8 2014, 02:38 PM Post #2 | |
Coding like a Rockstar! Posts: 1,468 From: ??? Joined: 28-May 09 | For tables: CODE [tablea ][tr][th]header 1[/th][th]header 2[/th][/tr][tr][td]cell 1[/td][td]cell 2[/td][/tr][/tablea] -------------------- | CLEO 4.3.22 | A?i?a?o?3D | UI SDK | Black Market Mod 1.0.1 | GInput 0.3 | Cheat Keyboard | Tactile Cheat Activation | Stream Ini Extender 0.7 | SuperVars | ScrDebug | Vigilante Justice: San Andreas | |
Nov 8 2014, 08:33 PM Post #3 | |
Ameteur Member Posts: 42 Joined: 3-November 14 | Thanks Bro. Updated -------------------- Rest in Peace Yeardley Diamond (Dec. 21, 1985 - Aug. 20, 2014) Rest in Peace Kitty0706 (1994 - Jan. 25, 2015) |
Nov 9 2014, 02:16 PM Post #4 | |
Trained Member Posts: 76 From: Nederland, NL Joined: 1-May 12 | You should use semicolons more consistent in your tutorial to avoid confusion. You've also used ">>" instead of ">" in an example. The first operator is for bit shifting and the second one is for comparing. Also I would recommend explaining functions and maybe a bit of OOP before moving on to event handlers. It's a nice tutorial, but I'm afraid it's to easy for people who already know other languages and to hard for a real noob. Maybe you should add some working code they can play with, to see what they're actually doing. |
Nov 9 2014, 04:06 PM Post #5 | |
Ameteur Member Posts: 42 Joined: 3-November 14 | for the tip. P.S : This is actually the first time i create a big tutorial. -------------------- Rest in Peace Yeardley Diamond (Dec. 21, 1985 - Aug. 20, 2014) Rest in Peace Kitty0706 (1994 - Jan. 25, 2015) |
Dec 4 2014, 10:49 PM Post #6 | |
Newbie In Training Posts: 16 From: NYC USA Joined: 29-December 09 | Great tutorial! I've been meaning to get into scripting IV and now I'll have a big head start. One really attractive thing about it is you get to use normal programming as apposed to the really limited SCM cleo scripting where you run out of variables so fast. I've really got to find time to learn to use scramble along with super vars which would seem to remedy the limitations but never seem to have time. But yeah some really useful stuff here like the basic programming rundown, the new project example, the key press etc will help jump in quicker. This post has been edited by Sidewalk Surfer: Dec 4 2014, 11:05 PM -------------------- Whoa dude! Mr. Turtle is my father. |