Static Variable

Would it be possible to use static variable in order to count how many towers are left? This is how I would see it:

  1. in Tower.h we add static varibale int32 called “counter” initialized to 0
  2. in Tower.cpp in the constructor we add a line of code which is incrementing that variable by 1
  3. in Tower.cpp in the destructor we add a line of code which in decrementing that variable by 1
  4. in Tower.cpp Tick (called every frame) function or GameMode.cpp in HandleHameStart() function we would add code that is checking if our static variable is <= 0, if yes then GameOver(true) would be called

Wonder if that is possible and what are pros and cons of that solution in comparison to the one presented in the course.

1 Like

The constructor and destructor aren’t called when you think they are and would result in incorrect numbers. The static member variable would also persist for the entire duration of the program i.e. between levels, end games, main menu, etc.

To do what you’re suggest would be to store this as a non-static variable in the game mode then you would have a AISpawned function that is called from the AI controller to go alongside the actor died function to update the count.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms