Topic was automatically imported from the old Question2Answer platform.
Asked By
Xian
Basically what that title says. How do I pass something from 1 script to another? I have an array of nodes that exist in my generate terrain script. And I tried to pass it via file save and load system. but the engine read the array of nodes from the file as a array of string(PS I tried many methods to save and load already). Is there a more efficient way to pass an array of nodes to different script?
First Script
var options
func some_method():
second_script = preload(“res://second_script.gd”).new(options)
Second Script
var options
func _init(o):
options = o
thanks this would work for most situations, but not all… in my situation my second script generates a voxel terrain and keeps some blocks in a array list of nodes, and i want to pass that list to my first script. but doing so with the method above did some weird logical errors
Passing any data from a script to another depends on what instance they represent. You then get the second instance and call it from the first instance.
# In script A
var b = *get the instance of B*
b.some_function(the_data)
or
# In script B
var a = *get the instance of A*
print(a.the_data) # do stuff with it
If they are both nodes, it looks like this:
# In script A
var b = get_node("Relative/Path/To/B")
b.some_function(the_data)
# or
b.the_data = the_data
But it’s not the only way of course (they can be resources or pure classes).