Skip to main content
GameDev.net gamedev.net
🔒 Locked

Data-access like Data[x]["Key"] = Value

Started by Dummie Jun 17, 2010 at 5:00 PM 3 replies 1.8k views
Original Post
Dummie
Dummie
Hey,

I like to make some data of my program accessible from Lua like that:
Data[x]["Key"] = Value;

I think this is the same:
Data[x].Key = Value;

I already tried it with the following code:
lua_newtable(l);        lua_pushinteger(l, 1);        lua_newtable(l);            lua_newtable(l);                lua_pushstring(l, "k1");                lua_pushstring(l, "v1");                lua_settable(l, -3);            lua_newtable(l);                lua_pushstring(l, "k2");                lua_pushstring(l, "v2");                lua_settable(l, -3);            lua_settable(l, -3);        lua_settable(l, -3);

Unfortunately nothing happens when I access the created table from Lua. It seems my Code is not right. What could be the problem? Where is the mistake?

Greetings
Matthew Shockley
Matthew Shockley
I may be wrong, but I believe you have to settable after every time you push a value.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
m_switch
m_switch
Quote:
Original post by Matthew Shockley
I may be wrong, but I believe you have to settable after every time you push a value.

You are wrong. settable consumes two values from the stack with each call. You should really test this or at least look at the documentation (which clearly suggests that this is not the case) before providing advice.

Quote:
Original post by Dummie
Data[x]["Key"] = Value;

I think this is the same:
Data[x].Key = Value;


It is, the second way is just syntactic sugar for the first. You'll find that Lua is a very sugary language.
Quote:

Original post by Dummie
Unfortunately nothing happens when I access the created table from Lua.

Define "nothing".

Quote:

It seems my Code is not right. What could be the problem? Where is the mistake?


You're pushing strings onto the stack as values when you want to push a table. This is one way to do it, here I've commented what would be on the top of the stack at each point.

lua_newtable(L); //Datalua_newtable(L); //Data, subDatalua_pushinteger(L, 1); //Data, subData, 1lua_pushvalue(L, -2); //Duplicate subData - Data, subData, 1, subDatalua_settable(L, -4); //Data[1] = subData - (pops 2) Data, subData//setting a fieldlua_pushstring(L, "v1"); //Data, subData, "v1"lua_setfield(L, -2, "k1"); //subData["k1"] = "v1" - Data, subData//pop off the subtablelua_pop(L, 1); //Data
Dummie
Dummie
Thank you for your very helpful post. Your code is working perfectly and the explanation helped me to understand it. Thanks :)
Matthew Shockley
Matthew Shockley
You don't have to be so rude about it. I mean seriously, I'm only fourteen. I'm just trying to help.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.