I have a tool script to resize this inventory and when it generates new children using PackedScene.instantiate() and add_child, the new children don't show up on the scene tree and don't show up when I run the actual scene (hotfixed this by having it regenerate on ready). I'm setting owner in the script that creates it and it's working fine in the editor part, am I doing something wrong, is something bugged, or is this a limitation of tool scripts?
#Where Did My Children Go?
1 messages · Page 1 of 1 (latest)
I have a @tool script and using add_child() + setting owner works for me. So show your code?
func reconfigure():
if %grid==null:
return ##During runtime, grid won't be there but **WILL** be called by the setter.
var slots:Array[Node] = %grid.get_children()
var child_count:int = slots.size()
if num_slots<=16:
%grid.columns = 4
else:
%grid.columns = 8
if child_count>num_slots: ##Delete until it fits
for x in range(child_count-num_slots):
var rem:Node = slots.pop_back()
%grid.remove_child(rem)
rem.queue_free()
elif child_count<num_slots: ##Add until full
for x in range(num_slots-child_count):
var new:Node = slot_scene.instantiate()
new.owner = owner ##Whatever my owner is, I guess?
%grid.add_child(new)
size=Vector2.ZERO ##Let our size be as small as it can be.
position=Vector2.ZERO```
Might be setting the wrong owner I suppose, so it's only working partway
Flip owner & add_child()? In my code I first do add_child(), and set the owner afterwards
And/or, try this for the owner stuff: new.owner = get_tree().edited_scene_root (or get the proper reference to whatever node you want to use? Maybe %grid?)
It was this fix that tipped the needle, but I did them both. Thank you.
Which tbh I was banking on it being the other thing.
So that was interesting
There's a couple of things that won't be set (or possible reset?) upon add_child(), some stuff probably gets default values upon entering the tree and running _init() (and/or _ready()). I think the same goes for setting stuff like global_position, etc.. it should be done after adding.