Author Topic: Looping thru clients = Lag  (Read 742 times)

I'm currently working on a desert roleplay thingamadoodle. In order to bottomprint everybody their thirst level and check if I should kill them, I've been looping through the clients like so:
Code: [Select]
function needs()
{
while(%i < clientGroup.getCount())
{
%client = clientgroup.getobject(%i);

if(%client.thirst == 0)
{
%client.player.kill();
}
else
{
%client.thirst--;
}
                commandToClient(%client, 'bottomPrint', "\c3Thirst: \c6" @ %client.thirst, 1);

%i++;
}
}

Problem is, this needs to be called every second. Is there a better way to make this without having lagtasms?

Can you check to see if a variable is too low, find the client, then execute?

Don't loop so frequently. This could easily work with 10 or even 60 second loops.

I found out that I had a loop of 0-10000 it would cause lag every second, is there anything you have that might cause this?

for(%i=0;%i<CLientGroup.GetCount();%i++)
{
   
}

Untested code, but a concept like this would remove the need for any looping:

Code: [Select]
package Thirst
{
function GameConnection::spawnPlayer(%this)
{
Parent::spawnPlayer(%this);
%player = %this.player;

%player.thirstLevel = 600;
%player.thirstTime = getSimTime();

%player.thirstDeath = %this.schedule(600000,thirstDeath);
}
};
activatePackage(Thirst);

function Player::thirstDeath(%this)
{
messageClient(%this.client,'',"You have died of thirst!");
%this.kill();
}

function Player::restoreThirst(%this,%amount)
{
%time = getSimTime();
%thirst = %this.thirstLevel - ((%time - %player.thirstTime) / 1000) + %amount;

%player.thirstLevel = %thirst;
%player.thirstTime = %time;

cancel(%player.thirstDeath);
%player.thirstDeath = %this.schedule(%thirst * 1000,thirstDeath);
}

1) You spawn, and get 600 thirst. Lasts you ten minutes.
2) If nothing happens to your thirst, you die in ten minutes.
3) restoreThirst should boost your life span by %amount seconds.

Example:

Code: [Select]
function serverCmdDrink(%cl)
{
%cl.player.restoreThirst(60);
}

Typing /drink would give you an extra minute of life.
This is untested, so I might've typo'd with the math.

EDIT: Found and fixed two typos.
« Last Edit: February 24, 2010, 04:48:11 PM by Truce »

edat: loops weren't lagging me, events on server were :/

edat: loops weren't lagging me, events on server were :/
Exactly, I suggested that it was something else:
I found out that I had a loop of 0-10000 it would cause lag every second, is there anything you have that might cause this?