#Instanced objects only showing in viewport and not when running the scene

1 messages · Page 1 of 1 (latest)

brisk tree
#

I have a script that generates a procedural terrain mesh. In this script, I am also running code that instances trees and places them on the map depending on the biome.

Instead of having the code in a _ready() function, I have an exported button that when pressed, runs my tree and mesh generating function. When clicking the button and running the function, it works as expected. It generates a procedural map and populates the map with trees. When running the scene, however, the trees disappear.

I checked the layering and everything looks good. When hand placing a tree into the scene, that tree shows up when running the scene. Weirdly, its just the instanced trees that aren't showing up.

I also have a function that keeps track of the tree instances and appends them into an array so that I could delete the trees when running the function again. When checking the size of the array some weird things happen. If I check it when generating the mesh and not when running the scene, the size of the array is ~10000 (as expected). When running the scene, the array size is 0. I thought maybe when running the scene, the function that deletes the tree instances is being called for some odd reason, so I deleted that function and tried again, but it didn't change anything. The array is declared outside of any function, so its not redeclaring itself as empty like I thought it might be.

Another thing to note: when clicking into another scene, and then back into the main scene that I am talking about above, the trees disappear in the viewport.

Any suggestions?

In the images below, you'll notice one tree is still there when running the scene, but that's the hand placed tree there to demonstrate that only hand placed objects appear.

brisk tree
#

func tree_inst(pos):
    var instance = tree.instantiate()
    instance.position = pos
    instance.visible = true
    tree_instances.append(instance)
    add_child(instance)

func generate_mesh():
    if tree_instances.size() > 0:
        for tree in tree_instances:
            tree.queue_free() #Deletes the previous trees before placing new ones
        tree_instances.clear() #Clears out the instances in the array

        for i in vertices.size():
             if noise_hills_data + noise_general_data > .8: #Checks if in the right biome
                   tree_inst(vertices[i])```
woven haven
#

The generated data isn't being saved. If you run a scene, perform some logic, then replay the scene, the changes won't persist, because those changes aren't part of the scene data, which is being reloaded. Tool scripts work identically, any changes made by them are transient and will be lost upon reloading. The only exception is scene information, e.g. node rotation or position, if these are modified in a tool script these will remain even upon reloading -- because these properties are saved as part of the scene.

To keep your data, you need to save it somewhere, then load it when you need to retrieve it.