Changing Strategy

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

    • I need a little more context here.... what's a strategy in this context? It looks like it's something that manages Win32 messages, but something needs to call into it. Are you changing how you handle messages in the middle of the game? If so, that's the thing I'd change. You shouldn't need to change how you handle Win32 messages. Why do you need to do this?

      -Rez
    • You really shouldn't be using Win32 messages for this sort of thing. You should have a layer that translates the Win32 message into something usable. All your Strategy should do is translate the events coming in from the Win32 messages and send key events from that.

      But, back to the problem at hand....

      The easiest thing to do is to simply not destroy the strategies. Instead, you keep an array of these strategies indexed by that enum along with a pointer to the active one. That way it doesn't matter where you set the strategy, even if you're inside a function already.

      Brainfuck Source Code

      1. //------------------------------
      2. // StrategyType
      3. //------------------------------
      4. enum StrategyType
      5. {
      6. MAIN_MENU = 0,
      7. GAME_SCREEN,
      8. DEATH_MENU,
      9. NUM_STRATEGY_TYPES // [rez] I added this
      10. };
      11. // Here's your array. You would create these in you initialization method
      12. // or the class constructor and destroy them in the class destructor. They
      13. // never change.
      14. Strategy* m_pStrategies[NUM_STRATEGY_TYPES];
      15. // This is the current strategy. Initialize it to something appropriate.
      16. Strategy* m_pCurrentStrategy;
      17. // Here's the function to set the strategy, which is callable from anywhere.
      18. // I don't know where this lives, so I'm just using ActionManager since I
      19. // see in your callstack. Slot this in where it's appropriate.
      20. void ActionManager::SetStrategy(StrategyType type)
      21. {
      22. m_pCurrentStrategy = m_pStrategies[type];
      23. }
      Display All


      Does that make sense?

      -Rez
    • Originally posted by kaykry
      For the current frame, let's say I would normally do ten collision checks. In the first collision check, I find out that the player hit an enemy and died.

      Now I'm still going to pointlessly go through the other 9 collision checks, process any game events, and update the actors/components even though the game is over.

      So? If the player didn't die, you'd have to do that anyway. You're not really saving any performance. Unless those collisions have the possibility of changing gamestate after death and creating bugs, it's not worth worrying about.

      -Rez