I am coming from a Unity background and there I just had a component of some custom class in the scene which I could then easily get by calling FindInScene<CustomComponent> or something like that. Not in Godot this doesn’t work, because I didn’t find a way to get the actual class of an attached script. I always just get GDScript as the class name even though I did specify a custom one.

The information I want to save are things like: where to spawn players, how many laps will this race have, maybe save references to the spawned players, etc.

So how would I save this “meta” information to get by another script in Godot?

EDIT: here is an example: I have a main scene which can load different levels. When loading a level I need to get some information about that level, like: the available spawn points. Inside each level I have a node with a script attached to it that defined class_name LevelMeta and holds the meta information that I need when loading the level. How do I detect this script and the associated meta information?

  • unexposedhazard@discuss.tchncs.de
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    4 months ago

    Im not really sure i understand. A scene is just a node with some children, if you want to store and read information from it then you just attach a script to the root node and declare some variables in it no?

    If you add some custom node or instance to it, then you just get_node that by its path and access its variables.

    • BentiGorlich@gehirneimer.deOP
      link
      fedilink
      arrow-up
      1
      ·
      4 months ago

      Yes exactly. but how do I detect a specific script in my scene view? Or in other words: how do I find the source of my meta information in a scene?

      • unexposedhazard@discuss.tchncs.de
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        4 months ago

        Oh you want to know the name of the script that was previously attached to a node?

        get_script().get_path() should return the filepath to the script that you used, not sure if thats what you want tho.

          • unexposedhazard@discuss.tchncs.de
            link
            fedilink
            arrow-up
            4
            ·
            edit-2
            4 months ago

            I see, looks like that is actually an issue.

            https://github.com/godotengine/godot/issues/21789

            Seems like you have to overwrite the get_class function if you want to detect custom classes.

            extends Node2D
            
            class_name CustomClass
            
            func get_class(): return "CustomClass"
            func is_class(name): return name == "CustomClass" or .is_class(name) 
            
            
            func _ready():
            	print(get_class())
            	print(is_class("CustomClass"))
            	print(is_class("Node2D"))
            
              • unexposedhazard@discuss.tchncs.de
                link
                fedilink
                arrow-up
                2
                ·
                edit-2
                4 months ago

                Defining a unique class name gets you the same thing as giving each script a unique filename and then differentiating between the get_script().get_path() but i can see how its not as clean for comparison.

                Another solution would be to just give all your custom classes a var called “class_id” or something and then you can read that out if you need it.

                  • unexposedhazard@discuss.tchncs.de
                    link
                    fedilink
                    arrow-up
                    4
                    ·
                    edit-2
                    4 months ago

                    Im assuming you have a predefined level scene, which you instantiate and add as a child to your main scene. If i were to save a bunch of metadata about that level scene, i would add a dictionary named “level_data” to the script of the root node of that levels scene.

                    extends Node
                    
                    var level_data = {
                    "spawn_point": Vector2(x,y),
                    ...
                    }
                    

                    After you instantiate that level from your main scene with:

                    var level1_tscn = load("res://path/to/tscn")
                    var level1 = level1_tscn.instantiate()
                    

                    You can then get or modify the metadata from that instance reference.

                    print(level1.level_data["spawn_point"])
                    

                    When you are done you can then add it as a child to the main scene with add_child(level1).

                    You can also do that right away and access the data later ofcourse, but if the values are needed in the _ready function of the level, then you need to modify them before adding it to the main scene.