Forum

> > CS2D > Scripts > Variable
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Variable

8 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Variable

Maathy
User Off Offline

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

alt Re: Variable

DC
Admin Off Offline

Zitieren
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.

alt Re: Variable

Rocik
User Off Offline

Zitieren
Global.
If you need local you need function initArray

alt Re: Variable

Banaan
User Off Offline

Zitieren
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

alt Re: Variable

Banaan
User Off Offline

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

alt Re: Variable

Maathy
User Off Offline

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

alt Re: Variable

Rocik
User Off Offline

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

alt Re: Variable

KimKat
GAME BANNED Off Offline

Zitieren
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.

Zitat
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht