How to pass a dictionary of data from one script to another.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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?

:bust_in_silhouette: Reply From: siamak-s

Here is something that I use for above scenario:

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

Xian | 2019-08-06 00:14

:bust_in_silhouette: Reply From: Zylann

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).

You should learn how to manage multiple scripts in your project and make them interact, try to follow the tutorials in the doc (all of it): Scripting — Godot Engine (3.1) documentation in English

Also that question was answered many times: Search results for from another script - Godot Engine - Q&A