Archived

This topic is now archived and is closed to further replies.

Passing objects between script and game

This topic is 5128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My game engine uses Lua for scripting, but I can''t really figure out the best way to pass game objects back and forth from the script to the game. Like if I want to create a tank with the script, I may do something like this: t = createObject(TANK); registerObject(t); createObject() would be a function that the game engine exports to Lua so that the scripts can use it. The function then returns some sort of reference to the object that was just created. Then the object is passed back to the game engine through registerObject() which then puts the object into the game''s database so that it''ll show up in the game world. But what is ''t''? If I understand right, Lua only supports a few basic types, and numeric types are (by default) a 64-bit floating point number. So I guess I can either use that or some kind of string. It''d be easiest to make ''t'' a pointer that stores the adress of the object, but using a 64-bit floating point number to store a 32-bit integer would open a pretty nasty can of worms. One solution I can think of is to just compile Lua to use 32-bit integers instead of 64-bit floating point numbers, but I really need floating point numbers in the script, plus I''d like Lua to work fine out of the box. Any other ideas?

Share this post


Link to post
Share on other sites
You should look at toLua which will generate the glue code you need to interface with your engine.

In my engine I also use lua and I also used toLua. It works great and saves me a lot of headache.

I think in your script you should not have to worry with things like registering objects with your managers. Why don't you do it automatically in your C++ class? It will save you lots of mistakes I'm pretty sure.

In my engine it looks like this (let's say for my UI which is scripted):

In C++:

Window::Window(...){
UIManager::getInstance()->addWindow(this);
}
Window::~Window(){
UIManager::getInstance()->removeWindow(this);
}


In lua:

-- creates a window
window = Window:new("Hello World!")
window:delete()


All I have to worry is to create the object correctly. It will automatically appear on the screen. If I don't want the window to appear on screen right now, I simply call it's hide() method and the UIManager will not handle this window. Simple. Effective. I hardly can make a mistake in my lua code. It's also a lot easier to maintain. Object management should be performed in C++ instead of lua, you probably don't have a good lua debugger integrated in your project? Do you? It's going to be a pain in the ass to understand why your object is not working as expected if you write too much code in lua.

All you have to do is to write a few event handler in your lua object to respond to events coming from your engine (click on buttons, collision, action triggered by the user). So your application is written in lua, but the intricacies of the engine are hidden in your C++ code.

But if you want to understand how it works, you should read about user defined types and tags in the lua documentation. In fact tags are pretty clever, you can even simulate object orientation with them. It's pretty cool!

My 2 cents!


-nosfy
Lead programmer
Creation Objet Inc.

[edited by - nosfy on November 27, 2003 4:38:05 PM]

Share this post


Link to post
Share on other sites