Rick Posted February 7, 2014 Share Posted February 7, 2014 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. Quote Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2014 Author Share Posted February 7, 2014 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 Quote Link to comment Share on other sites More sharing options...
Josh Posted February 7, 2014 Share Posted February 7, 2014 What script function are you trying to release them in? Quote 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 More sharing options...
Rick Posted February 7, 2014 Author Share Posted February 7, 2014 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted February 7, 2014 Share Posted February 7, 2014 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. Quote 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 More sharing options...
Rick Posted February 7, 2014 Author Share Posted February 7, 2014 cool thanks! Quote Link to comment Share on other sites More sharing options...
YouGroove Posted February 7, 2014 Share Posted February 7, 2014 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) Quote Stop toying and make games Link to comment Share on other sites More sharing options...
Rick Posted February 7, 2014 Author Share Posted February 7, 2014 I believe releasing an entity should call it's scripts Delete() function, which then we can handle releasing other things the scripts created ourselves. Quote Link to comment Share on other sites More sharing options...
Rick Posted February 8, 2014 Author Share Posted February 8, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.