How do I count time with time stamps

  • Lets say I have Stamina bar, whenever I press Attack button, it subducts the stamina variable by 1. The refill of stamina being performed by 1 every 2 minutes.

    What must be done to make it refill even when the user off-line?

  • Store the value and the date&houre&seconds right before gooing offline.

    When back online, get the value and recent date&houre&seconds and calculate the off time.

    Set value according.

  • I think it is easier to calculate the difference.. if you use Date.now()

    [quote:taxz262h]The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00

    [quote:taxz262h]1 Minute =

    60,000 Milliseconds

    you can simply set variable with: Browser.ExecJS("Date.now();")

  • Thanks for the suggestions however I've faced some awkward issue.

    I've prepared an illustration for the better understanding of the problem.

    When ever I press the attack button on the bottom left, it deducts from stamina 2 points, then there should be a timer that starts counting 2 minutes after which there will be refilled one point and make it 99/100.

    This is where the issue occurs, whenever I try to take the current time using the Browser, I take the time when this action was made, add to it the 2 minutes in milliseconds, and perform another event check if this value is < than the current time, then there should be an increase of stamina by 1.

    Following this method, if i'll have for example 67/100 points, i won't be able to see 68/100, 69/100, ..., n/100 it will simply jump to 100/100 once the total time has passed. Moreover, considering this in cases when the user logs out, and returns, some time has passed so he must have the corresponding filled points.

    The issue in two words, how to make it add 1 point every X seconds even when the user is offline. The solution i have in mind is making an array with the time stamps for each point. Which is a very strange and awkward attempt to resolve this task.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Did you do this ?

    Did you store Browser.ExecJS("Date.now();") when logging off in the Local Store ?

    Did you Compare the stored time to Browser.ExecJS("Date.now();") when login in ?

    Did you calculate the minutes based on the diffference ?

    Did you once (in the layout start) updated stamina for the lost minutes?

    I assume you do store the stamina in the Local Store.

    Once, not during the whole game. During running game you just do add 1 'every x seconds'.

    I think it is easier to calculate the difference.. if you use Date.now()

    [quote:30jzha1r]The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00

    [quote:30jzha1r]1 Minute =

    60,000 Milliseconds

    you can simply set variable with: Browser.ExecJS("Date.now();")

  • so another way to do it is simply to say every 120 seconds, increment by one, using the system option. Add an upper limit of 100 and then it doesn't really matter anymore what the real time is.

    however this doesn't work if you are offline and you want it to recover between sessions, if that is what you're worried about

    if you're offline, do the following

    get the time from the last update

    get the current time

    get the difference

    stamina increase = difference / time-per-stamina point

    @korbaach showed how to get the tie.

  • More or less I've managed to do this, for testing I've manually inserted the saved data into variable.

    The issue I can't overcome is that in order to maintain whole numbers I had to use round, the problem now that values of less than 0 now are being rounded to 1 and as you can see it will add 1 to stamina, so in other words if user will keep on refreshing the page stamina will grow by 1 each time.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)