LUA: lua_setglobal

Started by
8 comments, last by madmaurice 16 years, 3 months ago
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.
Advertisement
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]
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?
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.
and what about light user data?
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 bint 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 eendfunction Example.getA()   return example.getA(self.index)endfunction 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.
i don't want to create an instance i just want to read out a struct instance when it was giving as a parameter or as a global.

oh sry didn't checked your post.
ok now i made a class for that but any error occurs while linking -.-
damn! so much work and no result.
Quote:Original post by madmaurice
ok now i made a class for that but any error occurs while linking -.-
damn! so much work and no result.


If you need help, post the errors and code.
i made a new project and it worked. but now i can't read out a whole table.

This topic is closed to new replies.

Advertisement