How to pass data between scenes?

Godot Version



So, i have this function where if the player interacts with a area2d, it’ll get a random enemy string and change then change the scene to the battle scene.

What i would like to do is, send the random enemy string generated to the new battle scene without using Singletons/Globals/Auto Loads since i don’t think its necessary

(Sorry for the bad code in advance)
Here’s a look at my code right now:


The script encounter generator:

Is there a reason you don’t want to use autoloads? Sounds like a good use case for me.

Since it’s a battle scene I’m assuming it’s a very temporary scene before you’re right back to the normal scene, and recommend not using change_scene. Add the battle scene to the root node, and set the current scene’s process mode to “Disabled”, this will essentially pause everything from before. If need be, hide the current scene too.

func _on_timer_timeout():
    var battle := preload("res://scenes/battle.tscn").instantiate()
    var tree := get_tree()
    tree.current_scene.process_mode = Node.PROCESS_MODE_DISABLED
    tree.current_scene.visible = false # if node2/3d


    await battle.finished
    tree.current_scene.process_mode = Node.PROCESS_MODE_PAUSABLE
    tree.current_scene.visible = true

This code is just a test place for a future game, where i can test features for a possible game.

I’m afraid that i’ll overuse singletons with the game, and to be honest i don’t think its safe either to use it

Wont that interfere with the game? since the main game is 2d and the battle is 2.5d (3d scene)

Nope, it works for me; it does need to visible = false the 2D scene though. Possibly even easier since you do not have to worry about cameras.

Thanks, i tried it out and it worked, i’ll try to manage a way to make it usable as in the statement, since i don’t know if i’ll use it in another feature, just to make sure.

The only problem is that the code is running in a singleton

But thanks you helped my very much <3

1 Like

I think for that i will just create a parent for the world map.
A tree example:

  • SceneTransition/SceneManager
  • CanvasLayer (Overall UI)
  • Master Node
    • Everything Else

So for that i would grab Master Node and Disable it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.