SetTimer: Difference between revisions

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 5: Line 5:
::interval : The amount of time (in milliseconds) after which the function will be called.
::interval : The amount of time (in milliseconds) after which the function will be called.
::repeat : 1, if the timer is to be repeated, 0 otherwise. </poem>|
::repeat : 1, if the timer is to be repeated, 0 otherwise. </poem>|
retvals=The ID of the timer created, which can be used with [[KillTimer]]|
note=Unlike the usual timer function in squirrel gamemode, this function has third parameter 1 or 0. If you pass 1, the timer will repeat indefinitely rather than execute one time. Pass 0 to execute timer '''one time''' only.|
example=<source lang="lua">
example=<source lang="lua">
//Copyright Kye 2009
//Copyright Kye 2009
Line 50: Line 52:


</source>|
</source>|
relfuncs=*[[SendCommand]]
relfuncs=*[[KillTimer]]
}}
}}

Latest revision as of 13:56, 25 March 2022


Description:
This will create set a timer, which will be triggered when the given time has passed.


Parameters:

(FunctionName, interval, repeat)

FunctionName : The name of the function to be called.
interval : The amount of time (in milliseconds) after which the function will be called.
repeat : 1, if the timer is to be repeated, 0 otherwise.


Return Values:
The ID of the timer created, which can be used with KillTimer

Important Note:
Unlike the usual timer function in squirrel gamemode, this function has third parameter 1 or 0. If you pass 1, the timer will repeat indefinitely rather than execute one time. Pass 0 to execute timer one time only.

Example

//Copyright Kye 2009
//modified by habi 2022
function OnNPCScriptLoad()
{
	SetTimer("TimerTest",10000,1);
}

function TimerTest()
{
	local msg;
    local name;
	local pos;
	local Distance;
	local x;
	local num_streamed_in = 0;
	local num_connected = 0;
	x=0;
	while(x!=MAX_PLAYERS) {
	    if(IsPlayerConnected(x)) {
	        num_connected++;
	        if(IsPlayerStreamedIn(x)) {
	            num_streamed_in++;
	       		name=GetPlayerName(x);
				pos=GetPlayerPos(x);
				Distance=GetDistanceFromMeToPoint(pos);
				msg=format("I see %s @ %f units with state:%d health:%d armour:%d weapon:%d",
						name,Distance,GetPlayerState(x),GetPlayerHealth(x),GetPlayerArmour(x),GetPlayerArmedWeapon(x));
				SendChat(msg);

				if(GetPlayerState(x) == PLAYER_STATE_DRIVER) {
				    msg=format("I see %s driving vehicle: %d",name,GetPlayerVehicleID(x));
					SendChat(msg);
				}
				
			}
		}
		x++;
	}
	
	msg=format("I have %d connected players with %d streamed in",num_connected,num_streamed_in);
	SendChat(msg);
}

Related Functions

The following functions may be helpful as they relate to this function in one way or another.