#Global Exposed Property

10 messages · Page 1 of 1 (latest)

amber venture
#

I have a workaround in mind for this already, but I just wanted to see if anyone else here could think of a more elegant solution. Any participation is welcome.

Requirements:
1) Be a single global variable property shared by any level that is loaded.
2) Be a property, meaning that it both can be seen from and edited from the editor GUI.

My current workaround:
1) Give an Autoload object/script the global version of the variable you plan to expose via property.
2) Have each level hold a local exposed property counterpart of the global version of the variable.
a) Via Signals (I hope), whenever a level is loaded, assign the Autoload's (effectively) global variable to the level's local exposed property.
aa) levels_exposed_property = autoload_global
b) Via Signals, update the global variable whenever any associated (level node script) property is updated manually by the editor user.
aa) autoload_global = levels_exposed_property

Is my workaround the most elegant approach for this goal? Or is there a more straightforward workflow I missed?

Note: As far as I am aware, the only way to tie a variable's lifetime to the game application is to store it inside an Autoload instance, and that Autoload variables cannot be exposed to the editor via the property @export macro.

icy anchor
# amber venture I have a workaround in mind for this already, but I just wanted to see if anyone...

Hello @amber venture!
Autoloads are good for this, however for your #2 requirement (having it be seen and edited from the editor)...
Since you cannot access the autoloaded node in editor through local scene tree / before runtime...

I would recommend trying to make your own master scene.

Instead of using the built-in scene-switching functions of Godot, the master scene's script would instantiate level scenes as children, and remove them. You can store variables in the Master Scene top node, and get that node using get_tree().current_scene (which in our case never changes, it is always the Master Scene)

#

Example script for master scene

extends Node2D

var current_level : Node

@export var global_var_1 = 5
@export var global_var_2 = 10


func _ready():
    var next_level = load("res://level_1.tscn").instantiate()
    change_level(next_level)


func change_level(desired_level):
    if desired_level != current_level:
        if current_level: current_level.queue_free()
        add_child(desired_level)

amber venture
icy anchor
#

Not sure exactly what you mean
but yes you can edit instances of scenes in the editor

In this case you can click the Master Scene node and then see global_var_1 and global_var_2 in the inspector

amber venture
#

I'll give an example.

Let's I have 3 distinct scenes (levels). I want them all to reference a common variable in order to automatically move different Nodes to different 3D coordinates at editor-time. (Snapping to Hexagonal Grid Coordinates).

In this case, it is a float variable that indicates how big a Hex Tile is in cartesian editor's) coordinates. I would change this value depending on what sized 3d meshes I import into the engine.

Your solution looks perfect for runtime use, though I am unsure if I can see how I'd use it for the use case of editing levels.

My current assumption is that you can only edit a Scene itself (not just an individual instance of it) by opening said scene directly in the editor (Ctrl+O).

icy anchor
#

Sorry if I misunderstand, but If you are attempting to use scripting to change the position of nodes in the editor (before / without starting the game), I don't think an Autoload would work for that either, since it's loaded on runtime. You would likely need to do some magic with @tool scripts.

amber venture
#

I think I'm starting to see what I should do.

If I need to position something, I should use an Editor Plugin (with the @tool macro in it.) It is active and alive during the entire editor's lifetime).

If I need to store grid information associated to an individual level (tiles and coordinates), I can have the editor plugin simply find and communicate with the currently loaded level (works for both the built-in scene mechanism, or tailor it for a custom mechanism).

#

Mad thanks Creature.

clear garnet
#

don't forget that you can also autoload a scene