Forum

> > CS2D > Scripts > [GUIDE] How to script
Forums overviewCS2D overview Scripts overviewLog in to reply

English [GUIDE] How to script

45 replies
Page
To the start Previous 1 2 3 Next To the start

important moved [GUIDE] How to script

Starkkz
Moderator Off Offline

Quote
Hello, I'll show you my Lua tutorial for you to learn the Lua scripting basics and some other stuff that will help you to know new things. I'll keep adding new content as I can do it (Twice a weekend). If you want to support this tutorial just comment it and I'll add it to the first post.

Before you start reading my tutorial, I would like to name some scripting programs/tools that you could use to learn Lua scripting.
• SciTE Included in Lua for Windows
• Notepad++ A nice editor with multiple features and scripting languages
• Cloud9 IDE A website that supports multiple scripting languages and collaborative scripting with other scripters.
• ZeroBrane Studio (My favorite scripting editor)

IMG:https://i.imgur.com/EFhZ2mL.png

You might have been taught on school some basic mathematics stuff where you use letters and numbers to make complex formulas, you can create them the same way as you do in mathematics.

• Numbers
Everything that is related to numbers is used for mathematical formulas, this is how you define a number.
1
2
3
NumberA = 1
NumberB = 100
NumberC = -10

• Strings
Strings, also known as sentences. It's a set of characters (numbers, words, etc) that makes a sentence. You always have to put quotes on the begining and at the end when you define them.
1
String = "Hello world!"

IMG:https://i.imgur.com/w6ETQrY.png

As you have noticed before, the = operator is used to assign variables, but there are more operators that do different things! You can multiply, divide, add, susbtract and even more. I'll show you some essential operators here.
> Z = X + Y
Z is equal to X plus Y
> Z = X - Y
Z is equal to X minus Y
> Z = X / Y
Z is equal to X divided by Y
> Z = X * Y
Z is equal to X multiplied by Y

This is how you use them
1
2
3
4
5
6
NumberA = 5
NumberB = 15
NumberC = NumberA + NumberB
NumberD = NumberA - NumberB
NumberE = NumberB / NumberA
NumberF = NumberB * NumberA

> What does the previous code mean?
NumberA is 5
NumberB is 15
NumberC is 5 plus 15, that means it's 20
NumberD is 5 minus 15, that means it's -10
NumberE is 15 divided by 5, that means it's 3
NumberF is 15 multiplied by 5, that means it's 75

• The string operator (Concatenate)
Just as you have a + operator for numbers, there's a special operator that is used to mix two strings as one, this is what is called concatenating. This operator is written as two dots.
1
2
3
StringA = "I like"
StringB = "Pie"
StringC = StringA .. StringB
StringC will result as one sentence that contains "I likePie".

Wait what? Shouldn't there be a space between "like" and "Pie" ?
Yes, but you would have to add the space character when you get those strings together.
1
2
3
StringA = "I like"
StringB = "Pie"
StringC = StringA .. " " .. StringB

IMG:https://i.imgur.com/1lBCBml.png

Functions are used to do more tasks and use less coding, functions have a number of arguments that are passed from the code that called it. This is how you call a function.
1
print("Hello world!")
The previous code is supposed to display the string "Hello world!" on the CS2D's server console. The print function is a function added by default from Lua, but it was modified to work with CS2D's console.

> How do I make my own function?
You have to write the keyword "function" and your function name, then you open a parenthesis/brackets () with the name of the arguments that are passed to the function, each argument must be separated by a comma.
1
2
3
4
5
6
7
function myFunctionName(ArgumentA, ArgumentB)
	print("The first argument is")
	print(ArgumentA)

	print("The second argument is")
	print(ArgumentB)
end

> What does ArgumentA and ArgumentB mean?
They're the name of the arguments used in this function, you can give it any name but you can only use word characters. When this function is called, ArgumentA will be replaced with the first value passed to the function and ArgumentB with the second one.
1
myFunctionName("Test", 123)
In the previous code we are calling our function, ArgumentA is "Test" and ArgumentB is 123.

> What does "end" mean?
The "function" keyword is used to start a new chunk of code, you have to use "end" to let the script know that there's where your function's code finishes.

IMG:https://i.imgur.com/OCBTjpv.png

> What if you want certain code to be executed, but you don't want another part of your code to be executed?
You can use conditionals. They try to figure out if a condition is possible or impossible.

> How do I make a condition?
Firstly, you have to write the keyword "if" and your condition.
Secondly, you have to write the keyword "then" and the code that will be processed when the condition is possible.
Finally, you have to write "end" to let your conditional know that there's where your condition ends.
1
2
3
4
A = 10
if A == 10 then
	print("A is 10")
end

> Why does that condition use "==" and not "="?
The "==" operator is a comparison operator, it is used to compare two variables. However "=" is just used to assign values. In the previous code we have assigned A to 10 and then we compared A with 10, it was a condition that is possible so it should print "A is 10" to our console.

IMG:https://i.imgur.com/ZZF0n83.png

> What is a loop?
A loop is a script that gets executed a certain amount of times in a moment.

• "for" loop
The "for" loop executes a script and gives it a variable that will have a different value for every execution. This is how you make a "for" loop.
1
2
3
for i = 1, 100 do
	print("i has a value of: "..i)
end
The print function will be called 100 times, every execution will have a different value for i. It will start from 1 until it reaches 100.
1
2
3
for Value = From, To do
	-- Script here
end

IMG:https://i.imgur.com/auNPN68.png

Here we go with another variables lesson, you will get used to booleans and to tables.

• Booleans
Boolean is a data type with two possible values: true or false, in some other scripting languages true is 1 and false is 0. You can't compare a boolean with a number or a boolean with a string, but you can compare two booleans.

1
2
UnrealSoftwareRocks = true
HacksRocks = false

• Tables
The table is the most complex concept of Lua, you might want to imagine it as a list of elements. Imagine a list of the scores/places in a competition, you could imagine it as an excel table aswell.

Score list example in lua >


> What is the index/key?
The index or the key is the position of a value in our Lua table.
1
Table[Key] = Value

> What types of values can I put in a table?
You can put any type of value; number, strings, booleans and even more tables!

> Can the key/index be just a number?
No, the key can be any type of value too, but it's mostly used for strings and number than other types of values.
Cases >


IMG:https://i.imgur.com/KyRDzaJ.png

There are even more operators that you didn't know they existed.
> X < Y
X is lower than Y
> X > Y
X is higher than Y
> X <= Y
X is lower or equal to Y
> X >= Y
X is higher or equal to Y
> X ~= Y
X is different to Y
Operators as variables >


> How do I use those operators?
You can use them the same way you're using you use the comparison operator.
1
2
3
4
5
6
MinimumAgeForTheMovie = 18
YourAge = 17

if MinimumAgeForTheMovie >= YourAge then
	print("You can enter to the cinema sir")
end
In the previous code we were trying to check if 18 was higher or equal to 17, and this conditional is possible because 18 is higher than 17.

IMG:https://i.imgur.com/DCn30hI.png

> What does it mean that functions can return values?
Functions don't just do tasks, they also return values. That's why we have a "math" table with functions that we can use for mathematical operations.
1
Value = math.sqrt(4)
The previous script should set Value to the square root of 4, the square root of 4 is 2 so Value is set to 2.

> Can I make a function that returns something?
Yes, you just have to write the keyword "return" and the value that you want your function to return.
1
2
3
4
function add(x, y)
	return x + y
end
Value = add(10, 5)
The previous function sets Value to 10 increased by 5.

IMG:https://i.imgur.com/2LJiCf6.png

> What does "elseif" mean in a condition?
When the first condition did not succeed, we can put the keyword "elseif" with a condition, and if the "elseif" condition is not possible either we can put another one and so on.
1
2
3
4
5
6
7
8
9
A = 3
B = 2
if A + B == 6 then
	print("A + B is 6")
elseif A / B == 6 then
	print("A / B is 6")
elseif A * B == 6 then
	print("A * B is 6")
end

> What does "else" mean in a condition?
The keyword "else" is used when none of the previous conditions succeeded, so we put the keyword with the code that will be executed alternatively.
1
2
3
4
5
6
Value = "Test"
if Value == "asd" then
	print("Value is asd")
else
	print("Value is not asd, it's something else")
end

IMG:https://i.imgur.com/yKEShuJ.png

• "while" loop
The while loop works similar as the conditional, the difference is that it executes a script while the condition used is still possible.
1
2
3
4
5
6
A = 0
while A < 10 do
	A = A + 1
	print("A increased by 1")
end
print("A is "..A)

• "repeat" loop
The repeat loop also works similar as the conditional and the while loop, but it works with a conditional that is NOT possible.
1
2
3
4
5
6
A = 0
repeat
	A = A + 1
	print("A increased by 1")
until A >= 10
print("A is "..A)

> And why don't we use the "while" loop instead of the "repeat" loop?
The repeat loop has slightly higher frequency of execution than the while loop, that means that its code is executed faster.

IMG:https://i.imgur.com/H3eca92.png

> What does it mean that tables are pointers?
It means that if you modify a element in that table, any other object that has a copy of that table will be modified aswell.
1
2
3
4
Table = {}
CopiedTable = Table
Table.VAR = "This is a test"
print(CopiedTable.VAR)
In the previous code we made a table named Table and made a copy of it named CopiedTable, if we modify the key "VAR" on Table it will be modified on CopiedTable too.

> What does the RAM usage have to do with scripting?
The RAM memory is the memory that saves all your script variables, it is limited but on newer machines you won't have to worry about the RAM usage.

> What is a nil value?
A nil value means a value that is empty, nothing in it. You can use the keyword "nil" to free some memory from your scripts.
1
2
Table = {1, 3, 12, 60, 43, 123, 12348, 12347}
Table = nil
In the previous code we have setup a table and then removed it's code from our script, we have set free some memory.

IMG:https://i.imgur.com/Icw8UjH.png

> What is a logical operator?
We have two keyword logical operators, "and" and "or". You will see them most of the time appearing in conditionals like this.
1
2
3
4
5
6
CS2DRocks = true
UnrealSoftwareRocks = true

if CS2DRocks == true and UnrealSoftwareRocks == true then
	print("Our community rocks!")
end

• "len" operator (#)
Then "len" operator also known as length operator is just a character that is put before a variable's name to know what it's length is, it will only be available for use in tables and strings. Tables will tell you how many elements they have, and strings will tell you how many characters they have.
1
2
String = "This is a test string"
StringLength = #String
The length of the previous string is 21 characters, so our variable StringLength should be 21.

1
2
Table = {1, 6, 3, 9, 8, 2, 4}
TableLength = #Table
Our table has 7 elements, so our variable TableLength should be 7.

• Modulus operator (%)
You might think that the modulus operator is a percent, but it actually is what remains from a division.
1
2
3
A = 10
B = 5
C = A % B
B fits completely in A two times (5 * 2), so 10 - 5 * 2 results zero, that means C is zero.

1
2
3
A = 9
B = 5
C = A % B
B fits ONCE in A, (5 * 1), so 9 - 5 results 4, that means C is 4

• "pow" operator
The "pow" operator gets the result of a number multiplied by itself a certain amount of times.
1
A = 2 ^ 4
The first value is raised to the power of the second value, that means that 2 is multiplied by itself 4 times.
1
A = 2 * 2 * 2 * 2

IMG:https://i.imgur.com/EbXUdGf.png

Can I put a function inside a table?
Yes, just as you've been taught in the previouses lessons, you can define functions in different ways.
Cases >


• Some functions you might want to use for your scripts
tostring: The "tostring" fuction converts any type of value into a readable string. Normal strings are not affected by this function.
1
2
Number = 123
String = tostring(Number)
String is "123"

tonumber: The "tonumber" function converts certain type of values into numerical values, if it can't be converted then it results nil.
1
2
3
4
5
A = "0123"
B = tonumber(A)

C = "test"
D = tonumber(C)
B is 123 and D is nil because "test" cannot be converted into a number.

pcall: The "pcall" function calls a function with some arguments, the special part of it is that it catches Lua errors and doesn't throw them to the console, but you receive them as a string value.
1
2
3
4
5
function AFunctionThatTriggersAError()
	error("This is the error")
end

Success, Error = pcall(AFunctionThatTriggersAError)
If there was no error Success is true, otherwise Success is false and Error is a string that contains your Lua error.

IMG:https://i.imgur.com/gFWVvyh.png

> Is there any other way to operate with booleans?
Yes, there's a very simple conditional that lets you to know if a value is not nil and not false
1
2
3
4
Value = true
if Value then
	print("Value is not nil and not false")
end

> Does the previous method work just with booleans?
No, it works with any Lua type while it's not nil and not false.
1
2
3
4
5
Value = 12345
if Value then
	print("Value is not nil and not false")
	print("Value is "..Value)
end

IMG:https://i.imgur.com/SuiJvgE.png

> How can I know all the elements that a table has?
There are two methods. The first method is pairs, it loops through ALL elements with key and value and executes a script that you can give to it. The second method is ipairs, it loops numbers keys only and starts from 1 until it finds a empty value.

1
2
3
4
Table = {10, "test", ["keyname"] = "test2"}
for Key, Value in pairs(Table) do
	print("KEY: "..Key.." Value: "..Value)
end
Console has written
KEY: 1 Value: 10
KEY: 2 Value: test
KEY: keyname Value: test2


1
2
3
4
Table = {10, "test", ["keyname"], [5] = 40, [6] = 70}
for Key, Value in ipairs(Table) do
	print("KEY: "..Key.." Value: "..Value)
end
Console has written
KEY: 1 Value: 10
KEY: 2 Value: test

ipairs will skip "keyname" because it is a string, and it will skip the 5th and the 6th element because the 3rd and the 4th elements are missing in the table.

IMG:https://i.imgur.com/Fz5XapB.png

What does it mean that everything is a variable?
It means that every single thing that you've defined in your Lua script's memory will act as a variable, even functions are variables. Using functions as variables will be quite useful if you try to trigger events.

1
2
3
4
5
6
7
8
9
10
11
12
13
Event = {}
function SetEventFunction(EventType, Function)
	Event[EventType] = Function

	-- Let's test our function
	Function("this is a test")
end

function OnEvent(Argument1)
	print("Function called with input: "..Argument1)
end

SetEventFunction("Click", OnEvent)
On the previous script, we made a imaginary case on which a function assigns another function as an event. SetEventFunction has two arguments, the first should be a string on where you will put the name of the event (this is just an imaginary case, it's up to you to decide whatever you want to put there), and the second argument should be the actual function object. So we called SetEventFunction with "Click" event and our function OnEvent where it would be acting as a variable passed to another function through one of it's arguments.

Metatables
This topic isn't quite bound to variables but it needs to be explained now so I can continue with the following topics on the IV cathegory.

The metatables are tables that insert other functionalities into normal tables, it will give you the possibility to use tables as functions, numbers or strings. It does not sound logic but you'll be taught soon how that works.

1
2
3
Metatable = {}
Table = {}
setmetatable(Table, Metatable)
The previous code just shows how to insert a metatable into a table, on the next cathegories you will be taught how to insert functionalities to your metatable.
Note: setmetatable also returns the original table which is going to receive the metatable object.

IMG:https://i.imgur.com/uFBDZxz.png

Add: If you have two objects (ObjectA and Object), let's assume that ObjectA is a table, ObjectB could be anything.
1
2
3
4
5
6
7
8
9
10
11
12
13
Metatable = {}
function Metatable.__add(ObjectA, ObjectB)
	if type(ObjectB) == "number" then
		return ObjectA.Value + ObjectB
	elseif type(ObjectB) == "table" then
		return ObjectA.Value + ObjectB.Value
	end
end

ObjectA = setmetatable({Value = 10}, Metatable)
ObjectB = 35
ObjectC = {Value = 22}
ObjectD = ObjectA + ObjectB
ObjectC results 10 + 35 = 45
ObjectD results 10 + 22 = 32

Sub: Sub works the same way as Add, but the difference is that it uses the minus character (-).
1
2
function Metatable.__sub(ObjectA, ObjectB)
end

Mul: Mul works the same way as Add, but the difference is that it uses the multiplication character (*).
1
2
function Metatable.__mul(ObjectA, ObjectB)
end

Div: Div works the same way as Add, but the difference is that it uses the division character (/).
1
2
function Metatable.__div(ObjectA, ObjectB)
end

Mod: Mod works the same way as Add, but the difference is that it uses the modulo character (%).
1
2
function Metatable.__mod(ObjectA, ObjectB)
end

Pow: Pow works the same way as Add, but the difference is that it uses the exponentiation character (^).
1
2
function Metatable.__pow(ObjectA, ObjectB)
end

Unm: Unm works the similar to Sub, but the difference is that it uses only one argument.
1
2
3
function Metatable.__unm(ObjectA)
	return -ObjectA.Value
end

Concat: Concat works the same way as Add, but the difference is that it uses the concatenation characters (..).
1
2
3
function Metatable.__concat(ObjectA, ObjectB)
	return ObjectA.String .. ObjectB.String
end

Len: Len works the same way as Unm, but the difference is that it uses the concatenation character (#).
1
2
function Metatable.__len(ObjectA)
end

Eq: Eq works the same way as Add, but the difference is that it uses the comparison characters (==).
1
2
3
function Metatable.__eq(ObjectA, ObjectB)
	return ObjectA.Value == ObjectB.Value
end

IMG:https://i.imgur.com/OOtIeFq.png

Function memory
Every function is bound to it's own memory, the memory is the amount of variables this function will have to loop through in order to find a value.

Upvalues
The function's upvalues are values that cannot be modified unless they're pointers (tables can be modified because they're pointers). I'll show you how to make a function with a upvalue on it's memory.
1
2
3
4
5
6
7
8
9
function GenerateFunctionWithUpValue(Value)
	-- This is a function that returns another function
	return function ()
		-- Whatever is returned here will be able to access the arguments from the top
		print(Value)
	end
end
Function = GenerateFunctionWithUpValue("This text is an upvalue being printed")
Function()
GenerateFunctionWithUpValue is a function that generates a new function, this new function is able to access the arguments from the previous function and those arguments will be kept on it's memory until it is modified or removed by the generated function itself. Therefore, calling Function with or w/o arguments will always print the same text.

Function environment
The function environment is like the workspace of your function, _G is the default function environment. However you can create functions and modify their environments so they won't be able to access variables that you want to be locked.
1
2
3
4
5
function TestFunction(Message)
	print(Message)
end
setfenv(TestFunction, {print = print})
TestFunction("test message")
We created a function named TestFunction, after that, we set it into a environment where the only variable available is print.
Hint: This is an optimization trick I use for my heavy functions, the less amount of variables it uses, the better it performs.

Tables as functions under metatables
Following the examples from Operators IV, you will be able to use the __call metamethod to make the tables act as functions.
1
2
3
4
5
6
7
8
Metatable = {}
function Metatable.__call(Table, Arg1, Arg2, Arg3, ...)
	-- When we call our table, our first argument will be the table object
	-- The following arguments will be the arguments assigned to our call.
end

ObjectA = setmetatable({}, Metatable)
ObjectA("test", 123, {Test = "Variable"} )

IMG:https://i.imgur.com/tpcJx0X.png

Conditions under metatables
Metatables can also support the "<" and "<=" operators, they've got their own metamethods which are called whenever you use any of the previouses operators.

Lt: Lt works the same way as Eq, but the difference is that it uses the lower than character (<).
1
2
3
function Metatable.__lt(ObjectA, ObjectB)
	return ObjectA.Value < ObjectB.Value
end
It will be called whenever you use any of the following codes.
1
2
ObjectA < ObjectB
ObjectB > ObjectA
As there is no "higher than" metamethod, you can do it backwards but with the "higher than" character (>).

Le: Le works the similar to Lt, but the difference is that it uses the lower than character and the equal character (<=). Which means we're comparing a variable that could be lower or equal to another one.
1
2
3
function Metatable.__le(ObjectA, ObjectB)
	return ObjectA.Value <= ObjectB
end
It will be called whenever you use any of the following codes.
1
2
ObjectA <= ObjectB
ObjectB >= ObjectA
As there is no "higher equal" metamethod, you can do it backwards but with the "higher than" and the "equal" character (>=).

IMG:https://i.imgur.com/H8T8147.png

Custom loops
Have you ever wondered how the pairs function works like in loops?
1
2
3
for Key, Value in pairs(Table) do
	print(tostring(Key).." = "..tostring(Value))
end

I'll show you the equivalent code in another way.
1
2
3
4
5
6
7
8
9
local PairsFunction = pairs(Table)
while true do
	local Key, Value = PairsFunction()
	if Key and Value then
		print(tostring(Key).." = "..tostring(Value))
	else
		break
	end
end
What it basically does is returning a function that returns arguments, those arguments are passed into the loop. You could create your own function that does the same thing. I'll show you how.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function EachString(Table)
	local Index = next(Table)
	return function ()
		while Index do
			-- Repeat this process until we find a string
			local Key, Value = Index, Table[Index]
			Index = next(Table, Index)

			if type(Value) == "string" then
				-- Return the index and the value
				return Key, Value
			end
		end
		Index = next(Table, Index)
	end
end

TestTable = {1, 5, "Cookies", 30, "Test", {}, print}
for Key, Value in EachString(TestTable) do
	print(tostring(Key).." = "..tostring(Value))
end
The previous example is a function that looks for strings in a table and returns it to the loop.

IMG:https://i.imgur.com/QYjsYEc.png

As you might know, Lua doesn't support a native object oriented programming. For that, I've decided to create my own standard to object oriented programming.

Only few rules are required to do this.
1. The name of the class must be written with the UpperCamelCase style.
2. The name of the file must be the same as the name of the class you're going to make.
3. The name of the class must be initialized as a table at the begining of the code.
4. A '__index' attribute must be added to the table, and it's value must be the table itself.
5. A 'Type' attribute needs to be added, as a string (which is the name of the class) for identification.
6. The constructor & creator of the class must be 'Class:new()' where the 'Class' is the name of your class.
7. The constructor of the class must be added before the metamethods.
8. The methods must be added after the metamethods.

Example for 'MyClassName.lua'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
MyClassName = {}
MyClassName.__index = MyClassName
MyClassName.__type = "MyClassName"

function MyClassName:new(constructor, arguments, etc, ...)
	
	local self = {}
	
	self.Value = constructor
	
	return setmetatable(self, MyClassName)
end

function MyClassName:__tostring() -- This is a metamethod
	
	return tostring( self.Value )
	
end

function MyClassName:__concat(what) -- This is a metamethod
	
	return tostring( self ) .. tostring( what )
	
end

function MyClassName:DoSomething() -- This is a method

end

function MyClassName:GetSomeValue() -- This is a method
	
	return self.Value
	
end

function MyClassName:SetSomeValue(Value) -- This is a method
	
	self.Value = Value
	
end
edited 26×, last 13.11.17 03:43:56 pm

old Re: [GUIDE] How to script

MikuAuahDark
User Off Offline

Quote
If you want to add it:

∗ Tables ∗
It's like an ordered map. You will declare it like this:
1
MyTable={}

Inserting values
1
2
3
4
MyTable={}
table.insert(MyTable,1)
MyTable[2]="Hello World!"
print(MyTable[1],MyTable[2])
Code above should show "1" and "Hello World"

Any values can be used as key and any values can be used as value
1
2
MyTable={}
MyTable[MyTable]=MyTable

If you use tostring to the table, it returns it's address in memory
1
2
MyTable={}
print(tostring(MyTable))
It shows "table: hhhhhhhh" where hhhhhhhh is the table address

You may not compare table with another table, because it only compare based on the address above
1
2
3
4
5
MyTable={}
MySecondTable=MyTable
MyThirdTable={}
print(tostring(MyTable==MySecondTable))
print(tostring(MyTable==MyThirdTable))
It should output true and false

∗ Data Type ∗
Some list of data type
nil - Indicates that it doesn't exist
boolean - Value which should true or false*
number - Value that represent numbers
1
2
MyNumber=12345
MyNumber=0x3039
string - Value that represent sentences
table - Value that represent an ordered map
function - Value that represent a function
thread - Value that represent a coroutine
userdata - It can to be a various value which can be modified it's uses/functions by metatables
* - Unlike C++ or another programming language, booleans are exactly different in Lua. So you can't compare boolean and numbers.

old Re: [GUIDE] How to script

Infinite Rain
Reviewer Off Offline

Quote
Yea, you should add tables.
BTW, I'm pretty sure it's "conditions" but not "conditionals", I might be wrong so sorry if I am.

old Re: [GUIDE] How to script

MikuAuahDark
User Off Offline

Quote
You may also need this one

∗ Some Bulit-in Lua Functions ∗
print(text) - Shows something like a text in console
tostring(anything) - Convert any values to string
tonumber(number[,base]) - Convert string to number and also use base base if specified. Otherwise, use default value(10)
pcall(func,arg1,...) - One of important function that you may need to remember, it call function func with any number of arguments(any values passed there would re-used on the function) in protected way. It means that if there's error in your function, it catch it and returns the error message. In case
1
2
3
status,result=pcall(function() local asdf=nil; asdf() end)
-- status = false
-- result = attempt to call a nil value
newproxy(create_metatable?) - Create new userdata object where you can then use getmetatable to modify it's uses. For advanced scripters.

Tell me if you have some more

old Re: [GUIDE] How to script

Rainoth
Moderator Off Offline

Quote
So using ipairs will return only keys as numbers until a different variable type key is found. Using pairs will return all elements, not just numbers. What's the use of ipairs then? Is it more memory wise or what..?

old Re: [GUIDE] How to script

Starkkz
Moderator Off Offline

Quote
@user Rainoth: I never use ipairs, but you could use it to display hudtexts from a table. Ipairs will go from 1 to the length of the table, if there is a key missing between two numbers then it will stop looping. It will be the same as this:
1
2
3
for Key = 1, #Table do
local Value = Table[Key]
end

old Re: [GUIDE] How to script

Dousea
User Off Offline

Quote
Nice tutorial, user Starkkz. You forgot the usage of _G, metatable, ... and:
function table:functionsname (argument1, argument2)

Also there's another way to compare booleans to check it's true:
if (CS2DRocks and UnrealSoftwareRocks) then

-- Equals to

if (CS2DRocks == true and UnrealSoftwareRocks == true) then

and false:
if (not CS2DRocks and not UnrealSoftwareRocks) then

-- Equals to

if (CS2DRocks == false and UnrealSoftwareRocks == false) then

And you can compare something inside variable:
variable1 = 2
variable2 = 1
variable = (variable1 == variable2)

variable's value should be false because variable1 has different value with variable2.

old Re: [GUIDE] How to script

Starkkz
Moderator Off Offline

Quote
@user Dousea: Of course I know they're not in the tutorial, this is more like a tutorial for absolute beginners. I will probably add that stuff later in the next tutorial.

old Re: [GUIDE] How to script

Raaj Nadar
User Off Offline

Quote
I am making a script in which.
1) It has same txt.
Means :- txt == !somthing.

I want to use the same "!something" twice.

But if a player uses it 1st then it should Enable the function.
But if the player uses it 2nd time then it shoud disable.
But if the player uses it thrice then it should again enable.
And so on....

Which function should i use.. I tried a lot need some help.

2) I want to add a country detector (By and lua System) for a welcome msg (I want to make my own).

Bothe the things that i choosed are very tough but i want to complete my mission.

Thanks & Regards,
Raaj Nadar.

old Re: [GUIDE] How to script

Dousea
User Off Offline

Quote
@user Raaj Nadar:
-- Create random new variable which has false value
something = false

-- Add new "say" hook which triggered when player say something to "sayhook" function
addhook ("say", "sayhook")

-- Create new function for "say" hook
function sayhook (id, message)
	-- Check if "message" is "!something"
	if (message == "!something") then
		-- Check if "something" variable is true
		if (something) then
			-- Change "something" variable value to false
			something = false
		-- If otherwise (false)
		else
			-- Change "something" variable value to true
			something = true
		end
	end
end

Also for country detector thingy, try to learn from file File does not exist (12800).
To the start Previous 1 2 3 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview