Forum

> > CS2D > Scripts > Variable
Forums overviewCS2D overview Scripts overviewLog in to reply

English Variable

8 replies
To the start Previous 1 Next To the start

old Variable

Maathy
User Off Offline

Quote
All variables it is global (for all server) ? Or are local (for player) ?

old Re: Variable

DC
Admin Off Offline

Quote
are you talking about Lua variables? Lua scripts run on the server only. you have to use arrays/tables if you need variables for each player.

old Re: Variable

Rocik
User Off Offline

Quote
Global.
If you need local you need function initArray

old Re: Variable

Banaan
User Off Offline

Quote
Rocik that is complete bullshit. initArray is not a standard Lua function, it's a custom function DC used in his samples to easily fill tables with zeroes, and somehow other people copied it, assuming it was necessary. It has pretty much nothing to do with the variable scope.

If you want a local variable (which you should want 99% of the time), just put the local keyword in front of the first declaration of your variable. It will then be accessible only from within the current function (or script, in case you declare a local variable outside a function). You should actually always use local variables, either within your script or within your function, to prevent collisions.

example:
1
2
3
4
5
6
7
8
9
addhook("second", "counter")

local c = 0
-- the local variable c can be accessed from anywhere within this script, but not from inside another script.

function counter()
	c = c + 1 -- This uses the 'local' variable c as declared above.
	print(c .. " seconds have expired since the execution of this little script")
end

old Re: Variable

Rocik
User Off Offline

Quote
But he throught local as for each player...

old Re: Variable

Banaan
User Off Offline

Quote
In that case, DC's answer already explained it all: tables/arrays (which is exactly what initArray() does)

old Re: Variable

Maathy
User Off Offline

Quote
I still do not understand, can you give me a example ?

old Re: Variable

Rocik
User Off Offline

Quote
Tell we what do you seriously mean...
You looking for variable for all players (32), or something other?

old Re: Variable

KimKat
GAME BANNED Off Offline

Quote
To learn about global variables click this link below.
> Global variables.

Or if you want to read about the different variables that exist in Lua scripting, click the link below.
> The types of variables.

Quote
Variables are assumed to be global unless explicitly declared local
1
2
3
4
function code()
	b = nil
	print(b)
end
That would mean that b is a global variable as used in this code. Yay me? I'm getting better at Lua, I think. Anyways, the code above will print the global variable b as nil when the function is being called.

A local variable is as defined below by using the above code as template but with a slight difference.
1
2
3
4
function code()
	local b = nil
	print(b)
end
Here the b becomes a local variable and is not longer a global variable. The code itself will print the local variable as nil when the function gets called.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview