Change variables in script attached to dynamically created nodes

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

EDIT: here’s a more concise way of wording the problem:
I need a way to either:
a) change the value of a variable that is in a script attached to an instantiated node/scene
or b) call a function in a script attached to an instantiated node, to then change the variables/scene
Both need to be able to be done when the scene is instantiated.

[Original]
I’m making a satellite simulator.

I have a base satellite scene that has the satellite script attached to it. This script (satellite.gd) holds everything about the satellite - name, file path (where the satellite coordinates are located), etc. This script then sets the position of the satellite every frame.

I then have a separate script attached to a node that’s always in the scene tree.This script (satellite_builder.gd) loops through a JSON that contains the info for every satellite (name, file path, etc.) and instantiates the base satellite scene in the scene tree.

However, now I need to be able to take the name and file path form the JSON (just strings) and set it to the instantiated satellite.

I tried this:

for sat_index in satellite_config.size(): 
		var new_satellite = load("res://satellite_visualisation/satellites/satellite.tscn").instance()
		var sat_name = satellite_config[sat_index]["name"]
		var file_path = satellite_config[sat_index]["file_path"]
		var time_resolution = satellite_config[sat_index]["time_resolution"]
		
		new_satellite.setup(sat_name, file_path, time_resolution)
		
		add_child(new_satellite)

Then in setup() in satellite.gd, I set the name, file path variables, etc.

However, this throws the error:
“Attempt to call function ‘setup’ in base ‘null instance’ on a null instance”
Even though setup() is in satellite.gd

Then I tried just setting the variables directly like:

new_satellite.name = satellite_config[sat_index]["name"]
new_satellite.file_path = satellite_config[sat_index]["file_path"]

but that threw the error
“Invalid set index ‘file_path’”
(interestingly it could set ‘name’ properly, but not anything else - the variable name for ‘name’ was highlighted light blue in the editor and nothing else was, if that helps).

Essentially, how can I change the value of a variable stored in a script that’s on an instantiated node, from a non-instantiated node?

Thanks

:bust_in_silhouette: Reply From: nonunknown

Plz answer this question, so I can see if I can help you

  • Its really necessary in your case to use JSON or you did it cuz you dont know another way?
  • Explain in general what are you trying to do (no code, just the theory behind)

Cuz if you dont need json, I suggest you to look one of the most cool feature that godot have the Resource class
Also this tutorial about resources may help you understand its power

Yes, JSON is needed. This is proprietary software for a company and they require JSON.

Theory wise, I essentially need a way to either:
a) change the value of a variable that is in a script attached to an instantiated node/scene
or b) call a function in a script attached to an instantiated node, to then change the variables/scene

Both need to be able to be done when the scene is instantiated.

jacksonyukihillary | 2020-08-01 00:44