Using array of strings to access variables

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Scotty
Posts: 69
Joined: Fri Nov 07, 2014 2:59 pm

Using array of strings to access variables

Post 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!
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: Using array of strings to access variables

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

Code: Select all

print(table["varname"])
Scotty
Posts: 69
Joined: Fri Nov 07, 2014 2:59 pm

Re: Using array of strings to access variables

Post 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
Scotty
Posts: 69
Joined: Fri Nov 07, 2014 2:59 pm

Re: Using array of strings to access variables

Post 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!
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Using array of strings to access variables

Post by NutJob »

You're correct, yes. Use pairs when using associative tables.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Using array of strings to access variables

Post 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
Scotty
Posts: 69
Joined: Fri Nov 07, 2014 2:59 pm

Re: Using array of strings to access variables

Post 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.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: Using array of strings to access variables

Post 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)
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Using array of strings to access variables

Post 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.
Scotty
Posts: 69
Joined: Fri Nov 07, 2014 2:59 pm

Re: Using array of strings to access variables

Post 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.
Post Reply