I don't know how to clone a block

Godot Version

4.5.1

Question

Hello, I am developing a video game where you have to stack blocks. The idea is to stack the blocks to reach as high as possible.

The problem I have is that I don’t know how to make copies of the same block that appear when a block is stacked.

I’ve tried putting several hidden blocks and then revealing them, but the problem is that the blocks continue to interact with each other.

The blocks move from left to right. This is the code that makes them do that:

Please, can someone send me a tutorial or give me instructions on how to do the above?

Thank you in advance.

What do you mean by “block”? Is it a single node or a scene?

The block is a RigidBody

if you create the RigidBody in a scene you can make a reference to it with

var block = preload(the file path goes here, you should be able to type the scene name and it'll autocomplete)

then when you want to duplicate it you do the following code

var blockInstance :Node3D = block.instantiate()
add_child(blockInstance)

after calling add_child() you can change things of blockInstance like it’s size and position.
(is it ironic that I’m making this reply while I’m having my own issues with add child)

2 Likes

I think the easiest way is to instance the block objects from saved scene format.

3 Likes

Thank you very much. I haven’t had time to try it yet, but I’m almost 100% sure it will work.

Sorry for replying so late.

1 Like

Hello again, I implemented what you told me and I get this error: Stack overflow (stack size: 1024). Check for infinite recursion in your script. I don’t know how to fix it.

Sorry for being so annoying, but I’m pretty new to this and there are things I still don’t understand.

Let’s see your scene structure and your code.

You should call instantiate() from outside of the scene.

The code you currently have is instantiating another block whenever a block is instantiated. This results in infinite self-instantiation - hence the stack overflow error.

1 Like

Perfect, thank you very much I’ll try it later.

Hi, I’ve been very busy, and now that I’m testing it, I don’t know where to put the .instanciate() and how to activate it when I tell it to.

Maybe it’s a stupid question, but I have no experience in cloning.

You can call instantiate() from anywhere except from within the scene you’re instantiating. Put it wherever is convenient.

But how do I make it do the .instanciate() when I want it to do it?

I don’t know if you understand me.

When exactly do you want to do it? What should be triggering it?

When one block is stacked on top of another, it should be duplicated at the top of the screen.

Well then problem is actually how to detect stacking, not how to duplicate a block.

It will work if I create a 2D area that sends a signal when it detects one block on top of another, and then I make the block duplicate itself ?

Sounds like a good idea. Only that it’d be better for the main game logic script do the duplication, precisely to avoid the stack overflow problem we’ve discussed earlier.

1 Like

What is happening to me is that the block is duplicated at the bottom instead of being duplicated at the top as if it were the block at the beginning.