Device clock... do processes keep running when an app is inactive?

tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
edited April 2013 in Working with GS (Mac)
I'm wondering if there is any way to harness the device clock to determine how much time has passed between making an app inactive and then active again.

I was able to make an app keep track of the amount of time between closing/quitting an app and opening it again, and I can certainly track time while an app is active (every 1 second, add 1 second to game.timer), but when I switch apps and come back to the app, I can't figure out a way to keep track of the time that has passed. I can't use a timer because the app isn't active (in the foreground) and when I switch back to the app, my rules don't re-trigger because they've already run.

Am I out of luck?

I tried a simple test:

Change Attribute game.deviceMinutes (real) to game.Clock.Minute
DisplayText game.deviceMinutes --> works "once" when the app is first launched but does not update again until the app has been force-quit and re-opened.
DisplayText game.Clock.Minute --> always shows the current minute of the device's internal clock

Both of those DisplayText behaviors work as I would expect in the iOS Viewer (they both update when the app is made active).

Attached is the very simple project file I used to test this after I figured out what the issue was.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Best Answer

  • RThurmanRThurman Posts: 2,880
    Accepted Answer
    @tatiang -- I don't recall anyone ever asking this before. A brand new question!

    I don't have an answer but can ask a few questions. Is this an issue of the device clock or is it an issue of determining when an app comes back into focus?

    Do processes keep running when an app is inactive? That is -- do things keep moving/spawning/updating when an app is inactive?

    If processes stop when the app is inactive, could you make a pseudo clock by spawning an actor every 10th of a second and have that actor increment a global attribute. Then when the app goes dormant -- and then later reactivates -- it would resume tracking the spawn count where it left off. (Destroy each spawned actor after it has incremented the count.)

    Just some questions and an idea.
«1

Answers

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    @RThurman, I don't actually think it's specific to the device clock, but I'm not sure. I did notice that every x second timers will continue to function after an app is inactive and then active again, but behaviors without timers (I haven't tested to see if "true" rule conditions will also re-trigger) do not. So if I put change attribute game.deviceMinutes to game.Clock.Minute inside of a timer, it will update after being inactivated & reactivated. But without the timer, it doesn't run again.

    And yes, thanks for boiling down my wordy post so efficiently: :)

    "Do processes keep running when an app is inactive?" I think not, but I hope so!

    After I posted this, I tried a variation of your suggestion (I hadn't read your response then, but I'll try the spawn count). I tried updating the device seconds every one second and comparing it to the device clock using load/save attribute behaviors (too confusing to explain it all here!) to see if it increased more than a second or two -- which would indicate that the app had been inactive for a period of time. It didn't work, but I had that same thought, so maybe there is a way.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    @tatiang -- too funny! As I was typing up the spawn idea, I was also thinking about save/load device clock attributes. (It started to get complicated so I discarded mentioning that.)

    I kind of hope that processes don't keep running in the background of an inactive app. For example, if an actor quits moving when the app goes inactive, and then starts moving again when the app is refocused -- then you could use it's position as a time-stamp of sorts.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    If processes stop when the app is inactive, could you make a pseudo clock by spawning an actor every 10th of a second and have that actor increment a global attribute. Then when the app goes dormant -- and then later reactivates -- it would resume tracking the spawn count where it left off. (Destroy each spawned actor after it has incremented the count.)
    So that works fine. The question is how to measure the inactive time. The pseudo clock pauses and then continues but actually that's exactly what a standard every x seconds timer with change attribute does. So I don't think anything is gained through that method.

    Yeah, the save/load attributes thing is making my brain hurt. I feel like I have the right idea but no idea how to perfect it. Time will tell (ha!).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    @RThurman I suspect that the way GameSalad handles an inactive app is similar to the Pause Game behavior.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited April 2013
    @tataing
    Back to the spawn thing. If you record the begining time (at the same time as spawn count = 0). Then when the app resumes, you check the difference between device time and spawn time (er.. count). The difference is the time that the app was inactive.

    Edit: I'll be pretty embarrassed when someone answers with a simple solution to this. (Rather than slapping some rube goldberg workaround together.) Its probably something blindingly easy.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    I don't think it is simple. I've been working on it for about six hours. :( Maybe @CodeMonkey knows!

    But okay, the tricky part is where you say "when the app resumes, you check the difference between device time and spawnStartTime." Sounds good in theory but... how? You can't just say Change Attribute game.elapsedTime to game.deviceTime-game.spawnStartTime. You'd have to wrap it in an every 1 second timer or something. And so how do you know when the elapsed time is due to an inactive app and when its due to time passing while the app is active?

    (re: your earlier point about an actor moving, I did check and an actor interpolating its position will pause and resume movement).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Had a little breakthrough. I decided to use an every 2 second timer to create a timestamp and an every 1 second timer to check the device time against that timestamp. It works! Well, it works some of the time. But at least I know it's possible.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    You could count self time to see when an app comes back. As selftime stops when the app is not active but holds it's current value. So store the self time in a self attribute and you could do the math against your overall time.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    @FryingBaconStudios I appreciate the suggestion. I have a timer that pauses itself when the app is inactive and resumes when reactivated. I didn't find it useful in calculating the elapsed inactive time. The trick is that there isn't a way to know when an app has reactivated/resumed and to then compare values.

    Edit: hmm, maybe I can use that after all.

    When attribute self.Time < game.timeStampCurrent - game.timeStampStart - [some number of seconds]
    .....[the app was reactivated]

    The way I've started thinking about it is this:

    Imagine you have an app that pauses itself at random times and then unpauses itself at random times. You have no access to the pause/unpause behaviors to know when they occur. How do you determine how long the app is paused for?

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273
    I think I created a file about this a long time ago. I'll see if I can dig it up.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    I'm working on an actor that uses self.Time (thanks @FryingBaconStudios) and spawns and destroys itself (thanks @RThurman) in order to reset self.Time. I'll post a demo when/if it works.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    Thanks for the hint @FryingBaconStudios. Based on that, I cobbled something together that works with pause scene. I'm not in a position to test it and see if it works on a device at the moment. (Pause scene will work. But will it work the same when suspending/resuming an app.) Anyway here it is.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    @RThurman the version I've been working on is similar to yours. But when I try to respawn the actor to reset self.Time, it freezes up the app for quite a few seconds and then finally displays the correct amount of elapsed time. I think I'm going to go back to a manual timer that I can reset so that I don't have to do any spawning.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited April 2013
    @tatiang -- the file I posted does have a spawn in it. But it's purpose isn't to actually help in timing. It (the spawned actor) is simply being used as an alert for debugging purposes.

    I should have explained better about the file. I scrapped the previous idea (of spawning timers) in favor of @FryingBaconStudio's great hint. He indicated that self.time pauses when an app suspends. He said to compare self.time to overall time (um... game.time) to see when discrepancies arise. I assumed that he meant that any discrepancy between self.time and game.time could supply two pieces of information: 1) that there was a pause --or an inactive period, and 2) how long that inactive period was.

    The file I submitted does show that this is true when a game is officially paused. My question is -- does it also work when an app is suspended and reactivated? I am not currently in a position to do an adhoc test to see if works when an app is suspended and then resumes. I was hoping someone could test it out.
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    edited April 2013
    Yes I was doing testing on my game as to self.time as i was having problems with the pause menu funtion and that is how I know selftime starts then restarts from point left off. Glad it came in handy.

    @RThurman that was exactly what I was thinking.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    @RThurman Sorry, I didn't test it yet. It was almost identical to the version I made that does work so I assumed it would work. But I think I have a foolproof (ha!) algorithm now and will upload the demo shortly.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    @RThurman I tested it and the Time Discrepancy Alert doesn't appear after resuming the app.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited April 2013
    @RThurman I tested it and the Time Discrepancy Alert doesn't appear after resuming the app.
    In viewer or ad-hoc? (It shouldn't work in viewer. But I don't know about ad-hoc.)
    Thanks for taking time to test it out.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    Here's the algorithm:

    Set deviceStart to # of seconds since January 1, 2013
    Constrain deviceNow to # of seconds since January 1, 2013
    When selfTime < deviceNow - deviceStart - 1 + timeStamp
    .....Amount of inactive time elapsed = deviceNow - deviceStart - (self.Time-timeStamp)
    .....Change timeStamp to self.Time
    .....Change deviceStart to # of seconds since January 1, 2013

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    @RThurman Oh sorry, yours doesn't work in adhoc.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited April 2013
    Here's the algorithm:

    Set deviceStart to # of seconds since January 1, 2013
    Constrain deviceNow to # of seconds since January 1, 2013
    When selfTime < deviceNow - deviceStart - 1 + timeStamp
    .....Amount of inactive time elapsed = deviceNow - deviceStart - (self.Time-timeStamp)
    .....Change timeStamp to self.Time
    .....Change deviceStart to # of seconds since January 1, 2013
    Now that looks like an elegant solution!
  • HoneyTribeStudiosHoneyTribeStudios Member Posts: 1,792
    If I understand correctly you want to know the difference in time from:
    When someone completely quits an app i.e when it is no longer running in background.

    To:
    The next time they open the app.

    Is that right? If so they have to come to the first scene when launching the app from being closed. So you could add some attributes that track when the first scene to be opened is scene 1. (so they have not navigated there from another scene)

    e.g when going from scene 2 to scene 1:
    change game.CameFromScene2 to true.

    and in scene1 (in one of the last layers):
    change game.TimesOnScene1 to game.TimesOnScene1 +1

    Then if game.CameFromScene2 is false
    And game.TimesOnScene1 is 0

    You know they have come to scene1 because the app was launched, not because they navigated there from any other scene.

    So at that point you could could take snapshots of the device clock, save them and compare the difference.

    Unless I have misunderstood?
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    @HoneyTribeStudios. No. I have that figured out and it was fairly easy. I want to know when someone switches to another app and them returns to this app how long it has been since they switched apps.

    Launch app -----> switch -----> resume -----> switch -----> resume

    Between the switch and the resume above is the time I'm interested in. I think I have it... stay tuned. :)

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited April 2013
    And here is the file. I made a small change to the algorithm (now edited above).

    Thanks guys! I have a headache now. @-)

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    @tatiang -- Thanks for starting a really fun/interesting discussion -- with a totally novel question! It was fun to think about this. Even when, in the end, the spawning idea turned out to be a total bust.

    And thanks for posting a nice solution to your own question. I think this will come in handy for many GS developers.

    I wonder if anyone will be able to develop more optimized solution? Let the contest begin!
  • BasGoodboyBasGoodboy Member, PRO Posts: 310
    edited July 2014

    Okay, sorry for digging this one up, but it's a nice thread and it's exactly what I need for my new game.

    I used the template @tatiang made. It's very good and works adhoc when inactive and using an other app for example, but my main question is, and I really can't seem to find any thread where the answer lies.. what if the app gets closed down completely and restarted. The way @tatiang made it.. it will not count inactive seconds, this will stay 0.

    Very curious for this one since I want my hero to lose life energy when people close this app and come back after for example 24 hours.

    I was thinking like. A save time attribute every minute in the game.
    When game starts > load time attribute. Make a rule that says current time attribute - loaded time attribute = certainnumberhere. And make certain rules that comply to this certainnumberhere, like for example when someone is gone for 22 hours.

    When attribute currenttimeattribute-loadedtimeattribute = 22, attribute energy - 50.

    But these rules make me rather dizzy then really giving me comfort, not sure if Im thinking right. Not such a math wonder.

    Goodboi Gamestudios | Monster Jumper now on the App Store

  • ArmellineArmelline Member, PRO Posts: 5,369

    Unless I'm mis-remembering, @tatiang's template does exactly what you're after @BasGoodboy. It doesn't matter if the game is inactive or completely closed.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Correct, @Armelline, my template counts both the time that has elapsed during app use and the time elapsed while the app is backgrounded or closed. The file I posted above is incomplete, though, since it was an early attempt at all of this.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • BasGoodboyBasGoodboy Member, PRO Posts: 310
    edited July 2014

    @tatiang do you maybe have a complete file you would like to share? I would like to use it as a basic for my new app, sort of tamagotchi thing, where the hero actor will lose life when not on the app for like some hours.

    EDIT: I noticed this is a template in your shop.

    Goodboi Gamestudios | Monster Jumper now on the App Store

Sign In or Register to comment.