How to destroy an actor?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How to destroy an actor?

      In the TeapotWars, all the actors are created by the BaseGameLogic. The BaseGameLogic has strong pointers to each actor it created. Therefore, to truly destroy an actor, we can't just call actor.Destroy();; we have to remove the strong pointer from the BaseGameLogic.
      The TeapotWars provides an event namely EvtData_Request_Destroy_Actor for destroying actors. We can simply create and dispatch this event, then the BaseGameLogic will be triggered by it and will do the clean up.
      But what makes me confused is a comment in the Events.h:
      //---------------------------------------------------------------------------------------------------------------------
      // EvtData_Request_Destroy_Actor - sent by any system requesting that the game logic destroy an actor
      // FUTURE WORK - This event shouldn't really exist - subsystems should never ask the game logic to destroy something through an event, should they?
      //---------------------------------------------------------------------------------------------------------------------
      My questions are:
      - Why subsystems should never ask the game logic to destroy something through an event?
      - What is the better way to ask the game logic to destroy something?
    • Sorry, I was on vacation for the last week and not really keeping up with the boards.

      babygogogo wrote:


      - Why subsystems should never ask the game logic to destroy something through an event?
      - What is the better way to ask the game logic to destroy something?

      I don't agree with this. I think it's perfectly fine for anything on the Logic layer (like Lua) to destroy an actor and I think an event is probably the best way to do it. In my own engine, the official way to destroy an entity (my equivalent to actor) is with an event. There are a few paths to this (like calling DestroyEntity() from the ObjectManager in Lua), but they all end the same way. It spawns the event.

      The advantages of using an event are as follows:
      1. You decouple the destruction request of that actor from the system that manages all the actors. In other words, you don't have to dig through to find the Logic instance, you just spawn the event.
      2. Using an event allows you to go through the exact same path regardless of whether it's coming from script or from C++.
      3. The destruction of the actor is deferred, so you don't have to worry about destroying the actor from inside some code that relies on it. Consider the worst case: you destroy the actor from inside a function that exists on one of that actor's components. This is actually pretty common. Imagine the case where you have a pickup that you want destroyed when you detect collision with the player. If you did this with a single synchronous function call, you'll invalidate the memory all around you and crash. Using an event, you are guaranteed to process the destruction when the actor (and its components) are not in the call stack anywhere.
      Hope that clears it up.

      -Rez

      PS: I can't remember who actually wrote that comment. I might have even been me. ;) Still, I think it's incorrect and that you should actually PREFER to use an event.