AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help set_task (https://forums.alliedmods.net/showthread.php?t=313055)

tolpecek 12-26-2018 16:54

Help set_task
 
Hello
it looks like set_task ignores a delay or idk.. code:
Code:

plugin_init()
{
RegisterHam(Ham_Spawn, "player", "zaciatok_kola", 1 )
}

Code:

public zaciatok_kola(id)
{
        set_task(2,"Task_HPRegenLoop",id,_,_,"b")
        set_task(2,"uzdraveniloop",id,_,_,"b")
}

Code:

public Task_HPRegenLoop(id)
{
        if(g_item_reg[id] && cs_get_user_team(id)== CS_TEAM_CT && is_user_alive(id))
        {
                if((get_user_health(id) >= 500))
                {
                        set_user_health(id, 500)
                }
                else
                {
                        set_user_health(id, get_user_health(id) + 5)
                }
        }
        else
        {
                set_user_health(id, get_user_health(id))
        }
}
public uzdraveniloop(id)
{
        new float:addhealths = MaxHP[id]*0.05
        if(g_item_sti[id] && cs_get_user_team(id) == CS_TEAM_T && is_user_alive(id))
        {
                if(!(get_user_health(id) >= MaxHP[id]))
                        set_user_health(id, get_user_health(id) + floatround(addhealths))
                        if(get_user_health(id) > MaxHP[id])
                                set_user_health(id, floatround(MaxHP[id]))
        }
        else
        {
                set_user_health(id, get_user_health(id))
        }
}

adding health works ok but that delay:( thanks

Bugsy 12-26-2018 17:50

Re: Help set_task
 
1. set_task() expects a float for the interval, use 2.0 instead of 2.
2. What is the value of g_item_sti[]?
3. Add an is_user_alive() check before executing the set_task() calls in zaciatok_kola().
4. You can reduce code (which also fixes a player potentially getting over MaxHP[] in the first if-statement if a player has MaxHP[]-1 hp and then it adds [addhealths]):

Replace
PHP Code:

if(!(get_user_health(id) >= MaxHP[id]))
    
set_user_health(idget_user_health(id) + floatround(addhealths))
        
if(
get_user_health(id) > MaxHP[id]) 
    
set_user_health(idfloatround(MaxHP[id])) 

With
PHP Code:

set_user_healthid clampget_user_health(id) + floatround(addhealths) , MaxHP[id] ) ); 


tolpecek 12-26-2018 18:08

Re: Help set_task
 
Quote:

Originally Posted by Bugsy (Post 2631150)
1. set_task() expects a float for the interval, use 2.0 instead of 2.
2. What is the value of g_item_sti[]?
3. Add an is_user_alive() check before executing the set_task() calls in zaciatok_kola().
4. You can reduce code (which also fixes a player potentially getting over MaxHP[] in the first if-statement if a player has MaxHP[]-1 hp and then it adds [addhealths]):

Replace
PHP Code:

if(!(get_user_health(id) >= MaxHP[id]))
    
set_user_health(idget_user_health(id) + floatround(addhealths))
        
if(
get_user_health(id) > MaxHP[id]) 
    
set_user_health(idfloatround(MaxHP[id])) 

With
PHP Code:

set_user_healthid clampget_user_health(id) + floatround(addhealths) , MaxHP[id] ) ); 



g_item_sti[id] is bool, item in game from shop and it checks if it is bought or not
Thanks for reply, gonna test it

tolpecek 12-26-2018 18:18

Re: Help set_task
 
The first function, task_hpregenloop , is doing wierd things, it adds hp twice and then wait for 2 second..
so itīs like
set_user_health(id, get_user_health(id) + 5)
set_user_health(id, get_user_health(id) + 5)
wait(2sec)
set_user_health(id, get_user_health(id) + 5)
set_user_health(id, get_user_health(id) + 5)
wait(2sec) ... :D

tolpecek 12-26-2018 18:25

Re: Help set_task
 
[SOLVED] Just added if(is_user_alive(id) like you said and it works thanks

tolpecek 12-26-2018 18:45

Re: [SOLVED]Help set_task
 
Sorry for spam, but it doesnt work again, it worked for a while and now again same problem :/

Bugsy 12-26-2018 19:05

Re: Help set_task
 
Its hard to troubleshoot code without having the full code. Based on what you provided, my suggestions should have fixed the issue.

Another suggestion, use the same function and call set_task() only once.
PHP Code:

public Task_HPRegenLoop(id)
{
    if ( 
is_user_aliveid ) )
    {
        new 
CsTeams:ctTeam cs_get_user_team(id);
        
        new 
iHealth get_user_healthid );
        
        if(
g_item_reg[id] && ctTeam== CS_TEAM_CT )
        {
            
//if((get_user_health(id) >= 500))
            //{
            //    set_user_health(id, 500)
            //}
            //else
            //{ 
            //    set_user_health(id, get_user_health(id) + 5)
            //}
            
            
if ( iHealth 500 )
                
set_user_health(idclampget_user_healthid ) + 500 ) )
        }
        else if( 
g_item_sti[id] && ctTeam == CS_TEAM_T )
        {    
            new 
Float:addhealths MaxHPid ] * 0.05
            
            
//if( !( iHealth >= MaxHP[id] ) )
            //    set_user_health( id , iHealth + floatround( addhealths ) )
            //    if(get_user_health(id) > MaxHP[id]) 
            //        set_user_health(id, floatround(MaxHP[id]))
            
            
if ( iHealth MaxHPid ] )
                
set_user_health(idclampiHealth floatroundaddhealths ) , 1MaxHP[id] ) )
        }
        
        
//This is a complete waste:
        //else 
        //{
        //    set_user_health(id, get_user_health(id))
        //}
    
}
    
    
//This is a complete waste:
    //else 
    //{
    //    set_user_health(id, get_user_health(id))
    //}



tolpecek 12-26-2018 19:32

Re: Help set_task
 
I think i found a problem, it works only if i am alone on the server, when we are2, it goes twice faster and if 3 then 3 times faster :D

OciXCrom 12-27-2018 07:07

Re: Help set_task
 
It's because you're not removing the task when the client disconnects, so the same task is assigned to the same client id and is executed multiple times. Another reason can be that you're using 0 as the client index.

LondoN 12-27-2018 07:14

Re: Help set_task
 
Code:

public Task_HPRegenLoop ( id )
{
        if ( !is_user_alive ( id ) || !g_item_reg [ id ] )
                return;

        new Team = cs_get_user_team ( id );
        new Health = get_user_health ( id );

        switch ( Team )
        {
                case CS_TEAM_CT:        set_user_health ( id, clamp ( get_user_health ( id ) + 5, 1, 500 ) );
                case CS_TEAM_T:                set_user_health ( id, clamp ( get_user_health ( id ) + MaxHP [ id ] * 0.05, 1, MaxHP [ id ] ) )
        }
}

i think thats its very simple.
you can try first to check g_item_reg[id] and user alive then store team and hp then make switch.


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

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