How write the function of a button pressed inside an area2D function?

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

Greets!

Got an area2D wich once triggered gives some results, amongst them the possible change of scene with a button pressed.

Following:

func _on_Area2D_body_entered(_body):

I’d need something like that:

if button_Enter_is_pressed:
     get_tree().change_scene("res://Scenes/CCreation.tscn")

Any help?

So you want to have a button, that (once clicked) changes the current scene. But only after a body (Any body? Just the player?) entered a certain area? Should the button stop working after the body leaves that area again? Is the button inside the area?

njamster | 2020-02-24 12:13

Actually got one button on my GUI that should serve to change to scenes from my TileMap scene. On each tile there’s an area2d and its collisionshape when body entered/exited, triggering sprite/image, labels/description, audio/sounds, random events and so on.

Look: Imgur: The magic of the Internet

See the arrow to the right button? That should allow to enter the different adventures zones on special tile.
Here is the typical script of one of those collisionshape/tile: Imgur: The magic of the Internet

Trouble is, my button function only change scene to the last signaled/conected, always the same scene, whatever the scene name written in script. So i thought it best to be in the ‘on area2D entered’ function. But how?

And yes, once pressed, the scene should change, and there’s no more button.

Syl | 2020-02-24 12:44

:bust_in_silhouette: Reply From: njamster

So your question is: Once the player clicks the button, how does the game determine which scene to load next? You state that this information is provided by the tile in the TileMap the player is standing on. So the actual question is: How do you get data from one node (the TileMap / Area2D) to another (the Button)?

However, there is no unique answer to that question. The best solution depends on a lot of different factors that cannot be reasonably discussed in a forum like this. Here are a few suggestions that might or might not work for your specific project:

  1. When the button is clicked, get the position of the player-node, determine the tile underneath the player and deduce the scene to switch to based of that
  2. Attach a script to your button and create a variable storing the scene to switch to. Whenever the Area2D is entered, change that variable. You can either do this directly (by getting the Button-node and changing the value of the variable) or indirectly (by emitting a signal and connecting the Button to that signal).
  3. Create a new singleton script and store the path for the scene to switch to in there. A singleton will be easily reachable from everywhere by its name, so it should be easy to change and access a variable inside that script.

Cheeeeeeeeers! Made it with the second option. :slight_smile:

Syl | 2020-02-24 16:00