Skip to main content
gamedev.net
🔒 Locked

Using LUA with a fully OOP Engine

Started by Bu6mAn 2 replies 650+ views
Original Post
Bu6mAn
Bu6mAn
Hi there, i've just finished up reading the LUA Language documentation. I'm working on a graphic adventure engine, written in C++ and fully OOP (practically no standard c functions right now). I want to use LUA as the scripting language for the different graphic adventures that might be made with my engine. Currently i have more or less this layout (i'll give you just a rough sight): A cUtopiaEngine class object, that holds as attributes other subsystems and objects. Here's the interface (sorry about the comments in spanish but it's my native language. The notation and elements names are in english though)
          
// ------------------------------------------------------------------------- //

// Interface: Class cUtopiaEngine                                            //

//                                                                           //

// Descripcion: Main Engine Class                                            //

//                                                                           //

// Atributos: CL_ResourceManager* aResManager; // Manager para los Resources //

//            CL_Slot mousePress; // Slot para el evento Mouse Button Press  //

//            CL_Slot keyPress; // Slot para el evento Keybord Key Press     //

//            std::vector<cRoom*> aGameRooms; // Listado de Rooms            //

//            cRoom* aRoom; // Room actual                                   //

//            cPersonaje* aPersonaje; // Objeto Personaje de la AVG          //

//            int aDesiredAction; // Accion que el usuario eligio en el menu //

//            int aGameState; // Estado del juego: ej: IN_GAME, AT_MENU, etc //

//                                                                           //

// Metodos: cUtopiaEngine(); // Constructor                                  //

//          void mLoadNewRoom(int nRoomId); // Carga una nueva Room          //

//          void mHandleClick(const CL_InputEvent &key);                     //

//          void mHandleKeyPress(const CL_InputEvent &key);                  //

//          void mRun() // Es el main loop que chequea Input,Sound,GameLogic //

//          void mUpdateWorld() // Realiza la Logica necesaria segun Status  //

//          void mDrawWorld() // Blitea todos los gráficos necesarios en su  //

//                            // estado actual.                              //

//          void mWalkTo(int nX, int nY); // Mueve el personaje al destino   //

// ------------------------------------------------------------------------- //

class cUtopiaEngine {

 private:
 CL_ResourceManager* aResManager;
 
 // Input Handling

 CL_Slot mousePress;
 CL_Slot keyPress;
 
 // Game Objects

 std::vector<cRoom*> aGameRooms; // Listado de Rooms

 cRoom* aCurrentRoom; // Room Actual

 cPersonaje* aPersonaje; // The EGO

 cPathSystem* aPathSystem; // Pathfinding System

 TiXmlDocument aXMLDoc; // XML Support for resource data

 
 // GameState Crap

 int aDesiredAction;
 int aGameState;

 public:
 // Initialization

 cUtopiaEngine();
 void mLoadNewRoom(int nRoomId);
 
 // Input Handling

 void mHandleMouseClick(const CL_InputEvent &key);
 void mHandleKeyPress(const CL_InputEvent &key);

 // Game Logic

 void mRun();
 void mUpdateWorld();
 void mDrawWorld(); 
 void mWalkTo(int nX, int nY);

};
          
Finally, my question is, since i'm just starting up with LUA, AFAIK i could register C Functions at the host program, that may be called from LUA Scripts. But is it possible, to use the host program objects, directly from LUA Code, without having to wrap each object method via a C Function? I mean, suppose i want to call the method of the main negine object from LUA Code: objEngine->mLoadNewRoom(1); What i would do right now, is have a C Function at the host program, that wrapps that method, and register that to LUA so it can be called. IE: static int LoadNewRoom(lua_State *L) { // Get the room ID from the Lua Stack ... // Call the engine method for loading up a new room objEngine->mLoadNewRoom(nRoomID); } ------------------------------------------------------- Obviously, doing this for every engine method, is not quite efficient, cause i would have a layer of cfunctions wrapping an object methods, and after that layer the whole encapsulation OOP gives. So i would really like to know if LUA can interact directly with C++ Objects somehow. I'll keep reaserching meanwhile. Thanks in Advance, Bugo // EDIT: i've just read something about an implementation called TOLUA, which might be the solution for my problem. But i'm still open to advice. [edited by - Bu6mAn on March 11, 2003 4:23:38 PM]
thehurricane
thehurricane
quote:
So i would really like to know if LUA can interact directly with C++ Objects somehow.


Yep, you're right--ToLua is probably what you are looking for. I'm using it for my 2D sidescroller and its working great, it takes a lot of the effort out of the "glue" code...if you're using the Lua5 beta, it won't work yet, they're working on ToLua 5, but yeah, I'd definitely recommend at least taking a look at it.

It's easy, you just make stripped down .H files and have ToLua.exe generate the cpp code that you include in your project...for example:

class Monster
{
Monster(int x,int y,char* type);
~Monster();
int posX;
int posY;
DoAnim();
ChangeState(AIState* state);
};

Then in your lua code you can create a new monster object like this:

m = Monster:new(5,10,"ghost")

...it's good stuff

[edited by - thehurricane on March 11, 2003 5:22:40 PM]
Bu6mAn
Bu6mAn
Thanks, just a doubt you might be able to answer: The generated .cpp file is only for accesing the class from LUA, but if i plan on using that class inside the C++ code, i should still use the original .h file right?

So if i'm not wrong it should go like this:

- To use objects from some class un LUA Code, i use the ToLua parser and generate a binding code (a .cpp)

- To use objects of that same class in C++ code, i should still be including the original .h file in my project.

That way, objects of that class might be used at both places, LUA and C++ right?

Thanks in advance. //

[edited by - Bu6mAn on March 11, 2003 5:35:42 PM]

[edited by - Bu6mAn on March 11, 2003 6:27:16 PM]
thehurricane
thehurricane
Yes, it sounds like you have the idea. Including the original .h file will let you use the class in C++, but running the .pkg file through ToLua and including that resulting c++ file will allow you to use the class within lua. Actually first you have to "open" them in C++ like this: say your class name is Monster and you have a lua_State pointer L:

tolua_Monster_open(L);

Then you''re good to use Monster:new() and whatever other Monster methods/variables in Lua.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.