Sign in to follow this  

LUA: lua_setglobal

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to add a global variable with userdata in it. i made a struct:
struct somedata {
	int a;
	int b;
};

and i wanted to add it by calling:
somedata *tdata = (somedata*)lua_newuserdata(L,sizeof(somedata));
tdata->a = 10;
lua_setglobal(L,"somedata");

but when i call:
print(somedata.a)
in the script. nothing happens.

Share this post


Link to post
Share on other sites
See here for an example of userdata. With some meta table hackery you could make it so that instead of the example (array.size(a)) you could use (a:size()). Then take the basic approach and make it work for what you are trying.

Lua will not allow you to do what you are trying, not without a bit more work anyway [smile]

Share this post


Link to post
Share on other sites
what did i do wrong?

like in the example:
- i called lua_newuserdata() with luastate and size of my struct.
- i set a property.
- i made it a global variable.

so what would i need to do?

Share this post


Link to post
Share on other sites
You didn't expose the variable to Lua. Lua just takes a userdata and stores it, it doesn't know what it represents. All it known is its size. Lua cannot determine the names of the members of the struct - even if it wanted to. It is a good idea that it doesn't do this - it allows you to implement private variables.

However, the alternative is to not use a userdata at all and instead create a regular lua table. I'm haven't done this in C++ before so I'll leave you with this page, which hopefully describes what you need to know.

Share this post


Link to post
Share on other sites
Quote:
Original post by madmaurice
and what about light user data?


Almost the same as a regular userdata, except you only pass Lua a pointer.

If you explain what you are trying to do then maybe we could make a suggestion. You hardly are trying to expose a 2 integer struct called "somedata" to Lua!

What I do for my game is have an array (std::vector actually) of objects I want to expose to Lua. I provide an interface for getting and setting the values. I use simple functions to do this. Example:


struct Example{ int a,b; };

std::vector<Example> examples;


int create( lua_State *L )
{
Example example;
data.b = lua_tointeger(L,-1); lua_pop(L,1);
data.a = lua_tointeger(L,-1); lua_pop(L,1);
examples.push_back(example);
// return index
lua_pushinteger(L,examples.size()-1);
return 1;
}

// and a similar function for b
int setA( lua_State *L )
{
int a = lua_tointeger(L,-1); lua_pop(L,1);
int index = lua_tointeger(L,-1); lua_pop(L,1);
if( index >= 0 && index < examples.size() )
{
examples[index].a = a;
}
else
{
// handle error
}
return 0;
}

int getA( lua_State *L )
{
int index = lua_tointeger(L,-1); lua_pop(L,1);
if( index >= 0 && index < examples.size() )
{
lua_pushinteger(L,examples[index].a);
return 1;
}
else
{
// handle error
}
return 0;
}



Then I write a lua script like so(assuming all the above functions are in a namespace/table "example"):

Example = {}

function Example.new(a,b)
local e = {}
setmetatable(e,{__index=Example})
e.index = example.create(a,b)
return e
end

function Example.getA()
return example.getA(self.index)
end

function Example.setA(a)
example.setA(self.index,a)
end



This may not be the best way, but it has advantages for me. Firstly, the data remains in a contiguous block in C++, so I can store objects by value in a vector, rather than messing around with pointers. Also, I prefer writing table manipulation code in lua. I could change the C++ functions to parse lua tables but I find that awkward to write.

Obviously I just wrote that example off the top of my head - it has neither been compiled nor tested. In addition, it lacks error handling.

Share this post


Link to post
Share on other sites

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this