Jump to content

What's the recommended way to release entities during gameplay?


Rick
 Share

Recommended Posts

I have bombs and I want to remove/release them from the scene when they collide with the player. I can't do self.entity:Release() in the bomb script as it crashes the engine. I can't call entity:Release() in the player collision function because it crashes the engine.

 

I suppose I could probably set a flag key for this entity and in the App:Loop() loop over all entities in the scene and release ones that have this flag set, but there really should be an easier way to handle this inside entity scripts. Maybe some kind of delayed release function that won't release the object right then and there but instead wait until after all the scripts are called and then release objects that should be released before returning from world:Update()?

 

There has to be a better way that I'm missing. Hiding can't be the best option.

Link to comment
Share on other sites

I can't seem to call Release() on entities that have some kind of flag set to be released in App:Loop() while looking over the world entity list. In the below script, the x variable is 25 when it releases that entity, and when it's 42 it blows up because the entity is nil. There are 51 entities total in my scene. So not sure why it's blowing up on the 42nd entity. I could check for nil but clearly something gets screwed up when Release() is called in a loop like this.

 

function App:ReleaseEntities()
   local count = App.world:CountEntities() - 1
   for x = 0, count do
       local entity = App.world:GetEntity(x)
       local release = entity:GetKeyValue("release", "false")

       if release == "true" then
           entity:Release()
       end
   end
end


function App:Loop()

   self:ReleaseEntities()

   -- all the other stuff here
end

Link to comment
Share on other sites

It doesn't seem to matter what script does the releasing the game crashes. If I release in the bomb collision when it collides with the player with self.entity:Release() the game crashes. If I try releasing the bomb from inside the player collision with entity:Release() (entity being the parameter passed in by the collision) the game crashes.

Link to comment
Share on other sites

I'm adding a call to GCSuspend and GCResume before and after the physics update. This should prevent entity deletion from crashing the engine inside a collision script function.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Would it be possible ot have one call to a function, that would itself destroy the entity without any impact on collisions ?

 

If the entity uses some Lua ressources like sounds etc ...calling Release() does it releases also all object referenced by it's script ?

 

(Ususally we called some Clear() for Lua tables)

Stop toying and make games

Link to comment
Share on other sites

This seems to be working now. Although it means I have to rethink how to structure things. In my bomb script I play the explosion sound when collided, and I also release itself. This causes the sound to not play anymore (since the object gets deleted). I might have to just set a flag and then not release the object until the sound is finished playing. Though I remember you said something about this being specific to the collision function so guessing I won't be able to release itself in the update function.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...