Connecting Area2D to Input Recognition

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GiantIsopod
:warning: Old Version Published before Godot 3 was released.

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 _fixed_process(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 _fixed_process(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
		RayNode.set_rotd(180)
		Playerdirec = "Up"
	elif (Input.is_action_pressed("move_down")):
		direction.y = 1
		RayNode.set_rotd(0)
		Playerdirec = "Down"
	if (Input.is_action_pressed("move_right")):
		direction.x = 1
		RayNode.set_rotd(90)
		Playerdirec = "Right"
	elif (Input.is_action_pressed("move_left")):
		direction.x = -1
		Playerdirec = "Left"
		RayNode.set_rotd(-90)
		
	#velocity for player movement (which also works)
	velocity = MAX_SPEED * direction.normalized() * delta
	move(velocity)
	
	#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.

:bust_in_silhouette: Reply From: ADcomp

mm … I think your variable “buttonarea” is local to the “_on_EntranceButton_body_enter” 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!")

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

GiantIsopod | 2017-08-26 13:54