Author Topic: Timers?  (Read 1531 times)

I need someone to give me a run-down of how timers work. I do not know where to start.

I know that Aloshi's PDA had a timer, if you want one that ticks down...
I remember it involved schedules and variable decreasing.
I'd try to copy/pasta the script, but I'm not on my BL computer right now.

Code: [Select]
%scheduleID = schedule(%time_ms, %cancel, %functionName[, %args]);This will schedule %functionName to be called with %args in %time_ms milliseconds. However, when the time is up, if %cancel is no longer false, the schedule will be aborted.

To cancel a schedule, you can also use this method:
Code: [Select]
cancel(%scheduleID);
Every object has it's own schedule method, example:
Code: [Select]
%scheduleID = %obj.schedule(%time_ms, "delete"[, %args]);When time expires, and the object still exists, %obj.delete(); is called.

$Sim::Time also holds the number of seconds since the game started I believe, so you can use that for other methods.

Please give me an example code. I do not understand.

Code: [Select]
schedule(5000, 0, "echo", "hello");In 5 seconds, it will echo Hello into the console

Code: [Select]
$schedule = schedule(60000, 0, "commandtoserver", 'MessageSent', "60 seconds is up");
function cancelMessage(){
cancel($schedule);
}
After 1 minute, you will say 60 seconds is up.

Another way to achieve the same thing:
Code: [Select]
schedule(60000, $schedule, "commandtoserver", 'MessageSent', "60 seconds is up");
function cancelMessage(){
$schedule = 1;
}

Curious, but the timer will keep going until the cancel var is set to 0. That means if you..


Code: [Select]
schedule(60000, $schedule, "commandtoserver", 'MessageSent', "60 seconds is up");
function cancelMessage(){
$schedule = 1;
}

Set it to this, it would keep saying the 60 seconds is up until you call the funciton cancelMessage, right?

It'd say it every 60000milliseconds.

No, that would say it once.

Code: [Select]
schedule(60000, $schedule, "msg");
function cancelMessage(){
$schedule = 1;
}

function msg()
{
 schedule(60000, $schedule, "msg");
 commandtoserver('messagesent',"60 seconds is up.");
}

That should work.

Code: [Select]
schedule(10000, %this.Bounty.BountyClock, "BountyDeduct", %this);
function BountyDeduct(%client)
{
if(%client.Bounty == 0){
messgeClient(%client,"","\c2Your bounty is up.");
%client.Bounty.BountyClock = 1;
}else{
%client.Bounty -= 25;
messageClient(%client,"","\c3Your bounty has been reduced to \c0" @ %client.bounty @ "\c3.");
}
}


fix pl0x

Code: [Select]
function whenyouaddbounty(%client)
{
 //othercode such as %client.bounty += NUMBERS;
 cancel($bounty[%client]);
 $bounty[%client] = schedule(10000,0,BountyDeduct,%client);
}
function BountyDeduct(%client)
{
        if(!isObject(%client)){return;}
        %client.Bounty -= 25;
if(%client.Bounty <= 0){
                %client.bounty = 0;
messgeClient(%client,"","\c2Your bounty is up.");
}else{
messageClient(%client,"","\c3Your bounty has been reduced to \c0" @ %client.bounty @ "\c3.");
        $bounty[%client] = schedule(10000,0,BountyDeduct,%client);
}
}
fixed k?

It reduces your bounty every 10 seconds, if you gain bounty during this time the clock resets. (Kill someone, get 100 bounty, wait 10 sec, bounty is 75, wait 3 sec, kill someone, you must wait 10 seconds to reduce bounty not 7)

It also checks whether your bounty is less than zero, and resets it back to 0, otherwise it would continue reducing bounty into the -10000s.

If you're wanting any kind of persistence in your bounties (persistence of player stats in RPGs is a good thing), you should probably use bl ids to maintain the stats.

e.g. $bounty[bl id] = <value>.

Then, I'd add a check to ensure that the same bl id cannot join the server twice.