+1 vote

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 "[nodeclass:nodeid]" 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.

in Engine by (271 points)

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)

1 Answer

+1 vote

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: https://godotengine.org/qa/search?q=variable+from+another+scene

by (29,090 points)
edited by

thanks its helped :) 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 :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.