ENIGMA Forums

Outsourcing saves money => Programming Help => Topic started by: marcelo on August 27, 2018, 12:03:42 am

Title: Implementing WASD Movement
Post by: marcelo on August 27, 2018, 12:03:42 am
Hi there,

I'm working on simple object that responds to WASD input by moving. The only issue I have is that after a couple seconds of holding down a key, the movement and keystrokes start to lag - I've included a GIF to show what I mean. I've tried both code and blocks, and both of them yield the same output.

Any suggestions would be greatly appreciated, thanks

Creation code:
Code: [Select]
left_speed = 0
right_speed = 0
up_speed = 0
down_speed = 0

Step code:
Code: [Select]
if keyboard_check(ord('W')) {
up_speed = 5
}
else {
up_speed = 0
}

if keyboard_check(ord('A')) {
left_speed = 5
}
else {
left_speed = 0
}

if keyboard_check(ord('S')) {
down_speed = 5
}
else {
down_speed = 0
}

if keyboard_check(ord('D')) {
right_speed = 5
}
else {
right_speed = 0
}

x -= left_speed
x += right_speed
y += down_speed
y -= up_speed
Title: Re: Implementing WASD Movement
Post by: HitCoder on August 27, 2018, 07:38:05 am
From what I can tell this shouldn't be happening... :/
your code appears fine and i can't think what could be causing this, i'll have a look though

EDIT: Ok so it can't be a problem with your code... since I just tried it and everything works fine;
https://drive.google.com/file/d/1vLEruwIe3sshcvpoRoZoG6g2o1lDvtxs/view?usp=sharing
Title: Re: Implementing WASD Movement
Post by: hpg678 on August 27, 2018, 08:41:29 am
i talked to one of the developers and he, as well as I, did get the same response at intervals. Rest assured he will look into as soon as possible. I've found that for me when I include a background, there is no lag, so u may want to do that as a temporary measure.
Title: Re: Implementing WASD Movement
Post by: marcelo on August 27, 2018, 10:03:58 pm
Thank you for trying out my code.

I added a background but I still experienced the same lag.. Since it worked for HitCoder I'm thinking maybe I can get it to work by running the code on a Windows installation of Enigma.
Title: Re: Implementing WASD Movement
Post by: hpg678 on August 28, 2018, 04:15:08 pm
I came across an article which in my opinion is one of the best articles I have ever read pertaining to game development. i believe it can help you with your problem, on so many other levels that may occur in your game, or rather future games.


https://www.yoyogames.com/blog/432/buttery-smooth-tech-tips-movement (https://www.yoyogames.com/blog/432/buttery-smooth-tech-tips-movement)


I did a sample test and the results were much more than what I expected.



Title: Re: Implementing WASD Movement
Post by: HitCoder on August 29, 2018, 06:02:05 pm
running the code on a Windows installation of Enigma.

What OS were you attempting to run this code on prior?

EDIT: After running the same code on Manjaro Linux, i'm noticing the problem, very prominently. I'll mess around with some things for a moment and get back to you.

EDIT2: In the game settings, I went to ENIGMA>API and set Platform from Linux X11 to SDL and it fixed things. I could hazard a guess that maybe in X11 mode when you hold a key down it registers the keystroke inputs in the same way a text editor would, in the way that holding w down will tell the game it's held down, then the window manager spams that key on and off like when you hold the letter down in a text form.
SDL seems to work fine on Linux, and tbh it's probably best to stick to cross-platform things anyway if they work. That's my personal belief though.
Title: Re: Implementing WASD Movement
Post by: marcelo on August 29, 2018, 07:56:02 pm
 :o :o :o

After installing SDL and using it as the platform, the game runs flawlessly on Debian Linux. That makes a lot of sense now, that X11 was treating the keystrokes akin to a text editor. Thank you so much for your help, I appreciate it a lot.

Also thank you hpg678 for the article, it's really interesting. I had never thought of the concept of time-based movement... definitely something I'll try to implement.
Title: Re: Implementing WASD Movement
Post by: jbskaggs on August 30, 2018, 08:21:47 am
And this is why we need these things asked in forum not just on discord, as other people like myself can find these and learn from them.
Title: Re: Implementing WASD Movement
Post by: Goombert on September 17, 2018, 09:41:50 pm
Hey guys, sorry for my late response here. I finally got around to this issue not only because I keep getting more reports about it but I also continue to have the issue myself, so it was getting really annoying. Regardless, I've got good news and we do have a fix!

As you can see on the GitHub issue posted oh-so-kindly by HitCoder, I first documented my findings while looking for the issue.
https://github.com/enigma-dev/enigma-dev/issues/1375

I tracked the cause down to the pull request in which we generalized the main loop so we could add SDL support.
https://github.com/enigma-dev/enigma-dev/pull/1259

The solution was pretty simple, I just had to tweak the event handling slightly to make it correct again.
https://github.com/enigma-dev/enigma-dev/pull/1390

I am hoping to get that reviewed and we'll have it merged into the repo soon. Thanks for your patience!