duplicate() is not working on my code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TomasGamer

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 = rand_range(-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 =

func _ready():
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])
get_node(“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())

enter image description here

enter image description here

enter image description here

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

Noddy | 2022-02-11 04:54

:bust_in_silhouette: Reply From: rossunger

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

Eg.

Var newNode=oldNode.duplicate()
Add_child(newNode)

Thank you so much, it worked so well by add_child(get_node(“Wall”).duplicate())

TomasGamer | 2022-02-12 00:27

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.

grymjack | 2022-12-18 21:31

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

rossunger | 2022-12-18 22:04

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()
add_child(my_copy)
my_copy.visible = true
my_copy.set_position(Vector2(xpos, ypos))
my_copy.rect_scale = Vector2(asset_result[0]['scale'],asset_result[0]['scale'])

grymjack | 2022-12-18 22:29

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

rossunger | 2022-12-18 22:33

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()

grymjack | 2022-12-18 22:37

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()

rossunger | 2022-12-18 22:40

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!

grymjack | 2022-12-18 22:47

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

rossunger | 2022-12-18 22:51

That did the trick! Thank you!

grymjack | 2022-12-18 23:01