Can I delete Game Object by its sprite url? (SOLVED)

I make a Game Object called fx_slash.go that contains a sprite component (#sprite) which will play animation slash. It’s a 4-frames flip-book animation.

And my enemy.go contains a factory component (#factory_fx_slash) that set its prototype to fx_slash.go

Now I want to play a slash fx and then after the animation done do delete the whole game object (a game object spawned from fx_slash.go) not just its sprite component.

Is this following code acheived what I want? The slash fx is gone from the scene but I don’t know if the game object is still living in the dark or not.

enemy.script

local function play_fx_slash(self)
    local pos = go.get_position(".")
    local fx = factory.create("#factory_fx_slash", pos)
    msg.post(msg.url(nil, fx, "sprite"), "play_animation", {id = hash("slash")})
end

function on_message(self, message_id, message, sender)
    if message_id == hash("play_fx_slash") then
	play_fx_slash(self)
    elseif message_id == hash("animation_done") then
	go.delete(sender) -- HERE, is the whole game object get deleted or just its sprite component?
    end
end

Yes, doing go.delete(sender) will delete the whole game object and not just the sprite. You can easily verify this by adding a script component to fx_slash.go and print() something in it’s final() function. final() will be called when the game object is deleted.

You could also have a play_and_delete.script that encapsulates this behaviour and attach it to game objects that should play an animation and delete itself when the animation is done. Post a message to this script and let the script play the animation and wait for the animation_done message and when that happens do go.delete(). I think this solution is better because what happens if the enemy.go is killed and deleted before the fx_slash.go animation has completed? In that case you’ll never get an animation_done in the enemy.script and you’d end up with a lingering fx_slash.go object.

OK, that makes sense.

And thank you for your idea about play_and_delete.script that will prevent the lingering fx_slash.go object in your scenario.

Thanks :smile: