+1 vote

I'm developing a relatively simple top-down game, and I'm trying to make it so that the player can only perform a certain action after the player sprite has entered a specific area. For this I've set up an Area2D that successfully recognizes when the player enters the area (and exits the area), but I can't figure out to then after that have the action work only when the player is in that area. I've tried using if statements that recognize when the appropriate button (space) has been pushed in the signal function. I tried a boolean that changes to "true" when the player enters the area and then calling it later in the fixedprocess(delta) code, but for some reason the variable only reads as "false" even after the player has entered the area. (I've checked within the area enter function that the variable does in fact change to true, and it reads as true within that function.) I'm not quite sure what else to do. I feel like I'm just not understanding how functions work properly but I'm afraid I don't know enough to know what I don't know.

Here's how I've organized my nodes:
scene tree

The "Player Area" node is a superfluous node I forgot to delete before I took the picture, and there isn't actually any useful script attached to "EntranceBu...", which is the Area2D node that is successfully detecting the player's entrance and exit.

The area enter code:

 #this part works
    func _on_EntranceButton_body_enter( body ):
        print("Area was entered!")
        var buttonarea = "true"
        #but this part does not
        if (Input.is_action_pressed("space_press")):
            print("Button was pressed!")

The code in the fixedprocess(delta) function:

func _fixed_process(delta):
    var direction = Vector2()
    var velocity = Vector2()

    #player movement (which works, I'm leaving it just in case it's interfering somehow)
    if (Input.is_action_pressed("move_up")):
        direction.y = -1
        Playerdirec = "Up"
    elif (Input.is_action_pressed("move_down")):
        direction.y = 1
        Playerdirec = "Down"
    if (Input.is_action_pressed("move_right")):
        direction.x = 1
        Playerdirec = "Right"
    elif (Input.is_action_pressed("move_left")):
        direction.x = -1
        Playerdirec = "Left"

    #velocity for player movement (which also works)
    velocity = MAX_SPEED * direction.normalized() * delta

    #the questionable code, it always prints, "Well, that didn't work."
    if (Input.is_action_pressed("space_press")):
        if (buttonarea == "true"):
            print("Button was pushed!")
        elif buttonarea == "false":
            print("Well, that didn't work.")

Thanks in advance.

in Engine by (29 points)

1 Answer

0 votes
Best answer

mm .. I think your variable "buttonarea" is local to the "onEntranceButtonbodyenter" function ..
Declare it as global ..

extends [..]

# here your variable
var buttonarea = false

func _on_EntranceButton_body_enter( body ):
    buttonarea = true

func _fixed_process(delta):
    if (Input.is_action_pressed("space_press") and buttonarea):
        print("Button was pushed!")
by (48 points)
selected by

You're right, thank you! My code works perfectly now. I feel a little silly that the answer was so simple.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.