Why I Can Press 2 or more key in same time ??

Godot Version

3.5.3 stable

Question

Hello.
I Created a Simple game in godot (OS : Windows)
In this game, which is a volleyball game, there are two players, both of whom use only one PC and keyboard.
There are three buttons for each player: right, left and jump (up).
The first player uses the Rightwards Arrow, Leftwards Arrow and Up Arrow keys, i.e. “ui right”, “ui left”, “ui up”
The second player also uses W, A and D keys.
But the problem is that you cannot press two keys at the same time and both characters move at the same time.
The point is that my keyboard has anti-ghost and I can press many buttons at the same time in another game.

Here I put the codes of one of the players.
Player1.gd:

extends KinematicBody2D

var velocity = Vector2(0,0)
var jumping = false

func _ready():
	#if velocity.y != 450 :
		#velocity.y = 450
	$AnimatedSprite.play("default")

func _input(event):
	velocity = Vector2()
	$AnimatedSprite.play("default")
	if event.is_action_pressed('ui_right'):
		velocity.x += 250
		$AnimatedSprite.play("run")
	if event.is_action_pressed('ui_left'):
		$AnimatedSprite.play("run")
		velocity.x -= 250
	if event.is_action_pressed('ui_up') and not jumping:
		velocity.y -= 800
		jumping = true
		$AnimatedSprite.play("hand")
	elif velocity.y < 370:
		velocity.y += 2400
		jumping = false
func _physics_process(delta):
	move_and_slide(velocity)

_input is called for lots of input events, beyond the buttons you’re checking for (including keys being released), but you reset the velocity every time regardless of the event received.

If you’ve got the same logic on both players _input will be called on both every time one of their keys is pressed, so moving Player 1 left will immediately reset Player 2’s velocity.

I would guard everything on the event being one of the actions you’re after, before even resetting the velocity and checking the individual buttons.

2 Likes

Thanks !

What should I do to fix this problem?

Edit : I mean, please write a piece of code for this part or explain more. Thank You so much!

func _ready():
	#if velocity.y != 450 :
		#velocity.y = 450
	$AnimatedSprite.play("default")

var FreeToPress : bool = true
var SwitchBack:bool = false
func _input(event):
	velocity = Vector2()
	$AnimatedSprite.play("default")
	if not FreeToPress:
		SwitchBack=true
	if event.is_action_pressed('ui_right') and FreeToPress:
		velocity.x += 250
		$AnimatedSprite.play("run")
		FreeToPress = false
	if event.is_action_pressed('ui_left') and FreeToPress:
		$AnimatedSprite.play("run")
		velocity.x -= 250
		FreeToPress = false
	if event.is_action_pressed('ui_up') and not jumping and FreeToPress:
		velocity.y -= 800
		jumping = true
		$AnimatedSprite.play("hand")
		FreeToPress = false
	elif velocity.y < 370:
		velocity.y += 2400
		jumping = false
	if SwitchBack:
		FreeToPress = true
		SwitchBack = false

Thank you but now i can’t move characters

I bet it’s reading it as
if event.is_action_pressed('ui_up') and not (jumping and FreeToPress):

my bad try this
if event.is_action_pressed('ui_up') and (not jumping) and FreeToPress:

Thank You so much

I Tried and it’s not working
animations are playing correctly but characters can’t move (jump , left , right)

And one thing:
I can see the animations and with these changes, two animations are still not running at the same time, which shows that the previous problem is still not solved.

I’m sorry I forgot to say earlier that the animations are running correctly. Running the animations shows that the part
if event.is_action_pressed('up') and (not jumping) and FreeToPress:
is not problem.

Hey Friend !
Can You help me?

so how are you running when it’s player 1 turn to use the keyboard and not player 2. could you show us more of the project on github or something. are you using a Tutorial if so could you give the link so we can see/run more of the project. is their a reason for 3.5 godot.

hello dear friend I apologize for the delay. I haven’t been online for a while because of some issues… I’m really sorry

Yes, I use a tutorial, but unfortunately, the tutorial is not in English and the tutorial is old and is no longer available on the internet, so I can’t give you the tutorial link.
I was using Godot 3.5 because of that tutorial and I wrote this project a long time ago

This game is simple and similar to volleyball game and we have a character for each player. Two players must be able to use the keys at the same time to be able to move on the field and hit the ball. In this game, I did not work much with the details of the hand and the body, and it is enough for the ball to hit the character to hit the ball.

If I change the version of the project, I get errors. But they can be solved. Is it easier for You if I change the version?