AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Timers: Wiki misunderstanding (https://forums.alliedmods.net/showthread.php?t=324808)

St00ne 05-27-2020 21:49

Timers: Wiki misunderstanding
 
Hi, I'm using timers very often, and try to avoid handles as far as possible.
But I'm having troubles with the callback of a one-time timer using client serial (or user id) because the English wiki and RU wiki don't say the exact same.

I also searched here: https://sm.alliedmods.net/new-api/timers/Timer
Concerning the callback return, it says "Plugin_Stop to stop a repeating timer, any other value for default behavior."

So I'd like to know what to write at the end of such a timer:
PHP Code:

public void OnClientPutInServer(int client)
{
    
CreateTimer(15.0WelcomePlayerGetClientSerial(client)); // You could also use GetClientUserId(client)
}
 
public 
Action WelcomePlayer(Handle timerint serial)
{
    
int client GetClientFromSerial(serial); // Validate the client serial
 
    
if (client == 0// The serial is no longer valid, the player must have disconnected
    
{
        return 
Plugin_Stop;
    }
 
    
PrintToConsole(client"Welcome to the server!");
 
    return 
Plugin_Continue// <- ??? I am pretty sure I used to read return Plugin_Handled in that wiki and they changed it. Are they both working?


Thx for your advice.
St00ne

Fyren 05-27-2020 23:54

Re: Timers: Wiki misunderstanding
 
The documentation for the API page should be correct. (If it's not, file a bug report with code to reproduce and an explanation of what it is doing and what you think it should be doing.)

Since your example doesn't have a repeating timer, it doesn't actually matter what you return.

St00ne 05-28-2020 01:46

Re: Timers: Wiki misunderstanding
 
Thx for your answer. The logic of a repeated timer is well explained. I am wondering about a one-time timer situation, like in the example.

Ilusion9 05-28-2020 03:36

Re: Timers: Wiki misunderstanding
 
You are not using a repeating timer, so you can return any value or nothing, it doesn't matter for one time timers.
If you use a repeating timer, return Plugin_Continue will make the timer to repeat again, Plugin_Handled or Plugin_Stop will stop it.

You can use this without problems:
PHP Code:


public Action WelcomePlayer(Handle timerint serial)
{
    
int client GetClientFromSerial(serial); // Validate the client serial
    
if (client == 0)
    {
        return 
Plugin_Continue;
    }
 
    
PrintToConsole(client"Welcome to the server!");
    return 
Plugin_Continue;



St00ne 05-28-2020 07:23

Re: Timers: Wiki misunderstanding
 
Mmh ok, so "continue" or "handled" doesn't have a consequence here. Thx for your answers. I'll put this as solved if there are no more answers for a couple of days.

Tilex 05-29-2020 20:38

Re: Timers: Wiki misunderstanding
 
You are working with defines/enums, which are defined in sourcemod\scripting\include\core.inc
PHP Code:

/**
 * Specifies what to do after a hook completes.
 */
enum Action
{
    
Plugin_Continue 0,    /**< Continue with the original action */
    
Plugin_Changed 1,     /**< Inputs or outputs have been overridden with new values */
    
Plugin_Handled 3,     /**< Handle the action at the end (don't call it) */
    
Plugin_Stop 4         /**< Immediately stop the hook chain and handle the original */
}; 

So you might as well use these numbers as return values, if you want your code look obscure.

I bet that the timers would just have an if like: "if(bla == Plugin_Stop) ... else...", so it's possible that any other value than 4 might serve your needs.


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

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