AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Loop while player is alive (https://forums.alliedmods.net/showthread.php?t=180119)

Unreal1 03-10-2012 22:29

Loop while player is alive
 
How would I create a loop that executes a command every x amount of seconds while the player is alive.

thetwistedpanda 03-10-2012 22:37

Re: Loop while player is alive
 
How about CreateTimer(1.0, DatTimer, client, TIMER_REPEAT) and keep a Float:g_fTimerCount[MAXPLAYERS + 1] increasing by 1.0 each iteration of the timer. If they're alive, increase g_fTimerCount. If they're dead, set g_fTimerCount back to 0.0 and wait for them to spawn. If g_fTimerCount >= CommandDelay, g_fTimerCount = 0, IssueCommand. Pesudodrunkocodo.

Unreal1 03-10-2012 23:52

Re: Loop while player is alive
 
Quote:

Originally Posted by thetwistedpanda (Post 1666415)
How about CreateTimer(1.0, DatTimer, client, TIMER_REPEAT) and keep a Float:g_fTimerCount[MAXPLAYERS + 1] increasing by 1.0 each iteration of the timer. If they're alive, increase g_fTimerCount. If they're dead, set g_fTimerCount back to 0.0 and wait for them to spawn. If g_fTimerCount >= CommandDelay, g_fTimerCount = 0, IssueCommand. Pesudodrunkocodo.

Can you write this out in code?

For example, if I already have:

Code:

public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast) {
                new client = GetClientOfUserId(GetEventInt(event, "userid"))

//Create the timer stuff here

}

public Action:whattodo(Handle:timer, any:user_index)
{
//Does whatever
}

What do I need to put within the Even_PlayerSpawn meathod so that it executes whattodo on the client every few seconds.

necavi 03-11-2012 00:00

Re: Loop while player is alive
 
PHP Code:

public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast) {
        new 
client GetClientOfUserId(GetEventInt(event"userid"))
        
CreateTimer(1.0,whattodo,client,TIMER_REPEAT);
}

public 
Action:whattodo(Handle:timerany:client)
{
    if(
IsPlayerAlive(client))
    {
        
//dostuff
    
} else {
        
CloseHandle(whattodo);
    }


Something roughly like this would do.

mcpan313 03-11-2012 06:05

Re: Loop while player is alive
 
Quote:

Originally Posted by necavi (Post 1666436)
PHP Code:

public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast) {
        new 
client GetClientOfUserId(GetEventInt(event"userid"))
        
CreateTimer(1.0,whattodo,client,TIMER_REPEAT);
}

public 
Action:whattodo(Handle:timerany:client)
{
    if(
IsPlayerAlive(client))
    {
        
//dostuff
    
} else {
        
CloseHandle(whattodo);
    }


Something roughly like this would do.

CloseHandle(whattodo); ? why not use return Plugin_Stop;

necavi 03-11-2012 06:12

Re: Loop while player is alive
 
Because I don't tend to write many timers using TIMER_REPEAT, and while I considered return Plugin_Stop, I wasn't entirely sure it worked in that instance.

Bacardi 03-11-2012 08:12

Re: Loop while player is alive
 
I suggest use global Handle, create timer using Handle array index by client index... (what I writing here ?)

Anyway.

*edit
dam, I noticed if in timer userid return 0, it empty that array index.....
Then let use only client indexs
Spoiler


Like wiki site


All times are GMT -4. The time now is 19:39.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.