I got a little problem while trying to "spawn" Items from my Inventory. My Items are an own class based on Spatial. When I collect an Item, its Scene gets removed as a Child of the Scene Tree. I reference the Items in Inventory classes that "store" them in their arrays. But I got stuck while attaching them to the Player. I have a BoneAttachment at Players left hand, but I dont know how to equip an Item to it. Adding the Item-Node as a Child doesn't spawn its 3DModel and I got no other idea.
"Instantiating" an existing node to the Scene Tree
There are two ways. You can use load or pre_load to cache the scene (with the weapon or whatever). Then:
onready var sword_scene = preload("res://sword.tscn")
func _ready():
var sword = sword_scene.instance()
add_child(sword)
That would be the best way I think. But you can also use duplicate() if the scene already exists.
@cybereality said: There are two ways. You can use load or pre_load to cache the scene (with the weapon or whatever). Then:
onready var sword_scene = preload("res://sword.tscn") func _ready(): var sword = sword_scene.instance() add_child(sword)
That would be the best way I think. But you can also use duplicate() if the scene already exists.
Thank you cyber (again), but I don't think thats the (complete?) solution to my problem. As far as I can recognize, this is the procedure to add a totally fresh scene per code to the scene tree, with a totally fresh Script.
The problem I have is that the Script/Node of the scene I want to instantiate already exists in my inventory arrays after I deleted their scenes/3DModels. Stuff like their duration points are already stored in that Node. But I can't manage to "reinstantiate" the existing Node to the scene tree. Either that, or I have to find a way to attach an already existing Script to a new, scriptless Scene/3DModel.
@cybereality said: There are two ways. You can use load or pre_load to cache the scene (with the weapon or whatever). Then:
onready var sword_scene = preload("res://sword.tscn") func _ready(): var sword = sword_scene.instance() add_child(sword)
That would be the best way I think. But you can also use duplicate() if the scene already exists.
Seems like adding it as a child to the world tree works completely normal, so I guess the problem lies somewhere within my bone attachment. Thanks, anyways
Yes, if it exists already you can just use add_child (provided it isn't in another node already).