Having trouble coding interactions (beginner-intermediate help needed)

Godot Version

extends Node2D

var tree_state = “apple”
var player_in_area = false

func _process(delta):

if tree_state == "no-apple":
if tree_state == "apple":
	if player_in_area == true:
		if Input.is_action_just_pressed("e"):
			tree_state == "no-apple"

func _on_pickable_area_body_entered(body):
if body.has_method(“player”):
player_in_area = true

func _on_pickable_area_body_exited(body):
if body.has_method(“player”):
player_in_area = false


I am trying to make a mechanic where the tree image changes from a tree with apples to without apples with my player is inside a “collisionshape2D area” and presses “e”. I tried to do it on my own but was unsuccessful so I opted to follow a yt tutorial that i got the idea from 1 for 1 but it still doesnt work. Note - i do have “func player():” on my player script.

(Firstly, you can wrap your pasted code here in triple backticks ( ``` ) to format it more nicely, it’s hard to read the indentation as-is.)

I’m going to make a guess here:

You are using Input.is_action_just_pressed("e"), which usually requires an input action to be defined in Project → Project Settings → Input Map.

However, I’m guessing that you do not have an input action named "e", and you actually just want to detect when the actual E key is pressed.

If this is the case, you have a couple of options:

  1. Make a proper input action in Project → Project Settings → Input Map. I would name it something like "pick_apple", and then you can use Input.is_action_just_pressed("pick_apple").
  2. Or, you could instead switch to using Input.is_physical_key_pressed(KEY_E). Note that this doesn’t have the “just pressed” behavior, and just determines if the key is currently pressed.

Hi, so I changed the code to
" if Input.is_physical_key_pressed(KEY_E): tree_state == "no-apple" $AnimatedSprite2D.play("no-apple") if player_in_area == false: tree_state == “apple”

And the apples do dissapear, however the apples reappear right after I let go of the e key. Any advice??

It looks like you are trying to change the tree_state like this:

tree_state == "no-apple"

But you’ve accidentally used two equals ( == ) instead of one ( = ).

Actually, if you do have an "e" input action mapped, this could have been the original cause of the error. I didn’t catch it when I first looked at your code.

You should have gotten the “Standalone expression” warning for this. Make sure to always check your warnings, and you can change specific warnings to become errors in your Project Settings (search for gdscript).

Hey, ive fully fixed the issue by swapping from == to = thank you so much for the help i really appreciate it.

1 Like

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