[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How do I manipulate C++ vectors of C structs in Lua?
- From: "mark gossage " <mark@...>
- Date: Mon, 11 Sep 2006 18:47:11 -0600
Hello there,
To get your access to the vector, should be the same as getting access to the elements of the SPRITE_USERDATA, ie "use metatables".
I will give you an idea of how SWIG does its wrapperings:
The g_SpriteVector will be accessible within Lua via a userdata which holds a pointer to the g_SpriteVector and a metatable which holds all the methods.
When you call g_SpriteVector[0] from lua, what happens is this is routed (via the metatable) to a function
SPRITE_USERDATA* g_SpriteVector_get(int idx){return g_SpriteVector[idx];}
(or something like this), which will return a pointer to the sprite (again wrappered as a usedata)
And so, hey presto, you get your sprite which you can then manipulate.
If you don't want to mess about with the metatables, you can easily write a SpriteVector_get() fn which you can call.
Alternatively, you can look at one of the autobinders (SWIG, tolua, tolua++, luabind, luna). I recomend SWIG, but them I'm biased :-)
Hope this helps,
Mark Gossage
>
> This may seem like a question that has been answered dozens of times with
> "use metatables". However, I'm having trouble figuring out how to
> apply that
> to the problem I have right now. I'll try to explain...
>
> I need to manipulate sprites with Lua (I am embedding Lua in my own simple
> video game engine). A sprite is just a graphic that can be placed anywhere
> on the screen. I have this C structure for a sprite:
>
> struct SPRITE_USERDATA
> {
> bool Enabled; // Should this sprite be drawn?
> lua_Number X; // X position
> lua_Number Y; // Y position
> };
>
> This structure can't specify which graphic to use. It doesn't need to
> because there is only one graphic available at the moment.
>
> My program keeps a C++ vector of sprites. On each frame, it goes through the
>
> vector and draws all of the sprites in it:
>
> vector<SPRITE_USERDATA*> g_SpriteVector;
>
> A C function "Sprite_New" is exported to Lua. It adds a
> SPRITE_USERDATA to
> the end of the vector. I'm not worried about any "Sprite_Delete"
> function
> yet. I need to get this working first.
>
> All I need is a way to access the elements of g_SpriteVector from Lua. Then,
>
> I need to be able to manipulate the "Enabled", "X", and
> "Y" fields of the
> structure. In other words, I want to be able to access g_SpriteVector in Lua
>
> just like I can in my C++ code.
>
> I hope this is all understandable. I was thinking of using Lua Userdata
> objects to represent the sprite vector and each sprite. Then, I could give
> them metatables which let me access g_SpriteVector like an array and the
> SPRITE_USERDATA elements like tables. I need help figuring out how to do it,
>
> if it is possible.
>
> - Checkmate