Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  EventBus  (Read 1032 times)
0 Members and 1 Guest are viewing this topic.
Offline AbstractChaos

Senior Newbie


Medals: 2



« Posted 2011-12-31 22:15:28 »

Hey guys me again,
  I have been having some issues with maintaining my slick2D game in regards to notifying other areas of code something has happened. My solution was to create EventBus essentially a very simple version of the GWT version (I work with it at work so to me its second nature).

The code looks as follows:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
package uk.co.digitaldragonstudios.engine.events;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventBus {
   private static EventBus eventBus;
   private Map<EventType,List<Listener<Event>>> bus =
      new HashMap<EventType,List<Listener<Event>>>();
   
   public static EventBus get() {
      if(eventBus == null) {
         eventBus = new EventBus();
      }
      return eventBus;
   }
   
   public void addListener(EventType type,Listener<Event> event) {
      if(bus.get(type) != null) {
         bus.get(type).add(event);
      }
      else {
         List<Listener<Event>> listeners = new ArrayList<Listener<Event>>();
         listeners.add(event);
         
         bus.put(type, listeners);
      }
   }
   
   public boolean fireEvent(EventType type,Event event) {
      if(bus.get(type) != null) {
         List<Listener<Event>> listeners = bus.get(type);
         for(Listener<Event> listener : listeners) {
            listener.onEvent(event);
           
            if(event.isCancelled()) {
               return false;
            }
         }
         return true;
      }
      return false;
   }
}


My questions are, is there a better way to do it than this and should it be done in games?

i understand it would never work for Java 4k of course Wink
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-12-31 22:27:51 »

Your current implementation makes it impossible to add a listener of type X while you are handling an event of type X, due to the resulting ConcurrentModificationException.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline AbstractChaos

Senior Newbie


Medals: 2



« Reply #2 - Posted 2011-12-31 22:30:32 »

Yes true but in most cases you would register an event long before your actually gonna fire any... since object creation need be avoided in the update loop.

p.s Thanks for the quick reply as always riven Smiley

EDIT:
although thinking about it firing an event involves creating one in the first place, hmm.

Also does it mean it worth doing it this way (after i solve the concurrency etc out)?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline AbstractChaos

Senior Newbie


Medals: 2



« Reply #3 - Posted 2011-12-31 23:28:26 »

So I have been looking at this and one way in which it could be done was to use a "ConcurrentSkipListSet<E>" instead if a list which if I understand your point Riven would be solved as there is no longer a list that can throw the problem. Another idea I found was using annotations to signify a event handler this removes the need for Anonymous Inner Classes which apparently the GC and has caused memory leaks and so long as I ensure i keep track of the method and not keep looking up shouldn't slow anything down.

Also both the set and use of reflection with annotations is supported in Andriod and actually suggested.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-01-01 00:28:10 »

You need the ability to remove a listener (during the event, too).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DrZoidberg

Senior Member


Medals: 15



« Reply #5 - Posted 2012-01-02 08:11:33 »

You could run all the event handling code on the AWT event thread.
e.g.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void addListener(final EventType type,final Listener<Event> event) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            if(bus.get(type) != null) {
              bus.get(type).add(event);
            }
            else {
              List<Listener<Event>> listeners = new ArrayList<Listener<Event>>();
              listeners.add(event);
             
              bus.put(type, listeners);
            }
        }
    }
}

You do the same with fireEvent and removeListener. You could also make all methods synchronized but that could result in a deadlock if you try to add an event inside an event listener.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!