The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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
var z = rand
range(-7, 6) #Generates a Z position
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
node("Wall").translation = Walls[count] #sets the position of the block to then duplicate it on the right area
getnode("Wall").duplicate() #this is the part that I think is not working, and by what I saw its the only way to duplicate

enter image description here

enter image description here

enter image description here

Godot version 3.4.2 stable
in Engine by (15 points)

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

1 Answer

+2 votes
Best answer

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


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

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

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 = []

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

var my_node = get_node(tracker_array[loop])

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.