AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Any way to delay a task? (https://forums.alliedmods.net/showthread.php?t=277941)

gabuch2 01-20-2016 20:48

Any way to delay a task?
 
Hello

I want to know if there is a way to delay a currently running task (adding n to the remaining task time).

I know the method change_task exists but I checked and I haven't found a way to get the remaining time for a specific task.

Any help would be appreciated.

Bugsy 01-20-2016 21:34

Re: Any way to delay a task?
 
Quote:

Originally Posted by Shattered Heart Lynx (Post 2385026)
Hello

I want to know if there is a way to delay a currently running task (adding n to the remaining task time).

I know the method change_task exists but I checked and I haven't found a way to get the remaining time for a specific task.

Any help would be appreciated.

There is change_task(), but you can only set a new interval, not add to what has elapsed already.

You could use a thinking entity. You can test this in game by using 'set' at 1:00 on the game clock. If you do not use 'add', the task will complete at 0:55. Each time you use 'add', it will add 1 second. So using 'add' twice after doing 'set', will make it end at 0:53.
PHP Code:


#include <amxmodx>
#include <engine>

new g_Entity;
new 
g_SetTime;
new 
g_TaskTime;

public 
plugin_init() 
{
    
register_clcmd"say set" "CmdSetEnd" );
    
register_clcmd"say add" "CmdAddToEnd" );
    
    
g_Entity create_entity"info_target" );
    
entity_set_stringg_Entity EV_SZ_classname "timer_entity" );
    
register_think"timer_entity" "EntityThink" );
}

public 
CmdSetEndid )
{
    
//Set SetTime to now, and EndTime to 5 seconds (duration of task)
    
g_SetTime get_systime();
    
g_TaskTime 5;
    
client_printprint_chat "Task time set to %d" g_TaskTime );
    
    
entity_set_floatg_Entity EV_FL_nextthink , ( get_gametime() + 1.0 ) );
}

public 
CmdAddToEndid )
{
    
//Add 1 second to end time
    
g_TaskTime++;
    
client_printprint_chat "Task time increasd to %d" g_TaskTime );
}

public 
EntityThinkiEntity )
{
    if( 
iEntity == g_Entity 
    {
        if ( 
g_SetTime && g_TaskTime )
        {
            if ( ( 
get_systime() - g_SetTime ) >= g_TaskTime )
            {
                
//Do stuff here

                
client_printprint_chat "Time has elapsed %d" g_TaskTime );
                
g_SetTime 0;
                
g_TaskTime 0;
            }
            
            
entity_set_floatg_Entity EV_FL_nextthink , ( get_gametime() + 1.0 ) );
        }
    }



Xalus 01-21-2016 04:36

Re: Any way to delay a task?
 
Euhm It's probaly me but why repeating the think every 1.0 second?

PHP Code:

#include <amxmodx>
#include <engine>

new g_Entity;
new 
Floatg_TaskTime;

public 
plugin_init() 
{
    
register_clcmd"say delay" "CmdAddToEnd" );
    
    
g_Entity create_entity"info_target" );
    
entity_set_stringg_Entity EV_SZ_classname "timer_entity" );
    
register_think"timer_entity" "EntityThink" );
}

public 
CmdSetEndid )
{
    
// Repeat time to 5.0
    
g_TaskTime 5.0;
    
entity_set_floatg_Entity EV_FL_nextthink , (get_gametime() + g_TaskTime));
}

public 
CmdAddToEndid )
{
    if(
is_valid_ent(g_Entity))
    {
        
g_TaskTime += 5.0;
        
entity_set_float(g_EntityEV_FL_nextthink, (entity_get_float(g_EntityEV_FL_nextthink) + g_TaskTime))
        
        
client_printprint_chat "Task time increasd with 5 seconds" );
    }
}

public 
EntityThinkiEntity )
{
    if( 
iEntity == g_Entity 
    {
        
client_printprint_chat "Time has elapsed %d" g_TaskTime );

        
entity_set_float(g_Entity EV_FL_nextthink, (get_gametime() + g_TaskTime));
    }



Bugsy 01-21-2016 07:08

Re: Any way to delay a task?
 
I did consider that but didn't have time to make sure it worked correctly. Did you test?

I think you may have to not add onto the existing g_TaskTime, but just add onto the existing next think value. I think your method will compound the time with each addition. My thought is that would work if you += it to the game time value when the task was created. Plus you have to take into consideration multiple tasks with different intervals happening. I'm looking at this on a phone mind you

gabuch2 01-21-2016 07:27

Re: Any way to delay a task?
 
If that's the only way, I was thinking about creating another task, that repeats itself every second, thus increasing the integer by 1 each time.

But I wonder which method would be less resource intensive.

Xalus 01-21-2016 08:19

Re: Any way to delay a task?
 
Quote:

Originally Posted by Shattered Heart Lynx (Post 2385132)
If that's the only way, I was thinking about creating another task, that repeats itself every second, thus increasing the integer by 1 each time.

But I wonder which method would be less resource intensive.

So everyround you want a timer that starts with 1second, and every 'Think',
timer delays with 1 second?

gabuch2 01-21-2016 08:22

Re: Any way to delay a task?
 
Quote:

Originally Posted by Xalus (Post 2385138)
So everyround you want a timer that starts with 1second, and every 'Think',
timer delays with 1 second?

Two tasks, the task (main task) I want to delay and the "counter task" which basically I want it to keep track how many seconds has elapsed since the main task was activated.

When I want to delay the main task, simply I use change_timer with the value from the counter task.

I don't know if a constant repeating task would be more resource intensive like the entity method Bugsy suggested.

Bugsy 01-21-2016 08:34

Re: Any way to delay a task?
 
Task creation is an expensive operation, avoid it if you can. I can make this easier to use, or even build it into your code for you.


All times are GMT -4. The time now is 09:21.

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