# Unity [java] event listeners, etc.

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

## Recommended Posts

hey. I've been working on the program described here: http://www.gamedev.net/community/forums/topic.asp?topic_id=453645 (don't worry about reading it, it's just background info). I've reworked my code to implement event models, but I want to make sure I'm on the right track before I code too much further. I've scanned through alot of java tutorials (including the big one at Sun) and I can't find any mention of events (other than those directly tied to Swing). For example, I'm using custom events like TradeRouteEvent, etc. Now, I have interfaces for each type of listener (which also includes the 'firing mechanism' methods that send the event object to the listener)... each object that fires an event holds an array of listeners (typed as whatever 'listener' the object implements), which it cycles through. an example: (this is not directly pulled from my code, so it may have typos or whatever) <code> void fireEntityCreated() { for(int i = 0; i < noOfListeners; i++) { listenerArray.EntityCreatedListener(new EntityEvent(blah) ) ; } } </code> the problem I see with this approach is first, there will be ALOT of for-loops. (one for every possible type of event) also, there will be a lot of arrays (possibly with over-ridden types, knowing my program hierarchy), which I'm afraid will be very inefficient when taking into account how many events will be 'fired' during gameplay. I'm currently researching other containers (which is slow-going work), but I'm in the dark with regards to events, listeners, observer models, etc. am I on the right track? any tips or insults? lemme know, thanks

##### Share on other sites
From my experience, this is pretty much the norm. You might be able to do a Generic-based event delivery system, but that can be kinda difficult to design properly.
I would stick with what you have, as cumbersome as it seems. It's very straightforward and easy to maintain.
Another suggestion (which seems obvious, but is often overlooked) is to do very little in your event listeners as possible. Spawn off the work on another thread or something. You don't want to tie up your event delivery thread, otherwise things might become unresponsive.

##### Share on other sites
You're basically on the right track. The for-loops are not a big deal if you consider the alternative - each object looping over every event messsage generated this frame. I would recommend using ArrayList instead of plain old arrays since the number of listeners can be dynamic. You can also create event objects if you need to inform about the source - Java is generally considered to be very good at allocating and deallocating very small short-lived objects, so I would not worry about it until you know it to be a bottleneck.

ArrayList<TradeListener> tradeListeners = new ArrayList<TradeListener>();...TradeEvent e = new TradeEvent(this, 50, 100, whatever);for( TradeListener l : tradeListeners ) {    l.tradeHappened(e);}

Edit: If the event is not consumable and not modifiable (it should not be modifiable in my opinion), there is no reason to create a separate event for each listener, as you are doing right now.

Edit 2: Also, I do not agree with Antheus' suggestion about cross-manager access in the original thread you are reffing to. In my humble opinion, it's just a disguised global variable but with additional casts. It allows for such awkward situations as, say, your renderer getting a handle on your input system and calling its update loop. So with all that typing, while you do get somewhat loose coupling, you are still allowing access to every system from every other system, which spells disaster as far as trying to understand the architecture or track down bugs is concerned. This is not the case if your design requires you to pass around the various handles. You'd still want to centralize all the systems in the class that executes your game loop, but only pass down the systems that each component needs to do their work.

[Edited by - lightbringer on July 17, 2007 10:06:41 AM]

• 22
• 10
• 19
• 14
• 20
• ### Similar Content

• I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
• By Pixeye
I wrote an extension for unity inspector that allows to group/fold variables.
Available on github  , cheers!

• I'M interested in programming tools (For animation, UI, etc). Can anyone suggest me the resources where I can start learning or which technologies I need achive it.

Thanks,
Rakshit
• By Paszq
Group photo of some of the characters and creatures currently living in Arpago
• By Paszq
A bunch of chests full of sweet loot :)