[SOLVED] Duplicating entity

Hey guys i would like to know the way of cloning any entity.

I want my object to be spawned every 2 seconds on the same position, object has moving script so they wont be stacked

Cloning :

var entity = this.app.root.findByName('TheObject').clone();



If you are doing this you need to spawn a new instance each time the interval callback is called, otherwise you will get an exception that the entity is already parented.

  var entity = this.app.root.findByName('TheObject').clone();

Thank you guys, although setInterval is not working as supposed, when 2 seconds past, object is spawning infinitly without delay, im just gonna use timer to set it up.

looks like when i clone parent it is cloning all children as well so each time it spawns more objects :stuck_out_tongue:

How to teleport cloned object on the position I set?


EDIT: I have just rode that object needs to be added manualy in the hierarchy so thats why its not showing on the map, any way, parent has to be something different than the original object being cloned

The child entities are part of the parent so it seems obvious to me that they are cloned along.

I am curious why setInterval() does not work as expected.

Make sure to put the setInterval on your initialize method, not in your update.

Eventually you will want to drop using setTimeout/setInterval, it’s better practice to implement your own timer using the update(dt) callback.

Ah oké, so you have to call it once. How can you stop it?

Both setTimeout and setInterval return a pointer that you can use to stop it at any point.

var interval = setInterval(function(){
  var entity = this.app.root.findByName('TheObject').clone();

// later
1 Like

Hey @Leonidas, i now received error that graphnode is already parented


with setinterval was fix to .bind(this), how can i do that in this case? :slight_smile:

Yes because you aren’t adding to the hierarchy the clone, but the original entity. Which is already parented and if you try adding it again will throw an exception.

Change part of your update method to:

var instance = this.objectToSpawn.clone();

The update method returns a new entity instance which you need to store in a variable, check the docs:



Thank you so much, also it seem that my cloned object doesnt contain a script, do i need to enable it ?

This is the script that original object has attached on

The clone() method is a deep copy of the entity, all of the entity’s components will be copied and furhermore any children entities this entity may have.

Not sure why you are getting this behavior. Try checking in the console with console.log(instance); if the entity contains the components.

i have noticed it is working for few seconds but once new clone is created it is being teleported to the spawn position, you can check the game:


EDIT: script works until new object is created

Yes, your script isn’t working because you are trying to use entity translation methods on dynamic rigid bodies. That won’t work.

Movement of these bodies is controlled by the physics simulation. You would have to use either teleport or try moving them using forces or by setting their linear velocity. Check the first person movement tutorial on how that works:


Yes i know it is wrong, it was just because applyForce isnt working properly and i dunno why…

i have changed translate line for this: this.entity.rigidbody.applyForce(10, 0 , 0);

but i need you to check the behavior of the object in the game

EDIT: i found out that applyForce is working when it is falling, but once it hit the ground it stops

Yes it stops because the force will be applied only once, when you execute that command. If you would like to get it moving then you should apply the force in each frame, in your update() method.

Check the FPS example I posted above, it explains that accurately.

Another option is to use Kinematic rigid bodies, which you can control with setPosition/translate etc. and it will still report collisions. It may work in your case, check the manual (bottom of the page):


applyForce line is in update function so it should be giving force each frame right? i will take a look at the manual

Then if it doesn’t move try increasing its value to 100 or 1000.