0 votes

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?

in Engine by (271 points)

2 Answers

+1 vote
Best answer

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*


# 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")
# 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): http://docs.godotengine.org/en/3.1/getting_started/step_by_step/scripting.html

Also that question was answered many times: https://godotengine.org/qa/search?q=from+another+script

by (29,092 points)
selected by
+1 vote

Here is something that I use for above scenario:

First Script
var options
func somemethod():
script = preload("res://second_script.gd").new(options)

Second Script
var options
func _init(o):
options = o

by (83 points)

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

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.