Hello,
I am currently watching the how to make an MMO videos and i am trying to add a feature that is not covered.
Basically, I want players to have energy that recovers slowly over time even when they are not logged in at a fix rate.
Or if I was making a strategy game, a feature that would increase player resources constantly or buildings with build times.
Can someone please explain to me how I would do this or help my get started in the right direction?
Thanks
time based game functions
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: time based game functions
Halls has a video about "crons", which allow you to run PHP scripts in Linux at certain time intervals. However, for the first use (players recover energy slowly over time while logged out), you could just calculate how long they have been logged out when they log back in, and give them the energy then.
The indelible lord of tl;dr
Re: time based game functions
Store the time taken to build each type of building in a table (call it building_master).Generic wrote:Hello,
Or if I was making a strategy game, a feature that would increase player resources constantly or buildings with build times.
Can someone please explain to me how I would do this or help my get started in the right direction?
Store the time the build action was set by the player in a table (call it building_action)
When the player makes a query for the status of the building (this could be direct or indirect),
- check the current time;
- obtain action initiated time from the building action table;
- obtain elapsed time by subtracting current time from action initiated time;
- obtain time required to complete the building from the building_master table;
- provide the status to the user.
Re: time based game functions
Crons in case you have players who are logged out, other then that, timestamps will do just fine. It requires a bit of time validation but it`s prolly worth more then running multiple crons per user. ( remember every cron is a thread )
You need some timestamps, some "currency" like the rate of health grow per time unit ( seconds, minutes, hours ) and check that against the last updated time. If it`s time to update the health, update the health field, update the last-updated-time to now and watch the magic do the work.
You need some timestamps, some "currency" like the rate of health grow per time unit ( seconds, minutes, hours ) and check that against the last updated time. If it`s time to update the health, update the health field, update the last-updated-time to now and watch the magic do the work.
Re: time based game functions
could you explain what situations would this be needed in?Xaleph wrote:Crons in case you have players who are logged out
If there is a situation, wherein you might want calculate the outcome of an event that takes place on the data of an off-line player, I think you could still do it without cron jobs, but would be interested in your idea..
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: time based game functions
It basically depends on what kind of interaction it involves. If the energy level of a logged-out player matters to other players who are logged-in (for example, in a strategy game if a logged-out player's energy defends them from attacks from logged-in players), you would need to go crons because the other way, which involves timestamps, is basically an illusion and the logged-out player's energy won't actually go up while they are gone. That is, unless you wanted to get really tricky and cause an attack on a logged out player to check the timestamp on that logged-out player, and figure their energy then.
Really, timestamps can stand in place for almost anything a cron could because if a logged-in user does not do some action, how would they ever know that it was not building up constantly over time? Basically, whatever action a logged-in player takes on a logged-out player, you can run any of the "replenish over time" effects for that logged-out player and immediately use those effects.
It may be lack of sleep, but I am having a hard time putting this into a good wording. I hope you get what I am so ineloquently trying to say lol.
Really, timestamps can stand in place for almost anything a cron could because if a logged-in user does not do some action, how would they ever know that it was not building up constantly over time? Basically, whatever action a logged-in player takes on a logged-out player, you can run any of the "replenish over time" effects for that logged-out player and immediately use those effects.
It may be lack of sleep, but I am having a hard time putting this into a good wording. I hope you get what I am so ineloquently trying to say lol.
The indelible lord of tl;dr
Re: time based game functions
Well, that's exactly what I meant, but you typed it much better than I did (even with your lack of sleep excuse) 
Crons consume too much of server resources. Only update what is needed, when it is needed.
If we assume we just use cron jobs, how would that work in the scenario that you proposed (for example, in a strategy game if a logged-out player's energy defends them from attacks from logged-in players)... Wouldn't the cron job have to run almost on a continuous basis..or is just run every hour?
I guess, it would depend slightly on the type of gameplay you have in place..
Crons consume too much of server resources. Only update what is needed, when it is needed.
If we assume we just use cron jobs, how would that work in the scenario that you proposed (for example, in a strategy game if a logged-out player's energy defends them from attacks from logged-in players)... Wouldn't the cron job have to run almost on a continuous basis..or is just run every hour?
I guess, it would depend slightly on the type of gameplay you have in place..
Re: time based game functions
I don't think cron jobs would be the best thing to use in this case as players consume energy at different times meaning players will only be active shortly before their energy gets reset and shortly after. I find, and this is more of a personal opinion, but too keep players more active, you might want to try to give players an x amount of energy after an x amount of time since their last energy consumption.
How this would work is you could either do loop based game-play and run a loop on the server to constantly update players energy rather than cron as cron would not be able to run as often as a simple, dare I say it, infinite loop. Or you could make comparisons between a saved timestamp of the last time a user consumed energy, this script will have to be made in one way or another so here's a little code to hopefully help you build more of an image in your head:
How this would work is you could either do loop based game-play and run a loop on the server to constantly update players energy rather than cron as cron would not be able to run as often as a simple, dare I say it, infinite loop. Or you could make comparisons between a saved timestamp of the last time a user consumed energy, this script will have to be made in one way or another so here's a little code to hopefully help you build more of an image in your head:
Code: Select all
$refill_time_dif = 120; // time difference 2 minutes (in seconds) = how often energy can be refilled
$timestamp = time(); // current timestamp
// if player energy is less than their max energy
// and the last time they used or gained energy plus the time difference is less than or equal to the current time
if( $player['energy'] < $player['max_energy'] && $player['energy_last_update'] + $refill_time_dif <= $timestamp )
{
$energy_refill = 1; // how much energy per 2 minutes?
// calculate and floor how much energy they are owed by the amount of time since their last refill/use
$total_refill = floor( $energy_refill * ( ( $timestamp - $player['energy_last_update'] ) / $refill_time_dif ) );
// update table
mysql_query("UPDATE `players` SET `energy` = `energy`+$total_refill AND `energy_last_update` = '$timestamp' WHERE `id` = '{$player['id']}'");
} Fighting for peace is declaring war on war. If you want peace be peaceful.