[SOLVED] Spawn entity at certain time

How to spawn entity at a certain time, I used if this.timer > 30
Spawn entity, but this kept spawning it over and over again

Hi @David_Warren,

Use a second check together with the timer:

if(this.timer > 30 && this.spawned === false){
   this.spawned === true;
   // spawn your entity
}

ok thanks, where do i implement that code?

var Spawner = pc.createScript('spawner');


// initialize code called once per entity
Spawner.prototype.initialize = function() {
    this.Spawner = 0;
    this.isSpawnerActive = false;
    
    Spawner.prototype.beginSpawner = function(){
        this.isSpawnerActive = true;
        
    };
};

// update code called every frame
Spawner.prototype.update = function(dt) {
   
    
    this.Spawner += dt;
    
    if (this.Spawner > 10) {
         var Monster1 = this.app.root.findByName('Monster1');
    var F = Monster1.clone();
    Monster1.parent.addChild(F);
        F.rigidbody.teleport(-12, 1, -4);
        
        
    }
     
};

Something like this would work:

var Spawner = pc.createScript('spawner');


// initialize code called once per entity
Spawner.prototype.initialize = function() {
    this.Spawner = 0;
    this.isSpawnerActive = false;
};

Spawner.prototype.beginSpawner = function(){
    this.isSpawnerActive = true;  
};

// update code called every frame
Spawner.prototype.update = function(dt) {
   
    
    this.Spawner += dt;
    
    if (this.Spawner > 10 && this.isSpawnerActive === false) {

        this.beginSpawner();

         var Monster1 = this.app.root.findByName('Monster1');
    var F = Monster1.clone();
    Monster1.parent.addChild(F);
        F.rigidbody.teleport(-12, 1, -4);
        
        
    }
     
};

No Luck, its as if there’s an error but its not triggering the debug system

Try debugging your update loop either with the browser debugger or using console.log statements.

  • Does this.Spawner timer increases in value?
  • Does your if statement eventually turns to true?

Also a good practice is to use meaningful names for your variables/properties, this.Spawner is awfully similar to your Spawner script name. Maybe use something like this.spawnerTimer for that.

yea the timer value increases, but im not sure if the statement eventually turns to true, do you think just resetting the timer after 10 sec would make it spawn 1 single entity?

If you do that you will be spawning a single entity every 10 seconds, if that’s ok with your gameplay do that.

If you keep getting errors or wrong logic, try posting a project url to take a look.

the wall has the spawner script lol, the enemy is suppose to clone and respawn but nothings happening, and space to schoot

You haven’t really transferred all code I’ve pasted above to your project. Update your update method to the following (read carefully line to line!):

Spawner.prototype.update = function(dt) {
       
    this.Spawner += dt;
    
    if (this.Spawner > 5 && this.isSpawnerActive === false) {
         
         this.beginSpawner();

         var Monster1 = this.app.root.findByName('Monster1');
    var F = Monster1.clone();
    Monster1.parent.addChild(F);
        F.rigidbody.teleport(-12, 1, -4);       
    }
     
};

thats what i did at first but it still did not work so i just started changing things :sweat_smile:
ok i changed it back to the original now it works, thanks!

1 Like

quick question, if i wanted to make another 1 spawn at 15 seconds how would i do it, this code i used seems to not work.


var Spawner = pc.createScript('spawner');


// initialize code called once per entity
Spawner.prototype.initialize = function() {
    this.Spawner = 0;
    this.isSpawnerActive = false;
};

Spawner.prototype.beginSpawner = function(){
    this.isSpawnerActive = true;  
};

// update code called every frame
Spawner.prototype.update = function(dt) {
   
    
    this.Spawner += dt;
    
    if (this.Spawner > 5 && this.isSpawnerActive === false) {

        this.beginSpawner();

         var Monster1 = this.app.root.findByName('Monster1');
    this.F = Monster1.clone();
    Monster1.parent.addChild(this.F);
        this.F.rigidbody.teleport(-12, 1, -4);
        
        
    }
    
    if (this.Spawner > 15 && this.isSpawnerActive === false)  {
        
         this.beginSpawner();
    
        var Monster2 = this.app.root.findByName('Monster1');
        
       
        this.G = Monster2.clone();
        Monster2.parent.addChild(this.G);
        this.G.rigidbody.teleport(-12, 1, -4);
        
    }
    
};

Do something like this:

var Spawner = pc.createScript('spawner');


// initialize code called once per entity
Spawner.prototype.initialize = function() {
    this.Spawner = 0;
    this.spawnedMonster = 0;
};

// update code called every frame
Spawner.prototype.update = function(dt) {
    
    this.Spawner += dt;
    
    if (this.Spawner > 5 && this.spawnedMonster < 1) {
         this.spawnedMonster = 1;
         this.Spawner = 0;

         var Monster1 = this.app.root.findByName('Monster1');
    var F = Monster1.clone();
    Monster1.parent.addChild(F);
        F.rigidbody.teleport(-12, 1, -4);        
    }
     
    if (this.Spawner > 15 && this.spawnedMonster < 2 )  {
         this.spawnedMonster = 2;
         this.Spawner = 0;
    
        var Monster2 = this.app.root.findByName('Monster1');        
     
        this.G = Monster2.clone();
        Monster2.parent.addChild(this.G);
        this.G.rigidbody.teleport(-12, 1, -4);        
    }
};

your a genius, it worked!

1 Like