How do i run a script when entering an Area2D and pressing a key?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By iNeedMentalHelp

I need to run this script (which shows a dialoue box(i am using dialogic)) when i enter an area2d and press any of the buttons i have set as “Interact”:

	var new_dialog = Dialogic.start("computerInteract")
add_child(new_dialog)
new_dialog.connect("timeline_end", self, 'after_dialog')

anyone have an idea on how i should do this?

:bust_in_silhouette: Reply From: Gluon

The area2d have two signals one for body entering area and the other for body exiting area.

If you use a boolean guard you can have the player entering the area link the signal to a function which turns a boolean true and if the player exits the area have the signal link to another function which turns the boolean false. Then you only have to have some code which says if button pressed and boolean is true do x.

oh hey sorry, i was busy for the past few days since its christmas and all. i tried doing this:

extends Area2D

func _on_Area2D_body_entered(_body):
	var insideArea = true
	
func _on_Area2D_body_exited(_body):
	var insideArea = false
	
func get_input():
	if Input.is_action_pressed("confirm_interaction"):
		if insideArea == true:
			var new_dialog = Dialogic.start("computerInteract")
			add_child(new_dialog)

however im getting this error:

error(11,25): The identifier “insideArea” isn’t declared in the
current scope

It doesn’t make sense (atlest to me) since 11,25 is just a space

iNeedMentalHelp | 2022-12-31 15:04

If you use

var insideArea 

inside a function it creates a local variable which means its a variable only within that function. You need to declare the function at the top of the script for it to be a global variable which is shared by all functions. When you do this you then dont use var when you apply a value to it inside the function otherwise the system will think you are trying to create a new variable with the same name as another variable and you will get an error.

extends Area2D

var insideArea = true

func _on_Area2D_body_entered(_body):
    insideArea = true

func _on_Area2D_body_exited(_body):
    insideArea = false

func get_input():
    if Input.is_action_pressed("confirm_interaction"):
        if insideArea == true:
            var new_dialog = Dialogic.start("computerInteract")
            add_child(new_dialog)

Gluon | 2022-12-31 15:27

it isn’t working, but also isn’t showing any errors, am i supposed to assign any signals? if so, which one to which node?

iNeedMentalHelp | 2022-12-31 15:56

Yes as I mentioned in the original answer you have to attach the two signals for body entered and body exited.

Gluon | 2022-12-31 15:57

so i have to attach the player (kinematicbody2D) to body_entered and body_exited signals in the area2D? because if i do so then

func _on_Area2D_body_entered(body):
	pass # Replace with function body.


func _on_Area2D_body_exited(body):
	pass # Replace with function body.
 

appears in the player move script.
also sorry but i won’t be able to reply for a few hours

iNeedMentalHelp | 2022-12-31 16:07

I am limited in how I can help with this as I am not there but this is where the tutorials are for signals in the official documentation, this may help

Using signals — Godot Engine (stable) documentation in English

Gluon | 2022-12-31 16:17

so the docs didn’t really help but i’m thinking if there is a way to run the script in a loop and as soon as it would run i would set insideArea to false so it doesn’t run repeatedly unless i exit the area2d and enter again

iNeedMentalHelp | 2023-01-02 13:41

You could simply add a line when the button is pressed to turn the boolean to false again. That way when they enter the area it turns true, you press the button and have a line which says

if x == true and input.is_action_pressed("your action"):
    x = false
    do your other things here

Gluon | 2023-01-02 13:43

i have decided to lay this problem off for a while however i need help with a similar problem (it should be on my account)

iNeedMentalHelp | 2023-01-03 13:52