Hi,

small question. I have an object in all of my levels that has a reference to the next level, so I know where to warp to, when the level is finished

export (PackedScene) var next_world: PackedScene

This way I can easily link everything up. The same for my starting scene which links to the first level and the last level that links to the game ended screen.

Now I have one special case: If the player dies, I have to send him to the game over screen. Right now I have a singleton autoloaded that knows about this

SignalBus.emit_signal("request_change_scene", "res://GameOver.tscn")

First of all this is hardcoded and second of all it is incompatible with my other way of requesting a scene switch by linking a packed scene. But I also don't want to put that in every scene and wire it up.

Is there a way to export variables on autoloaded scripts or which other way would you recommend me to choose?

I do not know of any way to export variables in singletons, though there may be a way. Personally, I would define the scene as a constant at the top of the singleton script, so it looks something like this:

extends Node

const GAME_OVER_SCENE = "res://GameOver.tscn"
# other stuff here...
func _ready():
	# for example:
	SignalBus.emit_signal("request_change_scene", GAME_OVER_SCENE);

That way, you can just change the path in the constant, making it easier to switch scenes later in development if you need. An added bonus is in other scripts you can do something like change_scene(singleton_script.GAME_OVER_SCENE) for example, making it where the only hard coded path is in the singleton script.

That is what I would do, though there may be better ways to go about it. (side note: welcome to the forums)

Good idea with the constant. But I still need to maintain both apis for scene switching. By path and by PackedScene. And PackedScene as an export has the benefit that it updates when change the location of the resource.

Nevertheless I decided for a differen solution. I added an empty node to my levels and attached a script with the exported variable to link my scene. Then I branched this out as own scene and put this in all my levels. Now I can maintain it in its own scene.

.... while writing this, I wonder if changes to exported variables on my branched scene will propagate to the levels.... I will check it :-)

I'm not sure if I have understood your scenario correctly, but just for your information, if you are not aware of it yet, it's possible to define scenes as autoloaded singletons in the project settings, as well as pure script files. I have plenty of them in my project and my list of singletons is a mixed list of both .gd files and .tscn files.

The node name you give to the scene in the configuration will be the global name which you use in code to refer to the scene node.

And of course, the autoloaded scenes may contain exported variables.

For example, in my project, the scene:

"res://src/Core/Player.tscn"

containing the code:

extends Node
class_name Player

export (String) var starting_person_id
export (String) var starting_car_id
export (String) var starting_street_id
...

is setup in the AutoLoad tab with the Node Name:

"player"

and then exported variables as well as other members can be referred like:

player.starting_car_id

anywhere in the project code.

(Note that the node name "player" is in lowercase , deliberately to distinguish it from the class name "Player")

No, I wasn't aware and this is basically what I am looking for.

Thank you!

3 years later