lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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