Raised This Month: $51 Target: $400
 12% 

Any way to delay a task?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 01-20-2016 , 20:48   Any way to delay a task?
Reply With Quote #1

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.
__________________
gabuch2 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-20-2016 , 21:34   Re: Any way to delay a task?
Reply With Quote #2

Quote:
Originally Posted by Shattered Heart Lynx View Post
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 ) );
        }
    }

__________________

Last edited by Bugsy; 01-20-2016 at 21:53.
Bugsy is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 01-21-2016 , 04:36   Re: Any way to delay a task?
Reply With Quote #3

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));
    }

__________________
Retired.

Last edited by Xalus; 01-21-2016 at 04:37.
Xalus is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-21-2016 , 07:08   Re: Any way to delay a task?
Reply With Quote #4

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
__________________

Last edited by Bugsy; 01-21-2016 at 07:17.
Bugsy is offline
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 01-21-2016 , 07:27   Re: Any way to delay a task?
Reply With Quote #5

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.
__________________
gabuch2 is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 01-21-2016 , 08:19   Re: Any way to delay a task?
Reply With Quote #6

Quote:
Originally Posted by Shattered Heart Lynx View Post
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?
__________________
Retired.
Xalus is offline
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 01-21-2016 , 08:22   Re: Any way to delay a task?
Reply With Quote #7

Quote:
Originally Posted by Xalus View Post
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.
__________________

Last edited by gabuch2; 01-21-2016 at 08:24.
gabuch2 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-21-2016 , 08:34   Re: Any way to delay a task?
Reply With Quote #8

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.
__________________
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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