Forum

> > CS2D > Scripts > What first lines in examples stands for?
Forums overviewCS2D overview Scripts overviewLog in to reply

English What first lines in examples stands for?

12 replies
To the start Previous 1 Next To the start

old Re: What first lines in examples stands for?

DC
Admin On Online

Quote
1
if sample==nil then sample={} end
Checks if the Lua table "sample" is "nil" (nil means: nothing / does not exist). So it checks if the "sample" Lua table does not exist. In this case (if it does NOT exist) it creates the table by assinging an empty table ({}) with sample={}

1
sample.ads={}
This simply creates the new table "ads" within the samples tables by assinging an empty table ({}).

Why is this done? Welll, this way all samples are stored in the samples tables and each sample has its own sub-table (in this case "ads"). This way they will not collide with tables/variables from other scripts.

old Re: What first lines in examples stands for?

DC
Admin On Online

Quote
Create your own table for it, following the same scheme. Call it yazir (or something else unique) instead of sample.

1
2
if yazir==nil then yazir={} end
yazir.firstscript={}

Afterwards store ALL your variables, tables and functions in yazir.firstscript

e.g.:
1
yazir.firstscript.x=123

old Re: What first lines in examples stands for?

Rainoth
Moderator Off Offline

Quote
1. Decide what you want to create.
2. See if it's not already made (common mistake)
3. See what commands and functions you'll need to create it.
4. Begin creating.
5. If there are any changing variables (e.g. Weather ON/OFF) you can either use a normal variable (e.g. "a = true") or create a table (like in past replies of this thread)
6. Change the values whenever you need.
7. Finish all the functions, attach hooks (if you need any) and you're done.

old Re: What first lines in examples stands for?

VADemon
User Off Offline

Quote
@user Apache uwu: Indeed Lua wouldn't allocate the table to a new chunk of memory even if you wanted it to:
1
2
3
x = string
string.f = 5
print(x.f)

So in terms of time
1
yazir = yazir or {}
... is just a little bit slower when it has to make a new table (almost no difference)
1
2
yazir = nil
yazir = yazir or {}
But in this case:
1
2
yazir = nil
if yazir == nil then yazir = {} end
is faster. Here it only depends on the coding style you want to use.

old Re: What first lines in examples stands for?

Starkkz
Moderator Off Offline

Quote
@user Pagyra: I agree. If I'm not wrong, Lua internal code loops through the tables and compares keys to get values. It's faster if you use more global tables and push stuff into them (functions, strings, numbers, tables, userdata, etc). It should increase the memory usage slightly. The benefit of this system is that you can use ANY type of value as a key for your table, incluiding another table.
1
table[{}] = {}
The previous code should be valid.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview