Time to live

To give an object a finite life-span you could use the following code:

function init(self)
    self.t = 2 
end

function update(self, dt)
    self.t = self.t - dt 
    if self.t < 0 then
        go.delete() 
    end
end
What does this code do?

t is an attribute that holds the time left to live. This is initialised to 2 when the object is spawned.

In each frame the time to live is reduced by the delta time to ensure consistent timing regardless of the number of elapsed clock cycles.

Once the time to live is below zero the object is despawned.

Note, it is important to delete objects you want to despawn so they are not taking up additional memory and silently running their update event. There are a maximum of 128 objects in a typical Defold game to ensure frame rates don't drop.