So I'm trying to make a random blocks generator on a certain area, here's my code, when I test how many children the generator node has after it was meant to duplicate the node it says 1, and only 1 block was randomized on the game when I play it, by my testing there is some problem with the duplicate() function, it just doesnt duplicate the block

func RandomTilePosition():
var x = randrange(-7, 6) #Generates a X position
randomize()
var z = rand
range(-7, 6) #Generates a Z position
randomize()
var returning = Vector3(round(x) + 0.5, 1.5, round(z) + 0.5) #sets a variable to what I need to return, to test it later
if not returning == Vector3(-6.5, 1.5, 6.5): #tests if its not the exact place the player will spawn
return Vector3(round(x) + 0.5, 1.5, round(z) + 0.5) #gives the randomized value when you use the function

var Walls = []

if Global.difficulty == 1: #this is for a thing on my game, ignore
for count in range(3): #duplicates the block 3 times
Walls.insert(Walls.size(), RandomTilePosition()) #saves the random number on a list to use it later
get
node("Wall").translation = Walls[count] #sets the position of the block to then duplicate it on the right area
print(Walls[count])
getnode("Wall").duplicate() #this is the part that I think is not working, and by what I saw its the only way to duplicate
print(get
child_count())

Godot version 3.4.2 stable
in Engine

Please format the code with the code sample button or markdown.

You need to add_child() after you duplicate! :)

Eg.

``````Var newNode=oldNode.duplicate()
``````
by (1,346 points)
selected

Thank you so much, it worked so well by addchild(getnode("Wall").duplicate())

How do you clear the Add_child(newNode) from memory when done? I was having duplicated resources from the previous screen showing up in a redraw of the screen.

I'm not sure what your exact situation is, but if you've added a child, and you want to remove it you need to call `queue_free()` on the child if you want to delete it completely, or if you just want to remove it from the scene but keep it in memory so you can add it back later, you need to call `remove_child(child)` on the parent

I use the code below to create duplicate resources and display them. The user can then move on and request a new set of resources be displayed. However the old set are still displaying on the screen along with the new set. How do I track remove the older duplicated resources from memory? I also apologize for double posting as I asked this elsewhere as well.

``````# setting node name and placing resource on screen
temp_node = get_node(location_info.path + location_info.filename)
var my_copy = temp_node.duplicate()
my_copy.visible = true
my_copy.set_position(Vector2(xpos, ypos))
my_copy.rect_scale = Vector2(asset_result[0]['scale'],asset_result[0]['scale'])
``````

At some point, you need to remove the resources you just duplicated. Where is your code that does that?

If your question is more about how to TRACK them, then you need to have an array variable that stores all the new resources, and when you want to delete them you go through that array and call `queue_free()` on each one

How do you get an id for the resource to call queue_free() with? Something like the line below?

``````var resource_index = my_copy.get_index()
``````

you don't need the index. queue_free() is a function that tells the node to delete itself.

``````var my_node = get_node("your node")
my_node.queue_free()
``````

There can be up to 30 duplicated resources displayed on the screen at once and I don't want to remove them until the user moves on. So as I follow you something like below:

``````var tracker_array = []
tracker_array.append(my_copy)
``````

Then when the user is ready to move on loop through the array using your code

``````var my_node = get_node(tracker_array[loop])
my_node.queue_free()
``````

Does this look right to you? Thank you for sticking with me on this!

when you append(my_copy), you're appending the actual node, not a `node_path`, so you don't need to call `get_node` again when you're deleting.

you can use:

``````for my_node in tracker_array: my_node.queue_free()
``````

this will go through each node in the tracker array and delete it

That did the trick! Thank you!