• Advertisement
Sign in to follow this  

Unity [java] event listeners, etc.

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

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 this post

Link to post
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 this post

Link to post
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 ) {

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]

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
      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
      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