ID:1586003
 
(See the best response by Ter13.)
I want to make it so the player's age increases while the server is up, regardless of whether the player is logged in or not. I was thinking of using world.time but that resets when the server reboots. I could also make another variable such as Year and make it go up with sleep(). Unless I'm constantly updating Year (shouldn't create much lag since its 1 global var, right?) anytime the server goes down between update times, the counter for Year restarts. I've a few questions on how to do this well.


Is it better to update everyones age at a set time(each new day, month, etc) or would it be better to be constantly updating age?

Is it better to use world.time or sleep() for keeping track of the date ingame, or is there some other way that would be more effective?

When it comes to character age, are there any variables that I should save for technical reasons? (eg. Using DOB to calculate a characters age if their age var somehow gets messed up, anything i should save as tmp, global, etc)
I would use
proc
checkTime(/**/)
// var/day = time2text(world.realtime, "Day")// if you want days
var/time = time2text(world.timeofday, "hh:mm")
if(time == "00:00")
Do_event()


It`s pointless to make that players also age when they are not logged in because that would be a waste. (Because server would constantly updating its year even if he is not playing and you dont want 100 proc running for players that aint even playing the game)

Good luck
Best response
It`s pointless to make that players also age when they are not logged in because that would be a waste. (Because server would constantly updating its year even if he is not playing and you dont want 100 proc running for players that aint even playing the game)

That's... not actually true.

OP actually seems to know what he's doing to some degree here, Phat T.

The trick to making a player able to age when they are offline, is to save their year of birth, and when the player is read from a savefile, calculate how long it's been since the player was born.

#define YEAR_TICKER 18000 //world is saved every 30 minutes
#define YEAR_TICKS 48 //30 minutes * 48 ticks = 24 hours per year

world
New()
..()
start_time()

var
yearstart = 0
year = 0

proc
start_time()
set waitfor = 0 //make sure this proc doesn't block processing
. = load_world()
global.year_start = world.time - .
while(.)
save_world()
sleep(min(YEAR_TICKER,.)) //wait the remaining number of ticks
. -= min(YEAR_TICKER,.)

while(1) //set the loop to run as long as the world is online
increase_year() //tell the game to update the current year
for(.=0;.<YEAR_TICKS;count++)
save_world()
sleep(YEAR_TICKER) //wait 30 minutes

increase_year()
++global.year
for(var/mob/player/p in global.players)
p.increase_age()
yearstart = world.time

load_world()
if(fexists("world.sav")) //if the file exists, load it
var/savefile/f = new("world.sav")
f >> global.year //load the saved year
f >> . //load the elapsed time in this year
return .
else
return YEAR_TICKER * 48

save_world()
if(fexists("world.sav"))
fdel("world.sav")
var/savefile/f = new("world.sav")
f << global.year
f << world.time - global.year_start //export the current remaining duration of the current year



That pretty much covers saving the world's time, as well as running the event. I decided it would be best to save the world every 30 minutes, and I decided to use a 24hr = 1yr sort of setup.

Next, is the players.

var/list/players = list()

mob
player
var
tmp
age
birthyear

Login()
if(birthyear==null) //if the mob hasn't been born...
birthyear = global.year //set the birthyear to this year
age = 0 //set the age to the default
. = ..()
players += src

Logout()
//save the player
players -= src
src.loc = null

Read(savefile/F)
..(F)
age = global.year - birthyear //when the player is loaded from a savefile, we need to update the mob.

proc
increase_age()
++src.age


That's pretty much all there is to it. Make sure age is marked as temporary, because we don't need to save it. As long as we save the player's year of birth, as well as save the date of the world, we can have offline aging.

If you want this to happen even when the server is down, you should use world.realtime, although, I'd argue that's of questionable reasoning. Hell, even players aging when offline seems a little unreasonable.
Thank you very much, still so much to learn so this will help me out immensely.


while(.=0;.<YEAR_TICKS;count++)

This line seems to draw an error.

World.dm:26:error: .: missing comma ',' or right-paren ')'

Also, why did you use a semicolon in that statement? I've only been learning DM for a week or so now and I thought they were used as a type of newline. However in here I don't know why its being used (but its clear its important in the situation, did some playing around and only semicolon will work as a divider(?) of the three statements) for a different reason.

I couldn't find anything in the documentation about it either. Well, no semicolon operator that is.
Take a second look. I used a while instead of a for during a revision.

The reason for the semicolon is because a for loop has three potential clauses, one that runs on the first iteration, one that runs before every iteration, and one that runs after every iteration.