ID:118810
 
Redundant
Applies to:DM Language
Status: Redundant

This feature has already been implemented, or is already achievable with existing methods.
I have a game that gets 100+ people on the main server, and the most world.cpu intensive thing by far is mob/Stat()

And now look at this example code that will help illustrate what I'm saying:
mob/Stat()
if(statpanel("Not cpu intensive"))
stat("Hi")
sleep(5)
if(statpanel("Fairly cpu intensive"))
for(var/mob/M) stat(M)
sleep(15)
if(statpanel("Very cpu intensive"))
for(var/atom/O) stat(O)
sleep(100)

I could put sleeps in my game like this to reduce world.cpu (In fact I did for a while til I saw the undesirable behavior I'm about to tell you about). The problem with that is, if someone were to switch from the "Very cpu intensive" stat panel to "Not cpu intensive", they sit there looking at a blank "Not cpu intensive" statpanel while waiting for the sleep(100) from the former statpanel to finish.

This doesn't seem like desirable behavior, if the player switches from one stat panel to another, the new panel should be filled, not blank for prolonged amounts of time.

That is the new feature I'm suggesting.

Maybe some kind of special sleep(), like stat_sleep() or something. I tried to code something like that myself that should have worked, however it had very quirky behavior and it has something to do with how mob/Stat() works internally. It should have been simple to make by myself if it wasn't for that.

The function I coded was called Stat_Delay() and what it did was start a loop that sleeps in 0.1 second increments until either the timer reaches the desired amount or until the player changes tabs, it would then break the loop, allowing the new tab to refresh. So instead of using sleep(100) like the code example I would have put Stat_Delay(100). Simple right? Except that it doesn't work. It has some kind of internal conflict with mob/Stat(). And by that, I mean it caused flickering of tabs, and completely blank tabs for prolonged amounts of time.

If you need to test it yourself here it is:
mob/proc/Stat_Delay(Timer=0)
var/Sleeping_Panel=client.statpanel
for(var/I in 1 to Timer)
if(client.statpanel!=Sleeping_Panel) return //The player switched tabs, stop sleeping Stat() to allow the new tab
//to refresh
sleep(1)

Just use it in place of sleep() in the code example above.
mob/Stat()
if(statpanel("Not cpu intensive"))
stat("Hi")
sleep(5)
if(statpanel("Fairly cpu intensive"))
for(var/mob/M) stat(M)
sleep(15)
if(statpanel("Very cpu intensive"))
for(var/atom/O) stat(O)
sleep(100)

This code is always going to be very CPU intensive... You're looping through every /mob or /atom in the world - in every PC /mob's Stat()...

100 players - all with a loop that loops through every single /atom in the world, each. It's an insane waste of time.
1) That's example code to illustrate the issue, not code to be used in a real game.

2) The longer it sleeps the less cpu intensive it is over the same period of time.
mob/var/tmp/curPanel
mob/var/tmp/panelSleepTime=0
mob/Stat()
src.curPanel=src.client.statpanel
for(var/i=1; i<=src.panelSleepTime; i++)
if(src.client.statpanel!=src.curPanel) break
sleep(1)
if(statpanel("First"))
stat("Cake")
src.panelSleepTime=30
if(statpanel("Second"))
stat("Pie")
src.panelSleepTime=100
src<<"[src.client.statpanel] is active"

Dragonn wrote:
1) That's example code to illustrate the issue, not code to be used in a real game.
If any of the code in your stat panels is actually like that (loopy), you should fix it.

2) The longer it sleeps the less cpu intensive it is over the same period of time.
It uses less CPU because it is running the inefficient code less often, not because the code is actually more efficient.
Ter13 resolved issue (Redundant)

Stat() can be slimmed down by removing redundant loops and caching values where needed. if(statpanel()) should also be used to not render to tabs the player doesn't have selected.