I'm making a game like the game Evony for iPhone but instead of being an MMO it is single player. For those of you that don't know about Evony, in the game you upgrade your own buildings and the buildings take a certain amount of time in real life to upgrade. Evony runs off a server but I want my iPhone game to run off of the app. Which leads me to my problem of the game needing to continuously update the time while not running. I'm new to using the clock system in programming and was wondering if any of you could help me.
When the building object is created, mark it with a created time stamp. Then, whenever the view is loaded, or it awakes, compare the current date and time to the created time stamp, giving you the 'lifetime' of the building.
Here is how to get the current date and time in iOS sdk:
[NSDate date]
Related
I want to replicate some of the functionalities of sleeptrakcing apps like AutoSleep. Apparently I can not open the app for a day or two, and when I open it up, it can accurately access the sensor data on my Apple Watch to see if I'm moving or not at any given 15 minute interval.
I've tried looking at various functionalities of CoreMotion. The closest I've seen is using CMSensorRecorder and call recordAccelerometer to start recording. However, this only allows you to record up to 12 hours. And the user would have to start the app to start recording. This is clearly a limitation that AutoSleep doesn't seem to have, because it seems to get the data out of thin air.
Anyone know how it's possible to get historical Accelerometer data from WatchOS that stretches back a long time without actively recording it?
There are two decisions to make timers, with "update:" and with actions.
But i need a timer that could continue even if the player close the game.
So when player comes back to game after a certain time, his progress could be saved. Could i use CFAbsoluteTime to make this?
You should look at the Date/Time instead of in-app timers for this purpose.
Essentially, you need to track the time passed between launches of the app. You can do this via storing the Date in the user defaults when the app is closed, and comparing it to the current Date when the app is openened the next time to figure out how much time has passed.
You may want to look at using an external clock, like grabbing the time from a website, this will prevent people from simply changing there clocks to alter time. (This will not however prevent people from altering the DNS to go to there own web servers, but will most people go through that trouble for a simple game)
This question already has answers here:
Is there a clock in iOS that can be used that cannot be changed by the user
(6 answers)
Closed 9 years ago.
i am on way to making game. and i am trying to solve an issue about time.
gamer get life (when all rights of playing finished) every ten minutes. (at the same time i will sell a life as an in-app like candy crush). game is offline playable. i am getting time and save it when user killing app . When he/she opens game getting current time and giving life by making date subtraction. here is an issue that;
when user kills app, adjust time to 1 hour later and opens the game again (problem goes and i am giving lifes).
is there any way to solve this problem. is there any time source (without online access)
that user can not change and i can retrieve correct time.
ps: i will code the game in iOS environment. iOS specific answers will be appreciated.
I do not believe that there is a solution.
What you want is a relative time, so you could think about firing an NSTimer after a certain interval, but even that won't work because such timers will fail or die when the app is in the background (or killed).
Ultimately unless you utilize an off-device storage method you are beholden to the user's date management. Whatever absolute time method you use (such as CFAbsoluteTimeGetCurrent) it will be dependent on the system clock that the user can modify.
I would use the "monotonic time" given from the real clock time unit. On linux this is usually done by using clock_gettime(CLOCK_MONOTONIC, ×pec);. On mac/ios I think mach_absolute_time() is the function you are looking for. See one of these posts for further reading
clock_gettime alternative in Mac OS X
Monotonic clock on OSX
CACurrentMediaTime() uses mach_absolute_time(). So it should be your best friend since it is not possible to change by the user. Except for it will be reset on reboots (at least on mac). It would be best in your case if the mach_absolute_time wasn't reset on reboots...
I am developing an iOS application for iPod Touch in which my application displays the server time always. I always sync the application time with server time whenever the application comes to foreground by making a web service call to the server. If there is a connectivity loss between my server and client for few hours I wont be able to sync the application time. I read iOS does not support running a timer when the application is in background other than few limited cases mentioned below:
Apps that play audible content to the user while in the background, such as a music player app
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Newsstand apps that need to download and process new content
Apps that receive regular updates from external accessories
So how can I keep track of application time? Whenever the user switches to my application he needs to look at the server time so I need to run a timer to update the last synced server time.
A combination of other answers, create a class in charge of obtaining the server time and maintaining the last time the application was synced to the server using a combination of NSDate* lastSync and applicationDidBecomeActive. For example:
-(void)applicationDidBecomeActive:(UIApplication*)application {
[ServerTimeSync sharedInstance] resync];
}
ServerTimeSync will maintain an NSDate* property with the last sync time (you'll want to convert what the server gives you to an NSDate*).
You can store the NSDate when the app goes into the background. When it resumes, get the current NSDate again, and add the difference to your stored server time.
You don't need a timer for this at all.
I would suggest that when you sync time with your server, you have it return its current UNIX timestamp. You can then do:
[[NSDate date] timeIntervalSince1970];
...to get the device's current UNIX timestamp. Then what you can do is store the difference between these two timestamps. This is the clock skew between the server time and the device time.
You can now compute the server's approximate time by taking the device's current UNIX timestamp and adding the clock skew to it. Adjust for time-zone when displaying it (if you want), and you're done. Whenever you sync time with the server, you can just refresh the stored clock skew value.
If you want to get fancy, you can also attempt to measure and take network latency into account when determining the clock skew.
This approach should work much better than trying to store the server's absolute timestamp and then track how much time has elapsed using a timer (or any other mechanism).
In my game I am using device time for timers and I want to prevent bots and cheats such as changing game time during No Wifi or offline gameplay. How can I achieve this?
Depends really. If putting the time BACK is the issue, then you could store a 'latest time' and check the current time is greater than it. Just keep the stored time up to date during online play and check when offline.
If putting the time FORWARD is the issue then you could just store a counter somewhere from the start of the game, instead of using the realtime clock.