· 6 years ago · Dec 31, 2019, 04:12 AM
1[Table of Contents]
2- Resources
3- Introduction
4- Variables
5- Strings
6- Concatenation
7- Math
8- Functions / Methods
9- Conditional Statements
10- Relational operators
11- Tables
12- Loops
13- Gamesense Lua
14- Conclusion
15
16Resources
17- Gamesense LUA API
18- Offical gamesense lua scripts these are good example scripts because they are written properly and can be used to learn off of.
19- gamesense lua script help thread
20- gamesense scripting discord (by Sapphyrus, NmChris and Aviarita) here you can get help from other people including myself
21- Game events (CS:GO)
22- Game events (Main page, links to generic / other source game events)
23- Netprops (Credits: Sapphyrus / Phil)
24- Netvars (Credits: Aviarita)
25- Mod Events (Credits: Aviarita)
26- Lua SDK (Credits: Aviarita)
27
28Introduction
29I've decided to create this thread to teach people that know little to nothing about lua to be able to create their own scripts. I've noticed many people such as knight wanting to learn how to make lua scripts but have no idea how to or where to start and end up asking others to teach them, so here. This thread should at least teach you the basics to be able to create lua scripts.
30
31Variables
32Variables are probably the most important thing when it comes to programming / scripting. Variables are where you store your data and they become especially useful when creating complex scripts or scripts where you need to store and keep track of many values such as time, ui elements or other things.
33
34Using and Creating Variables
35
36-- This is a comment, thing of this as a little note for yourself so you can leave your self explanations or TODO's etc.
37
38local my_variable = 1 -- This creates a variable called 'my_variable' and sets it equal to 1
39
40print(1) -- Using the lua interpreter this would print to the console 1 but we are not using our variable
41
42print(my_variable) -- Just as above this will print 1 however we are using our variable 'my_variable' rather then '1' so whatever 'my_variable' is equal to that is what will be printed.
43
44-- Reassigning what the variable is equal to is also easy
45my_variable = 2 -- Now our variable is equal to 2 rather than 1. That is it!
46--in order to assign a new value to your variable all you have to do is put the variable_name = your_value. (Notice how you do not put 'local' when using / assigning new values)
47Strings
48A string is a group of characters such as a word, phrase, sentence, etc. In order to create a string you use "My String" or 'My String'
49
50Example:
51
52local my_string = "Hello World!"
53-- local my_string = 'Hello World!' would also work
54
55print("Hello World!") -- Output: Hello World!
56print(my_string) -- Output: Hello World! Notice how this outputs "Hello World!" even though instead of inserting a string as the argument we are using the variable, that is because our variable is equal to "Hello World!"
57-- A easy way to think of variables is an identifier that has a value so instead of having to type 6000000 for something that you use throughout your script you can create a variable for it and use that instead and you will know the reason for it and if you need to change it you can easily change it at all places it occurs. It makes your code much nicer to read using print(phrase_used_several_times) rather than having several occurrences where the string is retyped. However, don't use variables when they are unneeded.
58Concatenation
59Concatenation is basically a fancy word for joining strings.
60
61How to join strings together
62
63-- in order to concatenate strings all you do is put '..' between two strings
64-- Example:
65local my_string = "Hello"
66local my_other_string = "World"
67local my_joined_string = my_string .. my_other_string .. "!" -- This variable is now equal to "Hello World!"
68Math
69Math is also very important in creating more complex scripts, being able to calculate differences etc with different data to be used is fantastic. For example calculating percentages or calculating distance between two players or even doing more advanced math such as trigonometry to calculate how many degrees apart etc.
70
71Arithmetic operators
72
73local addition = 5 + 2 -- this would equal 7 obviously, but to add two numbers you just use a plus sign
74local subtraction = 5 - 2 -- this would equal 3, to subtract you just use a minus sign
75local multiplication = 5 * 2 -- this would equal 10, to multiply you just use an asterisk
76local division = 5 / 2 -- this would equal 2.5, to divide you just simply use a forward slash
77-- Keep in mind you can also do math on variables for example addition + subtraction = 10
78
79-- In scripting / programming math also does parenthesis first
80-- this means if you want two things to add first then multiply you can do this (5 + 2) * 5 just like on a calculator
81-- Obviously there are more math functions etc but these are the basic operators you will use
82-- the math library in lua provides things such as modulo / modulus (the remainder after division) math.fmod(numerator, denominator)
83Functions / Methods
84Functions or Methods are basically a variable that when called runs code that is defined with that function. It is kind of hard to explain so here is an example on creating and using functions
85
86Creating and Using Functions
87
88-- to create a function you do 'local function function_name() end' in between the end and the first line you put your code
89
90local function my_function() -- Creating a function called "my_function"
91 print("Hello World!") -- Inside these lines before end is what is inside of the function and will be ran when this function is called
92end -- Ends the function, basically needed to let the interpreter know that this is the end of the function and nothing after this should be included in the function
93
94-- in order to call a function all you do is function_name()
95my_function() -- this will run our function and print "Hello World!"
96
97-- However you can also create functions with parameters or arguments and make more advanced functions, here is how you do that
98local function my_function(arg1, arg2) -- You can name arg1 or arg2 whatever you want and you can have as many or as little arguments as you desire just separate them with a comma.
99 print(arg1 .. arg2) -- This is what the function will do, in order to get what you pass into the function you just use the variable that you used when creating the function
100end -- ends the function
101
102-- To call a function with arguments is the same way except instead of leaving the parenthesis empty you fill in the arguments
103my_function("Hello", "World!") -- This will run our function and print "Hello World!"
104-- How does this work??
105-- If you remember our function concatenates argument one and two together and for the first argument i put in "Hello" and the other argument "World!" so it just joins the two together and prints it.
106Conditional Statements
107Conditional statements or 'if statements' are basically statements where you can check if something is true or false. For example checking equality it will return true if equal or false if not equal. So say you want to check if something equals 5 and if it does then run code then you use a conditional statement.
108
109How to use Conditional Statements
110
111local my_var = 5 -- Creates a variable called 'my_var' and sets it to 5
112
113if my_var == 7 then -- "==" is the operator used to check if two things are equal to each other
114 print("my_var is equal to 7") -- Output: my_var is equal to 7
115end -- Ends the conditional statement, just like functions anything in between this and 'then' will be inside the conditional statement and will not run unless the condition is meant
116
117-- However you may be able to tell this code won't run my_var is not equal to 7 it is equal to 5 therefore whatever is inside of the conditional will not run. If we want to add an opposite check for example or 'else' where it will run different code if our first condition is not meant
118
119if my_var ~= 7 then -- using "~=" rather than "==" will check if the two values are not equal rather than they are equal
120 print("my_var is not equal to 7")
121else -- Here is our else statement anything after this will be ran if our first condition is not meant
122 print("my_var is equal to 7") -- Because my_var is not equal to 7 like it checks in the condition this will not be ran however the first print will be ran so it will output "my_var is not equal to 7"
123end
124
125-- another thing you can do is elseif basically you can use this do another check if the first condition isn't meant
126
127if my_var == 7 then
128 print("my_var equals 7")
129elseif my_var == 5 then
130 print("my_var equals 5")
131else
132 print("my_var does not equal 7 or 5")
133end
134
135-- in this condition it will output "my_var equals 5" because the second condition is if my_var is equal to 5 and it does so it will print out what is in that condition
136
137-- Sorry for the bad examples here its something you kind of got to get used to personally I think having to use 'end' etc in lua is stupid since in other languages such as c++ you do if (my_condition) { // my code } and use "{" and "}" to open / close the statement looks much better and its easier to find your mistakes
138Relational operators
139Relational operators are operators such as greater than or less than, they check if a value is within a range.
140
141The operators
142- "==" returns true if the two values are equal or false if they are not equal
143- "~=" returns true if the two values are not equal or false if they are equal
144- ">" greater than returns true if a value is greater than the other or false if it isn't
145- "<" less than returns true if a value is less than the other or false if it isn't
146- ">=" greater than or equal to, returns true if a value is greater than or equal to the other or false if it isn't
147- "<=" less than or equal to, returns true if a value is less than or equal to the other or false if it isn't
148
149Examples:
150
151if 5 == 2 then -- Obviously this will not be met because 5 is not equivalent to 2
152 print("Five is equal to two")
153end
154
155-- However in order to use relational operators you just put them between two values such as the arithmetic operators
156Tables
157Tables are basically variables that contain more variables. A table uses a key or index to store each value.
158
159Creating / basic usage of Tables
160
161local my_table = {} -- This will create a table called "my_table" with no values you can put values in the brackets separated by commas for example local my_table = {1, 2}
162
163my_table[1] = 5 -- This will set my_table[1] to 5 so basically in order to get this data you use my_table[key] which key here is 1 so my_table[1] is equal to 5
164
165-- the keys however do not need to be numbers you can also use strings
166
167my_table["kills"] = 6 -- This would set my_table["kills"] to 6 anyways rather than using numbers as the key you can use a string so to use this you would just use my_table["kills"] just like how you set what it equals
168
169print(my_table[1]) -- Output: 5
170print(my_table["kills"]) -- Output: 6
171Loops
172Loops are used in order to continuously run code. You can think of a while loop like a conditional it will keep running that code until the condition is met. A for loop however is useful for looping through tables for example with looping through all the players in your game.
173
174While Loop
175
176while true do -- Because the condition is always true this will just keep running over and over and not stop however you can replace true with a condition such is my_var == my_var_two and it will loop until this condition is met.
177 print("Hi")
178end
179-- This while loop will just keep printing "Hi"
180For Loop
181
182-- This is where it gets fun because the for loop is the loop you will use the most it can be used so you run a piece of code x amount of times or in order to loop through a table
183
184-- using a for loop
185for i=1, 10 do -- This will run 10 times 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
186 print(i)
187end -- this loop will print out 1-10
188
189-- however you can use for loops to loop through tables here is how:
190local my_table = {1, 2, 3, 4, 5, 6, 7, 8}
191
192for i=1, #my_table do -- #my_table refers to the length of the table
193 print(my_table[i])
194end
195-- This will loop through the table printing all values "1, 2, 3, 4, 5, 6, 7, 8"
196-- Obviously this is not all you can do with loops but this is the basics for what loops are used for
197Gamesense lua
198This section refers to how to use certain things with the gamesense lua api
199
200Accessing the API
201
202-- Accessing the gamesense lua api is simple all you need to do is go to the lua API documentation and find the function you're trying to use in this example I will use client.log we want to create a local variable to store this function for performance
203local client_log = client.log -- This gives us access to the client.log function using client_log (obviously you can still use client.log("my log")
204
205--to call this function we just do it like every other function, using the gamesense documentation we can see the syntax: client.log(msg, ...)
206client_log("Hello World!") -- This will log to our csgo console "Hello World!"
207Events / Callbacks
208
209-- A callback is basically a function that is ran when a game event is fired for example paint is used to draw and player_say when a player types to chat
210
211local client_exec = client.exec
212local client_log = client.log
213local client_id_to_index = client.userid_to_entindex
214local get_localplayer = entity.get_local_player
215
216local function on_player_say(e) -- "e' refers to the event callback different events return different information look at the resources to see what game events exist and what they return
217 local player_id = e.userid
218 local player_index = client_id_to_index(player_id)
219 local local_player = get_localplayer()
220
221 if player_index ~= local_player then
222 client_exec("say Hello World!")
223 end
224end
225
226client.set_event_callback("player_say", on_player_say) -- This is how you register your callback to an event you pass in the event name and then your function
227-- For example the above code will use an event that is fired whenever a player types in chat and will say in chat "Hello World!"
228Using Netvars
229
230-- You can use netvars to get info such as player health, life state, velocity, etc (see netvars reference for netvars)
231local get_local_player = entity.get_local_player
232local entity_get_prop = entity.get_prop
233
234local hp = entity_get_prop(get_local_player(), "m_iHealth") -- This would get local player health and store it as "hp" variable
235Accessing the UI
236
237-- Adding Components:
238-- Look at the documentation and find what UI component you want to add
239local new_checkbox = ui.new_checkbox -- syntax: ui.new_checkbox(tab, container, name)
240
241local my_checkbox = new_checkbox("RAGE", "Other", "My Checkbox") -- Creates a new checkbox called "My Checkbox" in Tab: Rage, Container: Other
242
243-- Getting Value of components:
244local ui_reference = ui.reference -- used to get an existing element example: local hitchance_slider = ui_reference("RAGE", "Aimbot", "Minimum hit chance")
245local ui_get = ui.get -- used to get the value from a ui element
246
247local my_checkbox_val = ui_get(my_checkbox) -- Pass in UI reference to get the value
248Using bitwise operators to check m_fFlags netvar properly (Credit to Wawixs for the original post)
249A bitwise operator is an operator that operates on bit patterns. Think of it basically that the netvar m_fFlags has a pattern and all you're doing is using an operator to change for example some number like 256 and getting a pattern from it for example like 1 or 2. We can use these operators in order to check if the player is jumping, crouching, standing, on the ground or in the air etc using one number and can get all these different flags from that number. Or in other source engine games if they're in water or noclip(Works in cs:go as well).
250
251How to:
252
253local get_prop = entity.get_prop
254local get_local_player = entity.get_local_player
255
256local bit_band = bit.band -- the bitwise operator (in lua many of these different operations are in functions rather than for example in C++ being "<<" etc
257
258local client_log = client.log
259
260local client_set_event_callback = client.set_event_callback
261
262local function on_run_command(e)
263 local localplayer = get_local_player()
264 local flags = get_prop(localplayer, "m_fFlags")
265
266 client_log("Flags: " .. flags)
267end
268
269client_set_event_callback("run_command", on_run_command)
270
271-- The above script all credits go to Wawixs with slight modification from me however this script will print out the current flags (256, 257, etc depending on how many different flags are active (in air, crouching, etc))
272
273-- You will have to play around with some values but here is how you use the bit.band function to check if someone is crouching
274local function is_crouching(ent)
275 local flags = get_prop(ent, "m_fFlags")
276 local crouching = bit_band(flags, 4)
277
278 if crouching == 4 then
279 return true -- player is crouching !!
280 end
281
282 return false -- player isn't crouching
283end
284-- credits to Wawixs
285Conclusion
286I know this tutorial isn't that great but it should be good enough to get some people started. If enough people like this I will spend some time improving everything and adding more tutorials to it if people ask. Anyways thanks for reading, and if you need help with anything join the scripting discord and there are a bunch of people in there to help you.
287
288Last edited by Voltz (2019-01-24 17:11:32)