How do you stop something from duplicating and reduce lag

Godot 4.5

I’m making a game where you can duplicate/split.
Is there a way I can limit the duplicating and how to reduce lag.
This game is about mitosis.

Code:
if Input.is_action_just_pressed(“mitosis”):
var copy = duplicate()
get_parent().add_child(copy)

You could use a static variable to keep track of how many duplicates there are

static var count_duplicates: int = 0

func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_pressed("mitosis") and count_duplicates < 1000:
        count_duplicates += 1
        var copy = duplicate()
        add_sibling(copy)

I just fly sometimes

is there a way to fix it

Could you explain further? Fix what? Can you example what you expect to happen versus what really happens?

On the second input I just start flying

Did that happen before? maybe you should move the clone slightly away?

func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_pressed("mitosis") and count_duplicates < 1000:
        count_duplicates += 1
        var copy = duplicate()
        copy.position.x += 50 # assuming 2D
        add_sibling(copy)

Some of the clone are still flying in the air

It copying but it copying on each other

Also It spawning in walls

maybe you will need to use a random value for the nudge, tough to say how you’d like to resolve this collision but it’s up to you

how would I make it a random value

Instead of the 50 value I gave you could use randf_range(minimum, maximum). same for a position.y addition.

could there be a way to change to position by 5 for the first input than 10 for the next

Like get some helium

I’m pretty new

Hey there! This is a community forum! People here donate their free time to try and help out others! With that said, please do not spam / bother someone if they don’t reply to you. They’re not being paid in any way to help you out.

my bad

Yes you could multiply the offset by count_duplicates, but I don’t think it would solve the issue there still may be a duplicate that happens to be in the same position.

I don’t understand what this means

Get some helium is a game