I've managed to instance a scene but unfortunately I can't seem to figure out how to change specific variables in the children of this instanced scene. Setting values the conventional way doesn't seem to work.
Accessing and changing variables in instanced scene's child.
- Edited
I put functions in the parent. Maybe there's some better way. I just make it a habit because it seems more organized to be able to operate everything from the parent.
You can also get children by calling parent's get_node()
method.
Whenever I try to even call a method from the instanced scene I get this error:
Invalid set Index on base Nil with value of type Int.
Calling the same method from the top level script does what is intended.
- Edited
brainsiege It'll be hard to help you without seeing actual code.
- Edited
func addchar(assignchar, assignspawn, assignteam, assignai):
match assignchar:
0: #bagel
var createdchar = BagelCharPrefab.instance()
#figure out how to do this.
createdchar.setteam(assignteam)
self.add_child(createdchar)
#createdchar.get_child(0).get_child(9).get_script().charID = CharIDToAssign
#createdchar.get_child(0).get_script().IsAI = assignai
#createdchar.get_child(0).global_position = spawnpoints[assignspawn]
extends Node
onready var charstats := $Character/CharStats
func _ready():
setteam(0)
func setteam(value):
charstats.team = value
export var team : int
This is some awful formatting but this is the function that instantiates and is meant to change details about the instanced scene, followed by the top level node's script and finally the variable declaration of the value I want to modify.
- Edited
brainsiege To format code properly, enclose the snippets with rows containing only ~~~
Like this:
~~~
paste code here
~~~
func addchar(assignchar, assignspawn, assignteam, assignai):
match assignchar:
0: #bagel
var createdchar = BagelCharPrefab.instance()
#figure out how to do this.
createdchar.setteam(assignteam)
self.add_child(createdchar)
#createdchar.get_child(0).get_child(9).get_script().charID = CharIDToAssign
#createdchar.get_child(0).get_script().IsAI = assignai
#createdchar.get_child(0).global_position = spawnpoints[assignspawn]
extends Node
onready var charstats := $Character/CharStats
func _ready():
setteam(0)
func setteam(value):
charstats.team = value
export var team : int
This is the function that instantiates and is meant to change details about the instanced scene, followed by the top level node's script and finally the variable declaration of the value I want to modify.
- Best Answerset by brainsiege
You got the error becuse charstats
in the second script is not initialized at the time you're trying to access it from the first script. charstats
is declared an an onready
property. It means its initialization code will be executed the same time node's _ready()
callback is invoked, and that's the time the node is added to the scene tree. So charstats
value will be uninitialized (null) until createdchar
is added to the scene tree.
Ahh right yeah that makes sense.
Ok I don't get the errors now as I've had the addchar function a yield for a single frame. This:
yield(get_tree(), "idle_frame")
However when I try to set the required values (global_position, team, etc.) via the functions they do not get changed in the actual instanced scene.
- Edited
brainsiege I can't see how your code flows just from a single line.
You don't need arcane stuff like yields for this. Just call add_child(createdchar)
immediately after instancing and before doing anything else with the instance. There may be problems with logic/flow elsewhere in the code.
xyz Nevermind I got it working now. Was poor handling of that top level script.