Page 1 of 2
Using array of strings to access variables
Posted: Fri Nov 07, 2014 3:07 pm
by Scotty
If I run the following chunk of lua script in an online interpreter it works perfectly fine and prints out 5.
Code: Select all
table = {"varname"}
varname = 5
print (_G[table[1]])
However, if I try and run it in my dungeon script, I get the error: "attempt to index global '_G' (a nil value)"
Does anyone know why this is? Any alternative way to access variables via strings?
Thanks!
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 3:12 pm
by MrChoke
We do not get access to all of the LUA capabilities in the game. I am sure the DEVs did this on purpose for our own protection. So we get no access to the "environment". We also get no _metamethod capability. I don't think we get most of the "raw" commands either. In fact, we cannot even iterate through most game objects with pairs or ipairs. They return 0 rows even though they are tables.
Regarding your code example though. You can use pairs() and ipairs() on your own tables to iterate. You can also directly access an object if you specify the key.
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 3:37 pm
by Scotty
I'm very new to lua by the way.
So pairs and ipairs just steps through the table right? Doesn't gain access to the variables from the strings on its own.
Code: Select all
table = {"varname"}
varname = 5
print (table["varname"])
Is just giving me nil there
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 3:51 pm
by Scotty
Oh I see, like this:
Code: Select all
table = {"varname"}
table["varname"] = 5
print (table["varname"])
So instead of using number indexes, using string indexes.
This should work for what I'm doing, thanks!
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 3:54 pm
by NutJob
You're correct, yes. Use pairs when using associative tables.
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 3:56 pm
by petri
_G does not exist because there is no such thing as global namespace in Grimrock. Maybe we can help if you tell what you are trying to do?
EDIT: Maybe this what you are looking for:
local t = { var = 5 }
print(t.var) -> prints 5
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 4:07 pm
by Scotty
Oh wait, no this doesn't work in grimrock:
Code: Select all
test_array = {}
test_array["test"] = 1
print (test_array["test"])
Global error.
So here's what I'm trying to do.
I'm working on a procedurally generated dungeon. I have a bunch of tables that store layout data for chunks of the dungeon. For example:
Code: Select all
module_corner_1_walls = {
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
2,0,0,0,1,1,1,
1,1,1,0,1,1,1,
1,1,1,0,1,1,1,
1,1,1,2,1,1,1
}
and there'll be a bunch of these, all the way up to say module_corner_15_walls.
I then have an array that stores the names of all these layouts:
Code: Select all
corner_walls = {"module_corner_1_walls", "module_corner_2_walls", "module_corner_3_walls" etc....}
What I want to be able to do is randomly select a string from corner_walls, and use that string to grab the corresponding layout from above.
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 4:10 pm
by petri
Scotty wrote:
Code: Select all
test_array = {}
test_array["test"] = 1
print (test_array["test"])
Global error.
? This prints 1 as it's supposed. btw. you can leave out the brackets, the above code is equivalent to:
Code: Select all
test_array = {}
test_array.test = 1
print(test_array.test)
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 4:13 pm
by NutJob
Sometimes writing ["insideQuotes"] is good for constructing the key for generated lookups, and to the OP yes that should work (don't know why it's not for you), but the dot.operator is feasible too with different lookup methods.
Re: Using array of strings to access variables
Posted: Fri Nov 07, 2014 4:20 pm
by Scotty
Ah yep, that does work. I think I still had another bit of bad code floating around.
Thanks for the help, this'll work nicely.