Sign in to follow this  

Unity Exposing functionality to scripts

This topic is 4246 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

I finally got Lua to play nicely with my C++ application last week, but now I'd like to know a bit more about where people tend to draw the line between compiled code and their scripts. I have three questions, that I'd like to survey the community with. 1.)What kinds of functionality do you implement in your code that you expose to your scripts? Do you prefer to do higher level things like person.run(destination), or should the entire functionality of the scene graph and renderer be available to the scripter? 2.)If you do expose a lot of the functionality of your framework, how might one do it dynamically? I don't want to type up a glue function for every single method of every manager. 3.)How does one tell a particular object in a scene graph tree to do something? Previously, in my all C/C++ project, I'd simply pass pointers; I know a little about Lua's userdata type, but for some reason giving scripters pointers to memory just gives me the void* jitters from my C days. This is the first time I've actually implemented a scripting language in any non-trivial way, so let me know if I'm missing something obvious! [Edited by - templewulf on April 28, 2006 2:26:53 PM]

Share this post


Link to post
Share on other sites
This is how my game does this:

1) The C++ engine owns all objects in the game and scene graph, draws everything and runs collisions and rigid bodies. You can attach small "Lua-tag" objects to game objects in the scene graph. One of these Lua scrips can send messages and query some information of game objects and get a small number of notification-messages from the engine. Example messages are OnButton, OnCollision, OnCustomMessage. When your script posts a message you can post with a time-stamp that makes it be delivered in the future. This lets you post a message to your own Lua script to get regular messages. The Lua scripts should _not_ be called every render frame or internal time-step, only when something more rare happens (button presses, collisions).

2) Messages.

3) I use a unique 32-bit ID for every game object so the Lua script (and the messages) can refer refer to game objects in a safe way. You _could_ use lua's lightdata type for sending around pointers in your Lua scripts. Lua cannot access _through_ these pointers, so your code is safe. You need to verify each pointer that the Lua code passes to C++, which makes it just as easy to use the ID method. Remember that the object you refer to might have been deleted when you try to access it.


HTH


/Marcus

Share this post


Link to post
Share on other sites
So, do you have a function in Lua of something like this?
int PostMessage(lua_State* L)
{
int msgID = lua_gettop(L);
msgQueue.push_back(CMessageFactory::Make(msgID));
}


What do you do when you want to send a message back to Lua? Would you push function parameters onto the Lua stack and then do a lua_call("MsgProc") on a lua function that processes messages like the WndProc in the WINAPI?

Thanks for your advice, by the way; ratings++!

Share this post


Link to post
Share on other sites
Yes.

My PostMessage() has 4 arguments:

deliveryTime (milliseconds in world-time)
messageType (a string)
destinationGO (ID)
data (a lua table with additional data, contents depends on the message type)


While in C++, a message is stored in a TMessage struct and C++ code can handle these directly. If the message needs to be delivered to a Lua script, I call a function called "OnMessage" in my Lua script, passing the four parameters.


HTH


/Marcus

Share this post


Link to post
Share on other sites

This topic is 4246 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this