Is there something like a node ID which is specific to that node and will be the same no matter how often I execute the game? That would be super useful for saving and loading games.

all 9 comments

sorted by: hot top controversial new old
[–] 3 points 2 years ago (1 child)

What is your use case? That would help narrow down a good solution. Node path could likely work. You could also add a uuid or similar (there's an extension that provides a uuid implementation). Might be hacky, but you could tap into this and see if it suits your needs https://docs.godotengine.org/en/stable/classes/class_resourceuid.html

  • source
  • hideshow 2 child comments
  • [–] [S] 1 point 2 years ago (1 child)

    I want to use this unique value as a key for a dictionary, which will have some data in it (positions, rotations and some custom values)

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 2 years ago (1 child)

    Is there a reason you don't want to serialize that data with the nodes that use them? Are those values intended to change when the entity isn't loaded into a scene (like a background simulation or something)?

    Seems like adding your own uuid or tapping into the ResourceUid class might be your best option

  • source
  • parent
  • hideshow 2 child comments
  • [–] [S] 1 point 2 years ago (1 child)

    I don't really know what you mean by serializing the data. I am using a custom data type for saving all the relevant stuff, in case that is what you mean. That date type stores only the position and rotation and optionally some other stuff too.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 2 years ago

    Yep! That would be an example of serialization. In computer science, taking an applications representation of a data type/object and formatting it as a series of bytes for storage or transmission over a network is referred to as serialization, with deserialization being the opposite process.

    In your case, I would definitely try out the ResourceUID class as it seems like it may fit your needs. You can use it to create and id and store that with the other fields in your custom data type. It returns an int so it will be very easy to use as a key in a dict. Just be sure to call add_id after creating so ResourceUID won't generate duplicates: https://docs.godotengine.org/en/stable/classes/class_resourceuid.html#class-resourceuid-method-add-id

  • source
  • parent
  • [–] 2 points 2 years ago (1 child)

    Node paths are unique and stay consistent across restarts, although I don't know if that's exactly what you're asking for.

  • source
  • hideshow 2 child comments
  • [–] 1 point 2 years ago

    I've thought about this problem creating a system to save game state. The issue with assigning a UUID works until you have dynamic scenes added to the game at runtime. The nodes in those scenes will all have the same UUID. In the end I ended up just using the paths and saving the fact that the scene that data is being saved for is dynamic. Then the system sees this and re-instances the scene and adds it back to the tree. (A slight adjustment to the path must be made as Godot will create nodes with the at (@) symbol in them, but you can't do that yourself.)

    You can see this in action at my demo repo on github.

  • source