Patterns for input handling

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Patterns for input handling

Postby EdouardTimsit » Sun Jul 14, 2019 9:25 am

Hi,

I'm trying to make a simple RPG with java/libgdx. I'm a self-taught coder, so I tend to improvize and my code tends to be very messy when I get deep into a project. Last year at college I had a java course, in which they taught us to use programming patterns. They taught us 3 patterns : Model/View/Controler, Factory and Observer. It really changed the way I coded. For example, last summer, I applied the MVC pattern to my gameworld and its rendering. I didn't touch the code for a whole year, and now here I am, and it took me only an hour to read my code again and going back to work.

I feel like the more I learn about patterns, the more I use them, the better my code is going to be.

I'm now handling the input handling part, and I feel like my code is going improvized and messy again. The "if" are multiplying in my handleInput() method and it's becoming unreadable again. Here's what I need to do :
I have a MapState gameloop in my code. It's the part of the game where my hero is moving on the map, interacting with NPCs (Not Playable Character) and elements of the map, colliding with the environment, etc. At the begining, my handleInput() method was something like :
Code: Select all
if left key is pressed :
    check collision left and go there
if right key is pressed :
   ...
...

Then I added an NPC on my map. So I added:
Code: Select all
...
if action key is pressed:
    if there's an NPC front of the hero:
        execute that NPC's script
    if there's an interactable element of the map in front of the hero:
        execute that element's script

But when a NPC is talking to my hero, I don't want him to be able to move. So I added a boolean canMove to my MapState.
But when a NPC is talking to my hero, I want the actionKey to make the messages scroll, not to trigger interactions with the elements of the map and the same NPC! So I added a boolean inDialogue...

I could go on for ever. The point is : it's working, I don't expect a pattern to be less expensive in terms of complexity, but it's unreadable.

Do you think some pattern could help me here? I thought of the State pattern, that could replace all the booleans that I made. I'd be glad if you could share some experience though.

Thanks a lot for reading this looong message.
EdouardTimsit
 
Posts: 4
Joined: Tue Apr 19, 2016 2:34 pm

Re: Patterns for input handling

Postby QuiIIraven » Mon Jul 15, 2019 11:47 am

I don't know about others but I am normally using an "InputManager" class where any class can register/unregister as an "InputListener" (Observer Pattern).

That way your input handling logic (=InputManager) does not need to know which classes are interested in events. If they are, they are registered as listeners and get notified.

For your second problem I would suggest to add a priority to the listeners (e.g. PriorityQueue) so that you can control which class retrieves input events first. Your "onInputEvent..." methods should then return a boolean.
If a listener returns true then the event will not be forwarded to the remaining listeners in the queue.

I think this is added in a similar way to gdx-ai and their StateListeners (or is it called Telegraph or something?).

Hope that helps!
QuiIIraven
 
Posts: 68
Joined: Sat Jun 02, 2018 4:59 pm

Re: Patterns for input handling

Postby EdouardTimsit » Tue Jul 16, 2019 12:14 pm

Thanks for your answer. It helps a lot, since I went for a totally different approach that ended up being messy also... I'll study this InputListener concept
EdouardTimsit
 
Posts: 4
Joined: Tue Apr 19, 2016 2:34 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest