• Advertisement
Sign in to follow this  

Unity Exposing functionality to scripts

This topic is 4315 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
Advertisement
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
BTW: lua_gettop() in your example gets the number of arguments on the Lua stack - it doesn't get you the actual argument (message ID).


/Marcus

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By khawk
      Watch the latest from Unity.
       
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
  • Advertisement