Key binding not working (Godot 4.3.stable)

Hi there! I’m new to game design and I’ve been following this tutorial here.

I’m specifically on the section “Player 2.0” which talks about key binding and animation flipping. I was able to successfully follow everything until I got to this section, and as far as I’m aware everything matches up to what the tutorial is telling me to do. Both the key binding and the flipping isn’t working for me, but I’d like to tackle one problem at a time.

This is what the finished code looks like for key binding in the video under the player scene:

This is what my code looks like:

It should be noted that when I first started play testing the prototype, I was able to use the arrow keys and space bar by default. I went into the project settings tab like so:

I feel like I followed this step by step and I recreated the code twice and made sure everything matched how it should. I can still use the arrow keys but I cannot use the newly bind keys.

I’m sure it’s a user error of some sort, but I’d appreciate any help, thanks!

Checking the images I couldn’t see your error at first glance, but you could try to debug your code to see if it’s working as intended.

There are two ways that I’m going to explain:

The first one is making breaks on the code, if you put the mouse on the left of the line numbers of the script, you will see that it pops a red circle :red_circle:, if you click it, when the game runs that line it will pause it, letting you run the next lines one by one or continue. In this mode you can even check the value of some variables.

Another way would be printing on the console, for example, writing print("Direction:" + str(direction)) would print the direction variable on the console so you can check if it’s running that part of the code, and check the variables that you write inside the print.

And don’t forget to check the error console to see if something is wrong.

Good Luck!

1 Like

So put this code in your ready for that scene. This will print true if your inputmap has the event (e.g. physical D) in the action move_right). Let us know what you find.

func _ready():
	var evt=InputEventKey.new()
	evt.physical_keycode=KEY_D
	print(InputMap.action_has_event("ui_right",evt))
	print(InputMap.action_has_event("move_right",evt))

P.S. this works for me with both actions. Also is there a chance you have D in some other action.

1 Like

I see a few issues with your code.

First, gravity works differently depending on the version of Godot you are using. When posting a problem, you should always post the version of Godot you are using, even if it doesn’t seem important.

Whichever version you are using, your gravity calculation isn’t working. He has this line:

	if not is_on_floor():
		velocity += gravity * delta

You have this line:

	if not is_on_floor():
		velocity += get_gravity * delta

In older versions of Godot, you have to define gravity by pulling it from settings. By 4.3, there is a function called get_gravity() that does this for us. However what you have done is reference it as a variable instead of a function by deleting the () at the end.

Change your code to:

	if not is_on_floor():
		velocity += get_gravity() * delta

And then delete line 6 declaring the gravity variable. You don’t need it.

Second, you are editing the wrong script.

I figured this out because the code you have above would be throwing an error if you were using it, and you said the keys were working both before and after you wrote that code. This means you were using the boilerplate code which maps jump to ui_select and moving left and right to ui_left and ui_right. If something is working and you didn’t write the code, look for the code you didn’t write.

Take a look at your project. You have two versions of player.gd. One is in your root folder, and one is in your Scripts folder. Your player is attached to the one in the Scripts folder, which is using the boilerplate code. The code you are showing us in your screenshot is not attached to anything, and so is not doing anything.

The solution is to attach the script you wrote to your player. Problem solved.

2 Likes

Thank you so much! I went ahead and edited the title for clarity’s sake. This makes so much sense and fixes both of the problems I had. I’ve was stuck on this for a few days and I’m glad I’m able to actually move forward with this project. :smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.