IS4Code / PawnPlus

A SA-MP plugin enhancing the capabilities of the Pawn programming language
MIT License
102 stars 17 forks source link

Question :) #15

Closed samp-pinch closed 5 years ago

samp-pinch commented 5 years ago

Well, hello world! This is my code:

public OnPlayerConnect(playerid)
{
    /*-----------------------------------*/
    TogglePlayerSpectating(playerid, true);
    /*-----------------------------------*/
    Ulogovan[playerid] = false;
    /**/
    if(fexist(Korisnici(playerid)))
    {
        INI_ParseFile(Korisnici(playerid), "LoadUser", .bExtra = true, .extra = playerid);
    }
    else
    {
        wait_ticks(1);      
    }
    /*-----------------------------------*/
    return 1;
}

Imagine that this is not wait_ticks(1) but the task that is like 20sec long... And IF this player i started register timer for, disconnects while task is still running and other player (with same ID) connects, would other player get that timer executed on his ID because this task wasn't executed. ( 1st player which is not registered quits and than 2nd ( registered ) player get tasks executed even if he registered )???? Or Unregistered player get task from logged in player cuz registered player quit server? Example: [22:00:01] (!fexist(UnRegistered)) John connected to the server. [22:00:01] (( task started )) [22:00:03] John quits the server even tho task is started.. [22:00:05] (fexist(Register)) Nicolas (Same playerid as JOHN) connected to the server. [22:00:05](( previous task wasn't ended yet )) Would John get task from unregistered player or can I kill all tasks for SPECIFIC ID... Example kill all player's tasks when disconnect?

IS4Code commented 5 years ago

Hi! That is a good question to ask; indeed, the execution will continue even if the player disconnects in the meantime. However, if you manage to delete the task when the player disconnects, the execution will not be resumed.

forward Bind_OnPlayerDisconnect(CallbackHandler:self, Handle:task_handle, Task:task, orig_playerid, playerid);
public Bind_OnPlayerDisconnect(CallbackHandler:self, Handle:task_handle, Task:task, orig_playerid, playerid)
{
    if(orig_playerid == playerid)
    {
        pawn_unregister_callback(self);
        handle_release(task_handle);
        if(handle_linked(task_handle))
        {
            task_delete(task);
        }
    }
}

stock Task:BindToPlayer(Task:task, playerid)
{
    new Handle:handle = handle_new(_:task, .tag_id=tagof(task));
    handle_acquire(handle);
    pawn_register_callback(#OnPlayerDisconnect, #Bind_OnPlayerDisconnect, _, "eddd", _:handle, _:task, playerid);
    return task;
}
task_await(BindToPlayer(task_ms(500), player));