Forum

> > CS2D > Scripts > Lua Question - Join two variables together
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Lua Question - Join two variables together

6 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Lua Question - Join two variables together

EngiN33R
Moderator Off Offline

Zitieren
So I was wondering, is it possible to join two variables together like strings?
You can do
1
2
3
a = "boi"
print("mah "..a)
mah boi
But can I use it for variables, like there are variables ci1-ci9 (from 1 to 9)? I tried ci..sel (in menu hook), but it doesn't work. Is it at least possible?

alt Re: Lua Question - Join two variables together

DC
Admin Off Offline

Zitieren
why don't you use tables (arrays) for this?

1
2
3
4
5
6
7
ci={}		//initialize array
ci[0]=...			//set value at index 0 (same as ci0=...)
ci[1]=...
...
ci[9]=...

ci[sel]			//get value at index sel (that's what you tried with ci..sel)

alt Re: Lua Question - Join two variables together

EngiN33R
Moderator Off Offline

Zitieren
ci is a table already - I've used initArray
1
2
3
4
5
6
7
function initArray2(f,v)
	local cmd={}
	for c=1,f do
		cmd[c]=v
	end
	return cmd
end
I want to do an initArray3, so a double table, but I'm too dumb to do it.

alt Re: Lua Question - Join two variables together

KimKat
GAME BANNED Off Offline

Zitieren
Blazz what does that code do anyway?

I've tried to understand it but all it tells me is that it sets zeroes for the arrays in the index way. Like a[0] = 0, x = 0, y = z and the (m,n,k) are the parameters that pass the variables and assigns them. The result would be (0,0,00) or something like it, but I now see it only returns (00) if I'm correct because a would then contain 0 and 0 taken directly from the variables x and y. Am I getting this completely wrong? or not?
1× editiert, zuletzt 08.01.11 16:42:04

alt Re: Lua Question - Join two variables together

Lee
Moderator Off Offline

Zitieren
It's also technically possible to concatenate the variable names together, this however requires that you use global variables (AKA not prepending definitions with the local keyword)

1
2
3
4
function get_ci(n) return _G["ci"..n] end

-- Test code
assert(get_ci(1) == ci1) -- True

However, this is not recommended as it implies that you will need to statically initialize everything, which defeats the whole purpose of Lua.

@KimKat:
It creates a two dimensional "array" of m by n (so a total of m*n elements). It then sets every single one of these elements to the argument k supplied by the function call.

A little caution about these types of functions, excluding primitive types (immutables such as string, number, nil), everything is passed by reference, this means that if you try to execute something like this, you may be surprised

1
2
3
a = doublearray(2,2,{1})
a[1][1][1] = 3
assert(a[1][1][2] == a[1][1][1])

So basically, don't pass in tables into this function.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht