alliedmodders / amxmodx

AMX Mod X - Half-Life 1 Scripting and Administration
http://www.amxmodx.org/
493 stars 197 forks source link

A short question about task_exists #673

Closed AdamRichard21st closed 5 years ago

AdamRichard21st commented 5 years ago

Hello, guys.

So, that's mostly a question first of all. Could we get task_exists function to return false if called from its own task callback?

I mean, task_exists turns into a more meaningful native when it's able to detect a newer task other than the task itself if called from its callback.

Please, read the following code:

#include < amxmodx >

#define TASKID  33

public plugin_init()
{
    register_plugin( "Task Handler", "0.1", "AdamRichard21st" );

    set_task( 5.0, "OnTaskTriggered", TASKID );
}

public OnTaskTriggered( id )
{
    log_amx( "task(%d) : %s", id, task_exists( TASKID ) ? "exists" : "does not exist" );
}

Output:

L 01/30/2019 - 17:16:36: [task.amxx] task(33) : exists

You'll notice that task_exists can refer to itself if called from its own callback.

SmileYzn commented 5 years ago

A task still exists because the callback is not finished itself yet.

rsKliPPy commented 5 years ago

There is already a topic about this: https://forums.alliedmods.net/showthread.php?t=301628 This is the key statement:

The task exists until the task is completed. The "task" is defined as running the function (which implies completion). So, the task exists until it has completed what you told it to do which is running the function to completion.

AdamRichard21st commented 5 years ago

Hey, Klippy, great link, thanks.

Yeah, that makes sense to think in that way. But, I honestly can't imagine a scenario where this behavior is welcomed tho.

rsKliPPy commented 5 years ago

Can you give me a scenario where you would actually want to call task_exists in a task handler(callback)?

SmileYzn commented 5 years ago

If your pointer is inside callback, you tecnically speaking do not need check if task exists, since your code is being executed in callback, confuse no?

Anyway, in topic have a case to use that task exists inside callback, also the solution for this.

rsKliPPy commented 5 years ago

Regarding the example in the linked topic: you should move the logic in another function and call that from the callback, passing a bool:fromTask argument so you know whether to check for task existence. You shouldn't ever really call callback functions on your own, it's always better to move the shared logic to another function.

AdamRichard21st commented 5 years ago

Yeah, there's no problem with Fysiks's example at all. I've switched my logic to exactly same solution as soon as I realized task exists till its callback ending.

I feel like it's kinda meaningful when task_exists is able to dinamically return false depending on context, I mean, tasks are not meant to be stoped from callbacks in anyway, so it's semantically intuitive (to me) to handle some checks at this point.

On my current scenario, I'm re-building my deathmatch based mod, to make it clearer at some points, one of them is avoiding too many handlers (such as checking tasks elsewhere) to do stuff. I just decided to create tasks from some handlers pointing to a single callback where I could actually write a self-dependent block of logic.

Just my thoughts guys, thanks. <3