How to work with the device clock...?

RUPASRUPAS Member Posts: 823
edited June 2012 in Working with GS (Mac)
The specific question is this; That rule I have to create, so that every 2 minutes, add 100 coins in the shop of my game? Worked well although the game is closed?

Answers

  • ORBZORBZ Member Posts: 1,304
    Are you talking about while the game is closed or while the game is running or both?
  • ORBZORBZ Member Posts: 1,304
    edited June 2012
    Well, i'm going to asume you mean offline when the app is closed because that's the more interesting discussion. As online while the app is open is as simple as a timer every 120s.

    So offline goes like this:

    First of all, working with time deltas without epoch seconds (GS I'm looking at you!) is rather tricky.

    While the app is running you will need to save the current epoch time every time the 100 gold is deposited and the total gold accrued using the Save Attribute behavior. This will be done every 2mins while the game is running, basically any time you add money to the shop. However, saving the time is a bit tricky because GS doesn't give you epoch seconds. So you need to compute it: (Damn you GS why don't you give us epoch seconds?!, it's just a standard system function call on your end, it's a bunch of work on ours)

    Computing epoch seconds, in essence, works like this: You pick a moment in time as the epoch. Say Jan 1, 2000 00:00:00 (midnight). Then you compute the total number of seconds since that moment. Where it gets a bit tricky is dealing with leap year and the number of days in a given month.

    So anyhow, here is an example of the math involved in computing the time in epoch seconds:

    http://www.esqsoft.com/javascript_examples/date-to-epoch.htm

    Save the current_epoch_seconds and total_gold using the Save Attribute behavior

    While the game is off nothing will happen because the game is off.

    When the game boots back up you will need to load the saved epoch seconds into a variable called saved_epoch_seconds, you'll also need to load the saved total_gold value.

    Then you need to compute the number of seconds between the current time and the saved time. You do that by computing the current_epoch_seconds and subtracting the saved_epoch_seconds from that. we'll call this delta_seconds

    delta_seconds = current_epoch_seconds - saved_epoch_seconds

    Then divide the result by 120 and ignore any fractional component to figure out how many whole 2 minute intervals have elapsed, we'll call this variable elapsed_intervals:

    elapsed_intervals = floor(delta_seconds / 120)

    Finally compute your new total_gold value:

    total_gold = total_gold + 100 * elapsed_intervals

    Then save the current_epoch_seconds and total_gold again again just in case they turn the app off.

    Poof, it's like your app was magically running when it was offline.

  • RUPASRUPAS Member Posts: 823
  • games4fungames4fun Member Posts: 185
    Im looking for something like this as well. Are there any templates that show this?
    Also how can we add a time counter to display the time left in seconds till the gold is given lets say.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    Hey! This looks like a fun. I'd like to make a "GS Epoch Calculator". Does anybody know the exact date and time that GameSalad Creator was launched? Its only fitting that we start counting the seconds from the moment that it was brought forth into the interwebs. (The next epoch can start when GS gains artificial intelligence or joints -- whichever comes first.)
  • dj_outlaw2000dj_outlaw2000 Member Posts: 11

    @ORBZ said:
    elapsed_intervals = floor(delta_seconds / 120)

    Finally compute your new total_gold value:

    total_gold = total_gold + 100 * elapsed_intervals

    Dear ORBZ

    ive been stucking in this problem for 3 weeks until now I cant even solve it and thanks to you that you gived this idea and explaination witch I absorve it in my way and fit it in my game... but my case is not coins like what has been discussed here... what I need is...

    I have a game that have 5 lives and every time you lose the level you will lose 1 life, and if the lives is lower that 5 I want it to be increased 1 life every 30 minutes of the DEVICE CLOCK even if the app is open or close until its reaching 5 lives and then stop increasing...

    so I used this rule you post with a little change to get the 30 minutes as 1800 seconds witch is...
    elapsed_intervals = floor(delta_seconds / 1800) to increase the lives every 1800 and it workds...

    now the problem I have is... when the elapsed_intervals witch is Lives = 5 I want the counting to stop!
    I put a rule to constrain attribute saved_epoch_seconds to current_epoch_seconds to keep resetting the saved_epoch_seconds to stop the counting and it work and the counting stopped and reset...
    but another problem appear that the Lives is resetting also...

    I don't wanna make it long on you guys but what I need is how to change the rule you give to fit it for the lives attribute to increase 1 live every 1800 seconds and stop when reach 5... and every time it goes bellow 5 to start counting 1800 again to reach 5 and stop again... and go on and go on....

    and buy the way I use this rule to calculate the saved time witch is (saved_epoch_seconds) when closing the game by seconds using the device clock... the rule is in one line no spaces...

    (game.clock.year-2011)36524+floor((game.clock.month-1)30.5)2460+(game.clock.day-1)2460+game.clock.hour60+game.clock.minute*60+game.clock.second

    guys anyone know how please help coz im waiting for your urgent reply...
    Thanks

  • dj_outlaw2000dj_outlaw2000 Member Posts: 11

    the rule changed when I post it so I will rewrite it again in a better understandable way... its...

    (game.clock.year-2011) X365 X24+floor((game.clock.month-1)30.5) X24 X60+(game.clock.day-1) X24 X60+game.clock.hour X60+game.clock.minute X60+game.clock.second

  • dj_outlaw2000dj_outlaw2000 Member Posts: 11
    edited March 2015

    any help guys? any hint :p
    I tried a lot of things and nothing works

Sign In or Register to comment.