Hello there. So I'm still fairly new to Godot, but I thought I'd make a simple program to help me understand the fundamentals more apart from watching tutorials.
Basically, in the game you're a small little blob and you go around the screen to 4 other blobs and press "Enter" to make them dance (the dance is very simple, it's just two frames, one standing one crouching).
I have my world scene, my player, and 4 instances of my "npc" scene, each with an Area 2D node and a collision shape 2D node. But after writing my script, I notice that the enter key has to be pressed AS you enter the collision area in order for the dance animation to play. How can I rewrite my script so that the npc animation will play as long as the player is in the area already and has pressed enter once?
Your problem is that you are using the body_entered function, meaning it only gets called once, when the collider enters for the first time. I take it that you basically want that function to keep getting called while the player stays in the area.
Now, I don't know GDscript but I searched it up and a good fix seems to be to use a true/false that keeps track of whether or not the player is inside the area. Then repeatedly checking if it is true:
Make a new boolean called "touching_player" or something similar. Then use the body_entered and body_exit to set it true or false. Then, in the main loop, if touching_player is true then do the enter key stuff.
Your error is caused because the line is not inside any function.
If you don't know what a function is:
It is basically a container that holds code (such as the if statement), and runs whatever is inside when the function gets "called." As you can see in your code, the _ready() function is "called" at the start of the game and runs the code inside.
What you need is the _process() function. It gets called on every single frame, so whatever you put inside will be run over and over. It comes as a default whenever you make a new script.
Anyways, once you have a _process() function then you can check for if the touching_player is true/false.