Issue with keypress 'e'

Godot Version

4.2.2

Question

So I’m new to Godot, and have been trucking at a project for about a week now. It’s all been going fine and dandy but I loaded my project up today and there was this error: anything with pressing the ‘e’ key didn’t work as intended.

The thing is, every other keypress works fine, like WASD movement controls for the player, space to open inventory, but if I want to do the simple act of hiding a sprite when I press ‘e’, it doesn’t even register the keypress (I tested it using print statements). What am I supposed to do now?

For more reference, here’s the pickable-object code that uses an ‘e’ keypress (called “select” here) to make a sprite not visible.

extends Node2D

var state = “exists” #there are two states, picked and exists:
#picked is for when we have the plant, exists is for when we don’t

var ripe = false #ripe, like ripe-for-the-picking, which is what this plant is

@export var item: InvItem #this makes it so that the inventory version and the original version are attachable
var player = null #declare the presence of the player

func _process(_delta):
if state == “picked”:
$Sprite2D.visible = false
if state == “exists”:
$Sprite2D.visible = true
if ripe:
if Input.is_action_just_pressed(“select”):
state = “picked”
player.collect(item)
print(“got it”) #test

func _on_pick_area_body_entered(body):
if body.has_method(“player”):
ripe = true
player = body

func _on_pick_area_body_exited(body):
if body.has_method(“player”):
ripe = false

Also, there are no errors in the Output or Debugger windows. From the outside, nothing appears wrong with the program when I try to run it. It’s only just the keypress.

Have you checked the InputMap in the project settings?
Project → ProjectSettings → InputMap

Yep, and I ran tests with it as well (it did not fix much). Thanks for the suggestion, though!

Update: I have found a solution! I’m still not really sure why it was being weird in the first place, but I changed this part of my original code, as well as putting the player into its own class (called Player):

func _on_pick_area_body_entered(body):
if body is Player:
ripe = true
player = body

func _on_pick_area_body_exited(body):
if body is Player:
ripe = false
player = null

Now all my keypresses work fine. At least my program is fixed! (for now) :sweat_smile:

1 Like

The reason that this code:

func _on_pick_area_body_entered(body):
if body.has_method(“player”):

seemed to be acting weird is because you were using body.has_method("player")

This is checking if the body that entered the area has a method called player, not if the body is the player.

Hope that helps explain why it wasn’t working.

2 Likes

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