Hi community,
I would like to get a good understanding of the basics -> communication between objects, trigger actions.
If I understand well this would have to do with event listeners, but I can't see how to structure the logic with efficiency.
Here is the situation :
Let's say I have a moving object that contains a property (ex : "enemy").
When enemy gets closer to a friend (object with "friend" property), I want to send a message to other "friends" objects to notify them.
Then a message is sent to the HUD to display a "Under fire" mode (let's say, a red light sign showing up in the corner).
I am not asking for precise code, but how would you handle that scenario? How would you structure the listeners/triggers?
Are there some things to know?
I am not asking for the community to do the work for me, but I don't want to reinvent the wheel if there are best practices that "every developer" should know about
Thank you!
Comments
For example:
The best way to do this is by events and listeners. Whenever you want to notify some class about a change or condition, (in this example enemy gets closer), you create a specific Event about it and dispatch that event. You also add this events listener to the objects that you want to notify about this event. So whenever the events is dispatch, the listeners know it happened and do the action.
Say you have a MovingObject class:
I hope this helps.
Cheers,
If you have a Game class, which handles enemies and friends, you can let it catch the event. It knows who are friends and notifies them.
https://sites.google.com/site/xraystudiogame
I have built some tools that help me achieve what I had in mind in a very convenient way, thanks to your help
@alexzheng
I am not sure I understand what you mean by broadcast events?
I have created a class (ex: Game like suggested by Deniz) that handles the attribution of triggers, listeners and actions to groups of objects. When a trigger sends an event, the listeners get automatically activated and an action is performed.
The Game class stores and pairs groups of objects.
Ex : when a moving lamp with a "light" property sends an event ("close"), a wall that has the same "light" property gets the message and its color changes.
Those triggers and listeners are grouped by "light" property and this allows me to loop and check distance between all objects contained.
Is it much different than what you are suggesting, Alex?
Can you tell me why isn't the following displaying "Enemy is close"?
If I understand well in your example self.image is telling to itself "enemy is close" right?
But how would "enemy" send a message to "friend" -> "I'm close, fear me!"
And "friend" would be listening, get the message, and react -> "We are under attack!"
I would like the sender and the listener to be different objects.
In fact I'm working on some tests with moving lights.
When the light is close to the wall object, "light" tells "wall" to light up. Wall changes color.
When the light is going away, the light on the wall reverts to normal.
My goal is to have a lot of different lights and walls.
Any idea how I should tackle this? I have tried without success..
1. How do you determine proximity? if you want some pointers on that, read up on Levenshtein Distance.
2. Loop through a list of all the objects and determine the Levenshtein Distance of each of the objects in reference to the wall.
3. Check if the object that does have the allowable distance (proximity) is a friend or an enemy, and based on that act accordingly.
Hope that helps you to proceed. In case you have different walls, run this for each wall to determine the distance and the lights that fit the criteria.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
1. How do you determine proximity?
Like this, it seems to work :
This is what I have and I am able to loop, check the allowable distance, check if friend/enemy ("light" and "walls" in the following example), but the last part (act accordingly) is not working.
So I believe the distance check is ok.
But I can't send the message to wallx so wallx runs lightUp.
The part that is not working is "send message -> get message, run function".
So that's why I was asking for a very simple example of working send/listen message between two different objects so I can adapt it to my own code (that would be two long to post here).