1

How do you repeatedly check whether a variable has changed?

Currently I'm using a hook class from a game engine to access the score like so:

 float score = ScoreController::scores().points.current();

The function is only called once from the AppDelegate, so I need a method to continuously check if the score then changes after I find out what it is, but I'm unsure how to approach this?

I'm thinking the logic would be:

 //store current score (as above)
     //if score increases (not sure how to check)
          //do something

Thanks for your help.

Reanimation
  • 3,151
  • 10
  • 50
  • 88
  • What causes the score to increase? Can whatever that is send a notification or execute a callback to inform you of the change? – Phillip Mills Jun 20 '18 at 20:35
  • Unfortunately due to the nature of the game engine, the only variable you can check is the current `points` as shown above. – Reanimation Jun 20 '18 at 20:38
  • So, nothing in your code causes the current points to change? Sounds odd...but, OK. – Phillip Mills Jun 20 '18 at 20:44
  • I'm using a hook class to access the Scores created in the `ScoreController`, and the only variable made available by the Game Engine is the `points.current`. I can not access the Game Engine code. Otherwise I wouldn't need to use their hook class. – Reanimation Jun 20 '18 at 20:51
  • You can use [gcd as in this answer](https://stackoverflow.com/a/14582980/102315) to check the points in a loop. – Alper Jun 21 '18 at 13:16

1 Answers1

0

Try using NSTimer.

NSTimeInterval timeInterval = 1; //How frequently to check for the score change.
CGFloat score = ScoreController::scores().points.current();
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval repeats:YES block:^(NSTimer *timer) {
     CGFloat newScore = ScoreController::scores().points.current();

    if (score != newScore) {
        //Score changed. Call a delegate or something.
    }
}];