ID:156019
 
Greetings,

I'm pretty far with my game code so far, yet there's a small hurdle for me to take.

I want my mobs to have a playtime value, which I could use later on, but I really don't know how to do it.

But all I can think of are infinite loops.

for()
var/mob/M
spawn(600)
M.playtime += 1


Even tho' this would work, it'd freeze up the whole thing.

Thanks in advance for your help.
JackSmoke
I think you would have to use world.realtime/world.timeofday, and assign a variable to the time that the player logged on. Then use Stat() to update the variable.

Although I'm not entirely sure.
In response to SadKat Gaming
SadKat Gaming wrote:
I think you would have to use world.realtime/world.timeofday, and assign a variable to the time that the player logged on. Then use Stat() to update the variable.

Although I'm not entirely sure.

That's an interesting idea. I haven't thought of using Stat() for that.

Thank you!
In response to JackSmoke
Solution:

Since I'm currently using the ActionLock cooldown, It'd look something like that:

    Stat()
stat("Playtime: " , "[playtime]")

if(ActionLock("Timer",10))
return
else
playtime += 1


This way, the playtime would increment by 1 every second.
Thanks again man !
client/var
TotalPlayTime
tmp/SessionPlayTime

client/New()
..()
SessionPlayTime = world.realtime

client/Del()
TotalPlayTime += world.realtime - SessionPlayTime
// Whatever saving you've got going on
..()

In response to Emasym
Emasym wrote:
> client/var
> TotalPlayTime
> SessionPlayTime
>
> client/New()
> ..()
> SessionPlayTime = world.realtime
>
> client/Del()
> TotalPlayTime += world.realtime - SessionPlayTime
> // Whatever saving you've got going on
> ..()
>


Thank you.
I achieved a similar thing with the Stat() proc.
The issue here is the infinite loop.

for()
var/mob/M
spawn(600)
M.playtime += 1


The main issue here is probably the misunderstanding that for() var/mob/M is looking through all of the mobs in the world and starting a new thread that executes in 60 seconds and increments that mob's playtime variable. What it IS doing is creating an infinite loop (equivalent to while(1)), and in the loop's block, both defining a variable called M, and spawning a new thread for every loop (which will never be given priority or do anything).

This is probably not anywhere near what you want it to do.

To make it do what you want, you should replace:
for()
var/mob/M
...


with:
for(var/mob/M in world)
...


However, I would be remissed to tell you that that is good enough. If you have any mobs that aren't players, this is a terrible concept. On top of that, if this is the only time this function executes, not only will it only work on the mobs that exist at the time, it will only increment their playtime once.

The best method, and the one that would prove the most accurate, would be something that uses world.time and, equally important, doesn't work off of a variable that every mob has. If anything, at least filter out client mobs...

mob
player
var
play_time
tmp/session_start // when did this session start?

proc
start_session()
session_start = world.time
return session_start

end_session()
var/session_time = world.time - session_start
play_time += session_time
session_start = null
return session_time

current_time()
var/session_time = world.time - session_start
return play_time + session_time


Running start_session() when they "start" playing and end_session() when they "stop" playing will result in exactly what you are asking without requiring any overhead for infinite loops that loop every second, minute, hour, etc... it's also as accurate as any time tracking can be.

Your usage of stat() to increment some counter (but only every X ticks) is... well, it's not good... if anything, create a dedicated infinite loop that works on your own schedule. Don't try to work it into an existing loop that is completely unrelated to the task.

(PS: if it was me, I would separate this data from the mob entirely, since it seems to have less to do with the mob and more to do with the player... I'm also not a fan of the /mob/player setup, as that limits you quite a bit...)
In response to Keeth
Thanks, but once again: I found a really neat solution to this problem also which is just a few lines of code in the Stat() proc
In response to JackSmoke
You might think it's neat, but it's just bad practice.

That's why I posted in the first place.
In response to Keeth
Keeth wrote:
You might think it's neat, but it's just bad practice.

That's why I posted in the first place.

Hmm, I see.
I've read your code again and you're right. Maybe that's a better way to do it (or rather more accurate).

I don't have that spawn check every 100ms that way.