Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (734)
Games in Android Showcase (222)
games submitted by our members
Games in WIP (811)
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  
  Where exactly does the input code go?  (Read 4473 times)
0 Members and 1 Guest are viewing this topic.
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Posted 2017-01-20 00:17:11 »

Hey guys, so I'm using basic key polling for the most part, and I just pass the key listener as an argument to whatever game state or game object needs it. So something like

 
1  
Player player = new Player(x, y, keyboard);


And then in player.update() it would be something like

1  
2  
3  
4  
keyboard.update();
if(keyboard.up) player.moveUp();
if(keyboard.down)player.moveDown();
...


So just wondering if this is the right way to do it, because people tell me I should separate input logic from game logic/ update loop, but I'm not really sure where it goes.

Living is good!
Offline philfrei
« Reply #1 - Posted 2017-01-20 05:42:01 »

Hmmm.

Do you have a class "Keyboard" that is implementing KeyListener?

If so, I'd be strongly tempted to make the class variables 'up' and 'down' static. Then you don't have to pass this class to every object that needs to consult keyboard state.

So, in player.update(), yes, reference those variables. As long as you never write to these variables from anywhere except the implementation of KeyPress, KeyRelease, etc., it seems to me they are ok as public variables. Or to be safe, only write a get() and no set() and make the variables private. It might be good to make the variables volatile, but this is where things get a little hazy for me.

This Keyboard class itself wouldn't have to be called by update(). The 'up' and 'down' (and 'left' and 'right' or whatever) variables are updated asynchronously, when the KeyListener implementation methods run (when the keys are pressed).


OK, just thought of an exception. There might be some sort of game state occurrence that triggers where you'd want the keys to do something different. Message that to the Keyboard class via settable variables in the class, and consult them as needed (might be either via a game-loop update cycle or when the KeyPress/Release methods execute).

music and music apps: http://adonax.com
Offline tariqbroadnax

Junior Devvie


Medals: 2
Exp: 3 years



« Reply #2 - Posted 2017-02-02 22:05:15 »

Think of it this way. You have your game world. It can do amazing things. But, it cannot read your mind. So what, you need is an "interface" between your game world and what you want to do. There is where your input code comes in. You know what else is an interface into your game world? Your actual Graphical User "Interface."

TL;DR: Wherever you put your GUI code, put your input code.

Make sure you properly synchronize everything. That is, you may want an Action Buffer that runs and flushes every loop cycle. Since you input thread and game thread are different threads, you may want to brush on java concurrency.

Do not be afraid to hard code your mouse input stuff. That stuff usually doesn't change. Its the keyboard input should be able to work on any key b/c you probably want to support rebinding.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Reply #3 - Posted 2017-02-07 22:29:22 »

Thanks a lot guys. Sorry for the extremely late reply; been having to prioritize school projects. I'll take a closer look at the thread when I find the time. Cheers!

Living is good!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
cybrmynd (37 views)
2017-08-02 12:28:51

cybrmynd (52 views)
2017-08-02 12:19:43

cybrmynd (62 views)
2017-08-02 12:18:09

Sralse (74 views)
2017-07-25 17:13:48

Archive (499 views)
2017-04-27 17:45:51

buddyBro (646 views)
2017-04-05 03:38:00

CopyableCougar4 (1132 views)
2017-03-24 15:39:42

theagentd (1135 views)
2017-03-24 15:32:08

Rule (1112 views)
2017-03-19 12:43:22

Rule (1089 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!