Jump to content

Question Regarding Timers


Usarjjaco

Recommended Posts

Hello Everyone,I'm working on a text based game, and I'm to the point where I'm trying to implement things like skill learning and travel times for both players and player units. From what I've read Javascript would be the key to creating the timers so that it takes a certain amount of time for this to occur. I'm going to be attempting to integrate the Javascript with MySQL and PHP. Does anyone know of a site/forum location with a good tutorial on the integration process, or someone who'd be willin to take the time to type out a simple code so I can get the idea?Something Like When Player Clicks "Go!"It takes the current time, and then in 1 hour it will update everything so it shows the player has arrived at the new location?Much appreciated in advance guys!

Link to comment
Share on other sites

it depends, that could go a couple of ways. If people are logging into this game, then by all means use SESSIONS and save the users start time, and then have your game pages check against this time stamp in each instance of the page, while using a timer within the page to keep checking. I think there will be different ways to do this depending on the nature of your game, and how often pages/screens change.

Link to comment
Share on other sites

Just a comment on the gameplay: when you say an hour, I hope you work on some kind of sped up time system? Cause an hour to wait while watching a screen is a bit long.

Link to comment
Share on other sites

i think its more of after an hour travel time (within the game and doing stuff) the scenery changes. Although I do agree with your point that no one want to literally do stuff for an hour before going to a "new" region within the game. Maybe like 5-10 minutes or so depending on the map.

Link to comment
Share on other sites

Hey guys,Sorry about the long response. Basically what I'm talking about is for two instances:A) A player wants to send units somewhere... and I want it to take X amount of time.B ) A player wants to learn a skill, and I want it to take X amount of time.I need the time to start when they ask for the action and continue running whether they are logged in or not.

Link to comment
Share on other sites

The best way to do that is store the timestamp in the database when they start the action. If you know when they started the action and how long it takes, then at any point in time you can figure out how much time is left. You can have Javascript periodically check with the database to see how much time is left. The difficulty is in actually making the event happen when time is up. There's no constant timer running, so you'll need to be able to determine if an action that should have already been completed actually has been or not. One way would be to use another field in your "pending tasks" database table to store whether or not that action has already happened, or you can also just delete the pending tasks once they happen (or copy the data to another table). In order to make the task actually happen, you can either schedule a script to run once a minute and check for tasks that need to be completed within the past minute, or just whenever the user refreshes the page you can first check for pending things that need to happen and do those first before doing whatever else the user wanted. Hopefully that makes sense.

Link to comment
Share on other sites

The best way to do that is store the timestamp in the database when they start the action. If you know when they started the action and how long it takes, then at any point in time you can figure out how much time is left. You can have Javascript periodically check with the database to see how much time is left. The difficulty is in actually making the event happen when time is up. There's no constant timer running, so you'll need to be able to determine if an action that should have already been completed actually has been or not. One way would be to use another field in your "pending tasks" database table to store whether or not that action has already happened, or you can also just delete the pending tasks once they happen (or copy the data to another table). In order to make the task actually happen, you can either schedule a script to run once a minute and check for tasks that need to be completed within the past minute, or just whenever the user refreshes the page you can first check for pending things that need to happen and do those first before doing whatever else the user wanted. Hopefully that makes sense.
Hey There,thanks for the quick response. I think I'm following pretty well. I'm going to have to go read up on the Javascript tutorials for integrating them with MySQL. I'll take a gander and repost here with any further questions. Thanks again!
Link to comment
Share on other sites

Hey There,thanks for the quick response. I think I'm following pretty well. I'm going to have to go read up on the Javascript tutorials for integrating them with MySQL. I'll take a gander and repost here with any further questions. Thanks again!
to help you clarify even further, there is not direct way for Javascript to work directly with MySQL. What JSG was alluding to would be to use AJAX to run a server-side script (like PHP) to request and retrieve info from the database and return it to the page in way meaningful enough that Javascript can make use of it. This could be done on page load, or with a timer, depending on your needs.
Link to comment
Share on other sites

to help you clarify even further, there is not direct way for Javascript to work directly with MySQL. What JSG was alluding to would be to use AJAX to run a server-side script (like PHP) to request and retrieve info from the database and return it to the page in way meaningful enough that Javascript can make use of it. This could be done on page load, or with a timer, depending on your needs.
Ok, Haven't even dabbled in AJAX yet, but I suppose there's not time like the present right? :)
Link to comment
Share on other sites

Haven't even dabbled in AJAX yet, but I suppose there's not time like the present right? :)
Precisely! :) It's not really that hard, though. The W3Schools tutorial is all you really need.The challenge is actually (in my opinion, anyway) in writing a PHP script to produce output that's meaningful and easily manipulated by JavaScript. I find that JSON strings work wonderfully for most tasks but they don't work for everything. Sometimes there are simpler ways, depending on what you need to do.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...