Does anyone have any idea how I can insert the real time in my game?
I want that my game runs in real time, like a browser game.
It would be ideal if the game would take over the computer time,
so I can say in the game that something should happen at a specific time.
Sorry about the mistakes, my english is not so good
if you don’t want to import any classes use
else use the Calender.class … I think it has more options
[java]Calendar cal = Calendar.getInstance();
You SHOULDN’T use the computer time for ONLINE GAMES AND APPLICATIONS! NEVER! Imagine that you created a game that uses the computer time. And, by example, at 8 pm, the shop prices would be much more cheap. And i am at 4 pm. I could JUST change the time of my computer to 8 pm, and then get the items much more cheaper. Of course this is just an example, but imagine there was a happyhour in your game, in which the items were free. That CAN´T HAPPEN! The best solution is to create a game server, and then give the server an hour. That will sychronize the game’s time using the server’s time. NEVER USE COMPUTER TIME ON MULTIPLAYER GAMES!
Of course, if you’re just testing, there is no problem. But if your game becomes very famous, it just can’t happen.
And well, about getting the system time, i don’t really know how to do that. But here is my advise, and i repeat: NEVER USE COMPUTER TIME ON ONLINE GAMES AND APPLICATIONS!
Hope i helped you,
Im portuguese, so i’m sorry if there are some errors in the text.
Thank you guys for the great answers they have helped me a lot. =D
well, you still can use System.currentTimeMillis, if you use it server side then communicate the result to the client via a message. I don’t see a negative aspect in this : the guy that has the computer with the server is basically the game master anyway.
But maybe that you can create something like an appstate client side that will give the current time
public class TimeHolder extends AbstractAppState
private long time;
time = System.currentTimeMillis();
public void update(float tpf)
time += (long) (tpf * 10); //or * 100 or *1000, i don’t remember if tpf is in millis, centi of deci. And i don’t have time to check now)
- Will be use by the “TimeMessageListener” when you’ll have a server.
- No need to spam the client with the current time, just send it to him once (or times to times) as it will update it himself
public void setTime(long time)
this.time = time;
public long time()
then you can use this as a temporary solution and you’ll be able to change the way the client has the current time later with only small changes.
It’s called level of abstraction, and it’s the solution for problems 99% of times.
Well if it is a pvp game dont do this do all logic on the server, client can of course use clock for only display things, but not for logic.
if you do a coop game which u usually play with friends, then you can skip most of cheat/sanity checks, and rely on the players to be fair.