Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  how to represent event data in event-driven communication?  (Read 585 times)
0 Members and 1 Guest are viewing this topic.
Offline tacticalPotato37

Senior Newbie





« Posted 2012-07-26 22:42:41 »

I'm trying to create a central event dispatching system using kind of an Observer pattern for my small RPG project, and I need advice on how best to represent event data.

I'll have an EventManager that registers listener classes for different event types defined in a static enum such as Event_Input_KeyPressed or Event_Game_ActorMoved. It will store events in a queue and dispatch them to the corresponding listeners, which will each have a method handleEvent(Event e) or something.

Each event will be an object of type Event, which holds data such as its event type.

My question is, since the nature of the event data differs considerably between event types, how should this data be represented? I'm not yet sure exactly what types of events I will want to create in the future, so I want this to be as flexible as possible. For example, an event could trigger other events if certain conditions are true (ie. when the player pressed the action key, a door opens if the player is in a certain location and has a key). Is XML or scripts a good choice? Another way I can think of is to create a custom class for every general event, like ActorEvent or MenuEvent, but that seems really inefficient and inflexible. Also, since some objects such as Character only need to know very specific events like when the "w" key is pressed, I figure they don't need to be notified when other keys like "h" are pressed. Is it viable to create event types that specific or is there a better way? ie. Event_Input_KeyPressed_W

Thanks  Smiley
Offline UprightPath
« Reply #1 - Posted 2012-07-26 22:50:47 »

Not sure I can give you some far reaching advise about this, however I can at least give two cents worth.

Separate your UI from your Logic. Your game's logic should not have any idea what a 'W' key is. Some other mechanism should be in charge of accepting your keyboard/mouse/orangutan-plug-and-play-event and then generating an event based off of that, which it puts on the logic event queue. The primary reason for this... Well, not everyone (Here especially) uses a qwerty keyboard so what seem like perfectly good ideas on that keyboard arrangement are utter rubbish to them (Like using Z, X, C for three button controls. Makes sense on a Qwerty, but...). This also means that you can cut down on the events that your system needs to know how to process (If you have three different ways to move your character-- WASD, Arrows, Num-Pad-- your logic shouldn't know that and they should all be treated as the same by your game.)

Beyond that: Build with extension in mind, but don't let making it extensible prevent you from getting it done. Figure out your base events and attempt to get a system that works off those, then try to get one that'll respond to others.

Offline actual

JGO Coder


Medals: 24



« Reply #2 - Posted 2012-07-26 23:26:24 »

I use the one class per message approach and I have not found it to be a problem. Each class is small, only containing data specific to that packet. You have access to the source code and re-compiling isn't a big deal, usually when you are adding new messages, you are doing other things anyhow.

While you could have a EventKeyWPressedMessage, it would be better to have a layer in between your input and the game entities reacting. So you have some input manager that listens for key presses. If the user presses "w", it fires off a MoveLeftMessage or MoveMessage with direction as a parameter.



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tacticalPotato37

Senior Newbie





« Reply #3 - Posted 2012-07-27 01:18:02 »

Thanks for the input guys.
I think I'll just go with one class per event first until any problems come up. The only concerns I have with this are clutter and that there isn't really any way to give events certain conditions or other effects, but I guess that stuff can easily be dealt with wherever the events are created and received.

You're right about separating UI from logic, that's actually why I'm making an event management system, so thanks for saving me from being stupid haha.
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.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!