# How do I count time with time stamps

Get help using Construct 2

### » Mon May 30, 2016 7:54 pm

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?
B
11
S
3
Posts: 44
Reputation: 713

### » Mon May 30, 2016 8:53 pm

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.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon May 30, 2016 9:40 pm

I think it is easier to calculate the difference.. if you use Date.now()
The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00

1 Minute =
60,000 Milliseconds

you can simply set variable with: Browser.ExecJS("Date.now();")
B
67
S
24
G
7
Posts: 1,519
Reputation: 11,094

### » Sat Jun 04, 2016 3:01 pm

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.
B
11
S
3
Posts: 44
Reputation: 713

### » Sat Jun 04, 2016 6:39 pm

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'.

korbaach wrote:I think it is easier to calculate the difference.. if you use Date.now()
The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00

1 Minute =
60,000 Milliseconds

you can simply set variable with: Browser.ExecJS("Date.now();")
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sat Jun 04, 2016 9:25 pm

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.
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

### » Mon Jun 06, 2016 5:20 am

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.
B
11
S
3
Posts: 44
Reputation: 713