Whenever a timer increases with 10 spawn object

In the update function in one of my script files i have self.time = self.time+1 so it should increase with 1 each frame and i wonder if it possible to have that each time self.time has increased with 10 a game object should be spawned?

So you want to spawn an object every 10 frames?

First I would recommend doing self.time = self.time + dt since each frame might have a different frequency, this will allow you more regularity.

Then you can do:

If self.time >= 10 then
 -- spawn here with factory.create(...) for example
self.time = self.time - 10
end

Hope it helps!

1 Like

thanks thats a good idea! i tried it but my factory doesnt spawn anything for some reason, i dont know why it doesnt work :confused:

i just tried adding a print(self.time) and i guess it should print 10 but it doesnt print anything at all :o

Did you maybe forgot to attach the script component to your object?

1 Like

No i have my zombie factory and my rocket.script in my rocket.go so i think that isnt the error

Does a print in the init shows something?

Is it zombies you want to spawn every ten frames? What GO is your factory attached to? It’s a script attached to this same GO that should have the print in the init.

Its just that I’m frequently creating new script and forgetting to attach them to GO and spending 30min trying to figure out why it doesn’t work. So I know it’s an easy mistake to make :smile:

haha okay :smile: I want to spawn zombies as the player live longer and longer, both the script and factory is attached to my rocket.go , i wrote print(100) in init in rocket.script and it showed so that seems to work atleast

anyone else have any ideas whats wrong?:slightly_smiling_face:

Please share the entire script so that we can take a look.

timer.delay(10, false, function()
    for i = 1, 10 do
        factory.create("#zombies")
     end
end) 

Above should spawn 10 objects of ā€œzombiesā€ factory every 10 second. Make sure your factory have a correct prototype. If you are using position in the ā€˜create’ function, maybe you want to use get_world_position?
But if the print statement isn’t showing there must be something else

2 Likes

Hello, i’ve managed to spawn zombies with my factory now, i had to create a new go and new script and chose zombie.go as prototype, so for some reason it didnt work when i used rocket.script even though it was connected with the factory and i used the samed code :o