How do I detect which Area2D has been entered.

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

I am working on a dialogue system for my game. I am making it that the player and intractable objects have Area2Ds around them. I connected the Players area to the object area with area_enter, and changes a var called caninteract to true. The problem is that connecting it like that makes me interact with the interactable that is highest in the scene tree, no matter which area was entered. I could connect from the object to my player, but storing the information for every single dialogue in the game in one script would be hellish. What should I do? Here’s the code if that helps.

extends Area2D
var caninteract = false
var stage = 1

func _ready():
	set_fixed_process(true)
	get_node("../../../World/CanvasLayer/PlayerSpeech/Button1").connect("pressed", self, "on_button1_pressed")
	get_node("../../../World/CanvasLayer/PlayerSpeech/Button2").connect("pressed", self, "on_button2_pressed")
	get_node("../../../World/CanvasLayer/PlayerSpeech/Button3").connect("pressed", self, "on_button3_pressed")
	get_node("../../../World/CanvasLayer/PlayerSpeech/Button4").connect("pressed", self, "on_button4_pressed")
func _fixed_process(delta):
	if caninteract == true:
		if Input.is_action_pressed("Interact"):
			caninteract = false 
			get_node("../../../World").set_isinteracting(get_node("../../../World").isinteracting + 1)
			get_node("../../CanvasLayer/Panel/NPCSpeech").set_text("The curtains partially cover the window.")
			get_node("../../../World/CanvasLayer/PlayerSpeech/Button1/Option1").set_text("Open the curtains.")
			get_node("../../../World/CanvasLayer/PlayerSpeech/Button2/Option2").set_text("Walk away.")
			get_node("../../../World/CanvasLayer/PlayerSpeech/Button3/Option3").set_text("...")
			get_node("../../../World/CanvasLayer/PlayerSpeech/Button4/Option4").set_text("...")
# Dialogue Tree.
	if stage == 1:
		get_node("../../CanvasLayer/Panel/NPCSpeech").set_text("The curtains partially cover the window.")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button1/Option1").set_text("Open the curtains.")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button2/Option2").set_text("Walk away.")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button3/Option3").set_text("...")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button4/Option4").set_text("...")
	if stage == 2:
		get_node("../../CanvasLayer/Panel/NPCSpeech").set_text("Before you lies the night sky of New York.")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button1/Option1").set_text("Close the curtains.")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button2/Option2").set_text("Walk away.")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button3/Option3").set_text("...")
		get_node("../../../World/CanvasLayer/PlayerSpeech/Button4/Option4").set_text("...")
	
	

	
func  on_button1_pressed():	
	if stage == 1:
		stage = 2
	elif stage == 2:
		stage = 1

func  on_button2_pressed():
	if stage == 1 or stage == 2:
		get_node("../../../World").set_isinteracting(get_node("../../../World").isinteracting - 1)

func  on_button3_pressed():
	pass
func  on_button4_pressed():
	pass






func _on_Player_Interact_area_enter( area ):
	caninteract = true




func _on_Player_Interact_area_exit( area ):
	caninteract = false
:bust_in_silhouette: Reply From: bruteforce
func _on_Player_Interact_area_enter( area ):
	caninteract = true

The “area” parameter is the Area2D node that entered… or I misunderstood your question :slight_smile:

Do you mean I put the name of the area node?

Frostybros | 2017-11-04 18:28

To rephrase the question, using…

func _on_Player_Interact_area_enter( area ):
caninteract = true

… only checks if the player has entered an area, not that it has entered a specific area. How do I make it detect if the player has entered a specific area?

Frostybros | 2017-11-04 18:31

I said, you get the Area2D reference automatically in your _on_Player_Interact_area_enter( area ) function (the parameter).

Check this:

func _on_Player_Interact_area_enter( area ):
	print(area.get_name()) # the concrete area name, that entered
	caninteract = true

bruteforce | 2017-11-04 18:34

I’ve since discovered this isn’t whats wrong with my code. I’ll continue working on this. Thanks :slight_smile:

Frostybros | 2017-11-04 19:01