[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Inheriting from C++ Classes
- From: Nick <nick@...>
- Date: Fri, 23 Jan 2009 16:57:57 +1300
I'm making a game engine, which is using Lua for scripting, using Lunar
to export C++ classes to Lua.
I'd like to be able to add unique methods to individual instances in
Lua, they don't need to be accessible to C++.
As an example, I have a Sprite class that is used for a lot of the
objects in the game, I'd like the instances of these objects to define
their own Update function, but this isn't currently possible with the
set up that I seem to have (Update is NOT a function in the C++ object):
PlayerSprite = Sprite()
PlayerSprite:SetImage("Player.png")
PlayerSprite.Update = function (self)
--Update the player's sprite e.g., animation, collision detection or
whatever
end
EnemySprite = Sprite()
EnemySprite:SetImage("Enemy.png")
EnemySprite.Update = function (self)
--Update the enemy sprite
end
Update Functions can be called fine even though they don't exist in C++
however calls to PlayerSprite:Update call the same function as
EnemySprite:Update, seems like its being added to the 'class' not the
instance.
I've used Ignacio Burgueño's modifications to enable property style
access, and uncommented this part which could be the problem based on
this comment in spanish (in Lunar.h):
// Esto es opcional. Si lo dejo, le puedo agregar funciones
// a un objeto trabajando sobre una instancia, sino tengo que agregarlas
// a la clase
if(lua_type(L, 3) == LUA_TFUNCTION) {
lua_pop(L, 1); // stack: userdata, clave, valor
lua_rawset(L, lua_upvalueindex(2));
}
else {
lua_pop(L, 1); // stack: userdata, clave, valor
lua_rawset(L, lua_upvalueindex(2));
}
Instead of a lua_rawset(L, lua_upvalueindex(2)) I tried setting to my
object on the stack (lua_rawset(L,-3)) but that just crashes the
program, I assume because its userdata not a table.
Is there any way of doing this along these lines (much preferred) or
will I need to add some sort of function registration system in the C++
code.
Cheers,
Nick
--
*Nick Koirala*
<http://www.littlemonkey.co.nz>