How to save a reference to a node, into a file and use that reference to target the node after reading the file.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Xian

I would like to show some code but that would be difficult since I have too much code and in different scripts that are all connected.
Basically, as the title said. I was working with multiple different scripts in my game and I want to pass non constant variables to other scripts during the gameplay. At first I decided to save the target node into an array and store that array into a dictionary and then into a file. long story short the data was different. if u print(str(ANY-NODE)) you would get “[node_class:node_id]” but after reading it, the data was not the same and the node was read as a text instead. I am probably doing this inefficiently. Someone please teach me how to pass data between scripts during gameplay.

I am pretty sure if you read a node, what you are seeing is the reference data to that node.(much like a pointer in c). if there is a way to type cast the string that contains the reference to a node, as a node then maybe what I am doing could work.(I have a string that holds the same data as a node, but read as a string. this is one of the difficulties of loosely typed variables)

Xian | 2019-08-05 07:02

:bust_in_silhouette: Reply From: Zylann

When you do str(object), what you get is the runtime ID of that object. So that one can totally change between two executions, or even be re-used by another object after it is destroyed.

In order to be able to identify an object from a file you saved earlier, you have to give it a unique name, or ID of some sort, so that you can rebuild references when you read it from the file. It is up to you to decide how to do it.
Of course, reducing interconnection between your scripts can help making this task easier.

However, I read this:

Someone please teach me how to pass data between scripts during gameplay.

I might be wrong regarding the context of your post, but the answer to that specific question has been answered several times, and you don’t need a file to do that: Search results for variable from another scene - Godot Engine - Q&A

thanks its helped :slight_smile: I learned something new. Also about the passing variables… i read through them already but passing nodes is rather the exception. most pass variables that are constant, some pass functions, and it all makes sense but passing nodes seem rather unique. i couldnt find a working reference… Not to mention I am generating a node and storing their reference to an array list so… yeah there isnt really any good reference to passing something like this… But your answer helped me out think about what i can do with nodes so maybe i can come up with a solution :slight_smile:

Xian | 2019-08-06 00:04