• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Brandon Marx
      Hello forum,
      I have some decent amount of experience in Unity making games for Software Engineering projects in college, these were very specific projects however and I still am fairly new to building games. I wanted to make a game that uses the shadows of objects for collision detecting (i.e. shooting a gun at a characters shadow causes that character damage. What is the best engine to do this in (game will be 3D), and does anyone have any advice on how to approach this concept? I consider myself fairly experienced in programming, but game dev is just an entirely different beast.
    • By juicyz
      Hey all,
      I've been slowly working on my game called AotW for a while now.  I have come to the conclusions that it would be nice to cooperate with 1 or 2 others to help finish it.  Ive been trying to keep my GDD up to date with my ideas and development so that would give a better overview of the game when the time comes.  Currently I have a basic skeleton of the RPG elements needed but everything can still be discussed and talked about and we can transform my idea to something the group likes.
      The premise of the game is a Diablo-like procedurally generated map with RPG elements that include sockets, inventory, classes, abilities, crafting, loot, items, sockets, and enchanting.  This will be done in a 2D iso view as I can't do 3D art and I enjoy 2D games a lot.
      I don't plan on releasing this as this is more of a hobby project for me and I have a full-time job.  Though I'd like to start putting more hours into development and having others definitely will be motivation.  I also want to be able to say I have finally "finished" a game idea to some degree.  If the time comes and we want to release it, then we can go ahead and do so but that's not my purpose or plan. 
    • By Arnold // Golden Donkey Productions
      Hi, I've been working on this issue for a while and haven't yet found an answer.
      Does anyone know the best way to convert unity's LAT & LONG into a vector 3 position that I could use in a virtual world (if it's even possible). 
      Thankyou in advance
    • By wobbegong_
      I am taking an absolute beginner's game development course and we have just finished game jams in small groups. Our current assignment is to get feedback from people working in any aspect of game development. I would very much appreciate any feedback! The game is up on itchi.io (sound warning) https://wobbegong.itch.io/zombie-shooter It's essentially a very basic PvE.
      I also have some things I'm wondering about, but you don't necessarily have to answer these. 
      1. Do you have any tips on working with physics? My group wrestled a bit with Rigidbody physics not totally working the way we wanted to -- jumping ended up kind of floaty and inclines seem to mess up movement. Alternatively... how can I build terrains with depth that won't result in wonky physics?
      2. How can I keep up the level of challenge in an interesting way as the player progresses through the waves?
      3. What are some of your personal guidelines for creating title screens?
      Thank you very much in advance!
    • By ethancodes
      I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console. 
      This is my current code for this part of the program, it is attached to the bricks:
      void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags. 
      Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Event-Listener with Lua and C++

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

Hello gamedev.net community!


I have a question about implementing a proper event listener. It shall be located in C++ but shall also get new event-listenings via Lua.


An event would be for example:

- Player walks into a set collision area.

- Player inputs a-button and is in reach of object x.

- Variable y inside the JSON-file reaches value 5.

and so on...


First of all, there are multiple level.lua-files that have a momentum where they describe those callback events.


I would like to store these event triggers in the C++-part of my engine, to avoid running Lua every iteration.

I thought about using an array of functions, storing functions that check if event conditions are met.

For example Lua calls a C++-function and passes arguments to it. The C++-function would then read out the arguements.

One arguement would determine what kind of function(-template) shall be selected and passes the other variables to it. These would be like which button to be pressed or the coordinates of that collision field.


An example on the Lua-side:

register_on_hit("name of this callback", x-coordinate, y-coordinate)

And somewhere in the map-file would be a function with the same name as the registered callback. This will be called by C++ once the function inside the function array with the id of "name of this callback" returns true.


Once all callbacks would be registered in the C++-array, the map-file closes and will be opened again when C++ realises that a function has been called.


When a new map-file will be loaded, the array will be cleaned of all registered potential events (except not map dependant events).


Is this a proper way of having an event listener? Are function arrays not recommended? Is there something I should do differently?

Edited by Angelic Ice

Share this post

Link to post
Share on other sites

If your event functions are written in lua, you can simply pass the function itself rather than its name. That saves you finding a function by name in the scope.

local foo = function(position)

register_func(foo, x, y)  -- not "foo"

If you also expose the trigger reasons to lua, you could have it create c++ trigger reasons, that it passes along in a function like the above.

Eg make a lua function that returns a "new TriggerReason;" object, and have lua pass that as part of the "regster_func" call.

Share this post

Link to post
Share on other sites

If I am reading this right, you want listeners in Lua. But your Event handling in C++?

Unless you don't mind undeterministic behavior, the best way I can think of doing this is by using a Message Board setup.

Basically you have C++ hold off onto writing events back to Lua until the time that it can receive them comes to an end. Once this is done, you write the events back to Lua into a double buffered global array. This will be our "message board".

Each listener will just iterate through the message board, looking for events it cares about. The listener will ignore data it doesn't care about, and act on data it does care about. This data will not be removed however, as other things may wish to act on the same message.

Only at the end of the end of the frame will the read message board be deleted, and swapped for the new one, which will in turn be processed on the update.


Note though, this is not the system you want to be using if events are to be exclusive to entities.

Example being a player fires a bullet into a monster. The bullet makes contact to the monster. At that instance, the bullet has a direct passage to reference the monster. It gives the monster a message immediately, then dies. On the monster's update the message will be processed from his personal mailbox, and exclusively to him.



Edit: If you don't want messages to be scattered around the heap, you can build a struct BLOB of with a book keepign header. Then a buffer of chars that you write data into with placement new, then cast them back. This will work when it comes to networking as well.

Edited by Tangletail

Share this post

Link to post
Share on other sites

Alberth, how would I receive that function as a value in C++? Does it has to do with the lua_registryindex?


Tangletail, Lua shall describe what kind of triggers should be implemented and also execute once the requirements are met.

If a requirement is met shall be tested in the C++.

Does this align with what you describe?

Share this post

Link to post
Share on other sites

Not quite. In general experience, it's easier to do the event firing in C++, and have it fire off events to lua without giving two craps if anyone is listening to it, then let Lua do the the rest.

For example, C++ will fire events for collisions, triggers being entered, etc. It will build a simple struct containing some basic details of the triggering instance (who triggered it, against what it triggered). Lua will handle handle testing these events, and the implementation details.

Don't worry too much about the performance of Lua. Lua is -very- fast. The problem comes when you make too many callbacks between lua and C++. That's why you send the events all at a single time.

Edited by Tangletail

Share this post

Link to post
Share on other sites

Well, in my case the events would be fired in C++ but the spectrum of what C++ shall test would be determined by Lua.

On this way I can avoid calling Lua. Even when you say Lua is fast, is calling it every frame not rather a bad idea?


I can see that this way avoids those templateish functions inside C++. I would not have to predefine all possible variations but making a proper comprehensive struct.

It sounds more dynamic, especially when I just want to add new values to check.


According to what you described, I would fire information every frame, for example like player is x 3 y 2, player is x 4 y 2 ...

Or when the player collides with an NPC, presses a button and what not...?


Moreover when a player uses the interaction input key, the engine looks for close up objects/NPCs and would store information in a struct?

Or would I rather just tell the struct, that a button has been pressed and give all possible distances to every object? Any better way to provide these information?


After that that, it would be tested by Lua if a certain criteria is met by running through all the possible events being defined in the map-file by comparing them to the passed struct/table?

Share this post

Link to post
Share on other sites

Alberth, how would I receive that function as a value in C++? Does it has to do with the lua_registryindex?
I don't know the implementation details at C level, but no doubt it would be some lua object.


In Lua, functions are first class citizens, you can make a function, and pass it around as data just like plain integers.




Edit: stackoverflow seems to know: http://stackoverflow.com/questions/2688040/how-to-callback-a-lua-function-from-a-c-function

Share this post

Link to post
Share on other sites
Ah, thanks for linking me that. Already found something similar to it. Alberth, I would be curious about your opinion about having a "message board" of firing events as Tangletail described. Do you think it is a better implementation?

Share this post

Link to post
Share on other sites

In either case, you need to have some starting point to arrive into Lua from C++ ("fire off events to lua" also needs something known in Lua that you can call).


From that you have choices. You can keep lists of Lua points to call in C++, or you can manage a table with such points in Lua.

The latter is arguably simpler in Lua, as Lua is a higher level language.


As for performance, the general rule in my experience is that your guess is wrong. I don't do much optimization, but if I do, I always guess where the performance problem is, and so far, it consistently turned up at different and very unexpected spots instead after profiling. Not once did I guess correctly.

As such, your worries about performance are probably wrong too, until you can proof it.


I can think of a few options to get further from here. One option is to measure and test before you decide. The biggest problem with that imho is that you don't run the real application, so any timing results are tricky to interpret.


A second option is to go with the simplest solution first, and see how it runs. If' it's ok, you're done, otherwise profile and improve. The disadvantage of that is that you might need a few improvement iterations, which mean you will throw away some code. In this case, most of that code will be lua, I think, which is not very costly to create.


Other options are probably to build a few alternatives so you can compare things. More costly in time, but it might give better results or insight.

Share this post

Link to post
Share on other sites
Thanks for your comprehensive answer. I will look into both implementations and see if I can find something that would suit my needs the most. If there are more possibilities and any opinions about the mentioned ways, I would be really curious to hear about them.

Share this post

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

  • Advertisement