Space key not being detected during diagonal Up & Left movement

Defold Newbie here. I have a player-controlled ship GO with a script attached.

When I use the arrow keys to move the ship object and press space, the key triggers a laser shot, which is correct.

However the hash(“fire”) isn’t detected when I press Up and Left keys simultaneously, but it works for Down and Left, Up and Right, Down and Right.

function on_input(self, action_id, action)
	local p = go.get_position()
	-- Each sprite image is 36 pixels wide
	if action_id == hash("left") and p.x > 18 then
		self.dir.x = -1
	elseif action_id == hash("right") and p.x < (640 - 18) then
		self.dir.x = 1
	elseif action_id == hash("up") and p.y < 240 then
		self.dir.y = 1
	elseif action_id == hash("down") and p.y > 32 then
		self.dir.y = -1
	elseif action_id == hash("fire") and action.pressed then
		self.firing = true
	end
end

Any ideas why the fire button isn’t detected only when I press up and left together?
Thanks

it may be related to the number of key presses your keyboard can accept at the same time… try holding space and left and the pressing up. Is the Up key input accepted?

4 Likes

I used to run into this issue all the time back in the day when playing split screen games. I haven’t encountered it since, though whether it’s because modern keyboards don’t have this issue (could be it was limited to ps2 keyboards?) or because I haven’t played a split-screen game in ages, I cannot say.

The solution was always to change the bindings. I believe arrow keys were especially vulnerable to this.

3 Likes

Guys thanks for the help, really appreciate the feedback.

I tried remapping the input keys to the keypad direction keys instead, and it is working now, so it must be a feature with my keyboard and the normal cursor keys.

Regards

6 Likes

I’ve had this issue too with the curor keys. When I remapped to WASD there was no problem. Weird one.

1 Like

A common problem in desktop games.
It has to do with how matrix keyboards work when pressing multiple keys on a single circuit.
I used to have a better diagram, but this still kind of shows what I am referring to.

7 Likes

Isn’t the problem because you use “if…elseif…” ? If the first condition is valide then the others “elseif” are just ignored no ? if you replace

elseif action_id == hash("fire") and action.pressed then
	self.firing = true
end

with

if action_id == hash("fire") and action.pressed then
	self.firing = true
end

i think it should work

2 Likes

Each keypress sends a separate action. The hash can’t be simultaneously “fire” and “left”.

4 Likes

But it works when I remap the inputs to use the keypad Left and Up keys, I can move diagonally and fire at the same time, which is why I was confused :confused:

But you and @nathan68560 are both right - I should do the test for the fire key separately, thank you for the feedback.

1 Like

You misunderstood. I was pointing out that nathan68560’s solution wouldn’t work, because the function is called for each key press separately, and during each call only exactly one condition can be true, so that can’t be the problem.

1 Like

the function is called for each key press separately,

I’m pretty sure that is only true for things that happen in the update function. Inputs are like messages, each one is sent individually (right?)

1 Like

Not sure what you mean by this. Here is what happens:

  1. The user presses “up” and “space” during the same frame.
  2. Those keys are mapped to actions “move_up” and “shoot” in the input binding file.
  3. The engine goes through the list of components that has acquired input focus.
  4. Each script component’s on_input() function gets called, once for each action. So one call for “move_up” and one for “shoot”.
  5. Next frame, go to 1.

(More details are in Device input in Defold)

4 Likes

Yeah, that’s the one.

Booleans (or other properties) can only be one thing at a time (which is what kingklear was trying to say, i believe). But various inputs can be interpreted basically at the same time, just like how multiple messages can be received more or less simultaneously.

I expressed myself poorly because despite working as an English teacher, translator, and writer for literally 10 years now, I need to drink at least 2 strong coffees before I can correctly construct a sentence.

1 Like

I looked for more lightweight engine than Unity and decided to check Defold. Got this bug when tried to follow War battles tutorial.
So interesting, if fire bound to ‘space’ it doesn’t work on any ‘arrow keys’ combination except Up + Right, but if you bind fire to any other key, it’s work perfectly. Strange glich which has not been fixed in the last 2 years - very disappoint (

Can you share your project (email to bjorn@defold.se) in its current state so that I can take a look?

Yes, sent it. Thanks for fast response.

So it’s definitely hardware and OS related. The project you sent me works perfectly on macOS for instance. I’m going to try it on Windows this evening.

1 Like

It’s possible. I have the Windows 7.
Can you check the html5 version? I have same problem there War battles tutorial 0.1
Will check it under Ubuntu later.

Are your sure it’s not a key roll-over issue? Sounds like a key roll-over issue. As @decoded pointed out, most keyboards don’t support every combination of multiple simultaneous keys. They generally cap out at 3-4 keys not including the modifier keys, depending on the specific wiring of your keyboard.

Try changing your keyboard. Maybe yours is wired in a weird way.

5 Likes

I have no problems with that web build.

I have, in the past, used one or two other keyboards that can’t handle two arrow keys and space together though. Definitely sounds like key roll-over to me too.

My current keyboard can’t do Space if A and S or S and D are pressed, though it’s fine with A+W or D+W and Space.

You cat also try: https://keyboardchecker.com/

2 Likes