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?

  • BougieBirdie@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    5
    ·
    5 months ago

    You may find some value in Groups. This lets you assign a node to a group by name, then later get the nodes in the group by searching for that name.

    In your specific example you mention being able to fetch the spawn points. I’d add each spawn point to a group called “spawn” and then later retrieve them with:

    var spawn_points = get_tree().get_nodes_in_group("spawn")

      • BougieBirdie@lemmy.blahaj.zone
        link
        fedilink
        English
        arrow-up
        4
        ·
        5 months ago

        I find them very handy. I also came to Godot from Unity, and groups almost work identically to Unity’s FindAllInScene()<> method. You just have to do a little setup to give them a string instead of searching for class type, but they behave very similarly.

  • unexposedhazard@discuss.tchncs.de
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    5 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
      ·
      5 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
        5 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.

  • butitsnotme@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    5 months ago

    I would create separate nodes for each piece of metadata within the scene you are loading, you can use a Node2D (or Node3D) to store a position (such as a spawn point) and there is a Timer node which can hold the time for a level.

    Alternatively, under the node inspector there is an option to “Add Metadata”, which might be more along the lines of what you are looking for. It can be read with the get_meta function.

  • Domi@lemmy.secnd.me
    link
    fedilink
    arrow-up
    2
    ·
    5 months ago

    Do you use C# since you’re coming from Unity?

    You can use GetNode<CustomNode>() or GetChild<CustomNode>() to find the node you need just like in Unity. CustomNode will be the type of your script or if there is no script attached to your node you can use the builtin types as well (e.g. Node3D).

    Once you have the node you want, you can either use Godots builtin functions SetMeta() and GetMeta() to set and get metadata on a node or use the C# way and access the public properties set on that CustomComponent class directly.

    I don’t use GDScript but I assume you have the same methods available there.