Short Version: I want to add the children of an instanced scene as children of a parent node.

Long Version: I want a system that I can put on any node and it just works.

  1. What is run before adding the children:

  2. Deleting the children(done)

  3. Why I want it:

  4. I want to delete the children of a node when the player gets far enough and keep the parent so that I can get the old
    location. (done)

  5. When the player gets back in range I want it to get the children back and add them back in. The object is instanced onto the main scene.

3. What I tried: Preloading the scene where the object is (Instance scene) spits out Error (which can be negated by using load instead of preload) and when I use get_child() after instancing the loaded scene and then using add_child to add it back. It says can't add child CHILD to PARENT. Already has PARENT_FROM_INSTANCED_SCENE get_children() gives me the object but after the objects children nodes are deleted the objects get deleted.

  1. Ideas:
  2. Maybe use a for loop with get_child() the current child number as i as how many times it irretated through the
    code. e.g. for i in get_children(): add_child(i) (Doesn't work!)

You can use remove_child followed up add_child (at a later time) to temporarily disable an object and have it appear later. Though I'm not sure exactly what you're asking. Can you explain the result you are trying to accomplish?

@cybereality I don’t want to use remove_child() because that doesn’t delete them and I think that if you have an open world that it’s gonna lag a lot.

What I’m trying to achieve is...

Delete the children of a rock as an example so no animations, sprites or processes occurre when the player gets far enough and then readd the children when the player is close again. So LOD but delete and readd the children instead of changing sprite.

So I have a question Does using remove_child() actually delete the children or does it keep them in memory? And does keeping them in memory actually lag?

Far as I know, if the system has enough memory it should not lag. It's only if you start paging into virtual memory/swap that it causes lag. Still, you should .queue_free() if you don't need to keep something in memory.

Does using remove_child() actually delete the children or does it keep them in memory? And does keeping them in memory actually lag?

It does not. Call node.qeue_free() to delete them.

for i in get_children():
add_child(i) (Doesn't work!)

that code is broken. you are getting children from a tree, then trying to each child to the node already parenting them.

In the case of what you are trying to accomplish, remove the nodes you wish to keep from their ancestors, if you intend to delete parts 'above' them.

I might suggest you create your scenes in the following way: one-scene contains the 'pure' low cost world element such as a rock model which contains a call that instances to itself a child scene of all the 'extras' you want when the player is near.

then all you have to do is add a function to remove the 'extras' with remove_child(extras) and extras.queue.free()

when the player is proximate, use another function to instance the extras back on your rock (or tree etc)

2 years later