SA-MP Forums Archive
Help with key - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Help with key (/showthread.php?tid=596870)



Help with key - vassilis - 23.12.2015

PHP код:
if(HOLDING(KEY_SPRINT))
    {
        if(
armytraining[playerid] == 1)
        {
            
SetPlayerProgressBarValue(playerid,testbar[playerid],GetPlayerProgressBarValue(playerid,bar[playerid])+ 1);
            
UpdatePlayerProgressBar(playerid,testbar[playerid]);
                if(
GetPlayerProgressBarValue(playeridtestbar[playerid]) == 99)
                {
                    
GameTextForPlayer(playerid,"~r~MISSION FAILED",3000,5);
                    
PlayAudioStreamForPlayer(playerid,"http://k003.kiwi6.com/hotlink/7o7bkddi9b/Tornado_Siren_II-Delilah-747233690.mp3");
                    
SetPlayerSpecialAction(playerid,SPECIAL_ACTION_HANDSUP);
                }
           if(
GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_DUCK)
            {
                
SetPlayerProgressBarValue(playerid,testbar[playerid],GetPlayerProgressBarValue(playerid,bar[playerid]) -1);
                
UpdatePlayerProgressBar(playerid,testbar[playerid]);
                if(
GetPlayerProgressBarValue(playeridtestbar[playerid]) == 0)
                {
                    
GameTextForPlayer(playerid,"~G~ keep stealth",3000,5);
                }
            }
        }
    
    
    } 
So what i want to do is when player is holding the sprint key the bar will be updated by +1 as far as he holds the key.
If he stop, then it the progress bar should stop updating .
If he crouch, then the progress bar should be updated by removing - 1
With this way crouch is not working, plus holding key for sprint just updating + 1 only once ..


Re: Help with key - prineside - 23.12.2015

This callback is called when player press or release one of GTA's defined keys.
You should:
1. Store current pressed keys of each player somewhere (in global variable)
2. Update this variable in the OnPlayerKetStateChange callback
3. Call your handler function each one second (SetTimer) and check if player holds sprint button using that global variable


Re: Help with key - Gammix - 24.12.2015

Holding is only called once. Create a timer/OnPlayerUpdate* for updating (under OnPlayerKeyStateChange):
pawn Код:
if (HOLDING(KEY_SPRINT))
{
    timerid[playerid] = SetTimerEx("OnUpdate", 1000, true, "ii", playerid, 1);
}
pawn Код:
forward OnUpdate(playerid, value);
public   OnUpdate(playerid, value)
{
    your_variable[playerid] += value;
    // update progress bar
}
And using this to reduce update by -1 (within the timer):
pawn Код:
if (GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_DUCK)
{
    value = -1;
}
And in order to stop update (under OnPlayerKeyStateChange):
pawn Код:
if (RELEASED(KEY_SPRINT))
{
    KillTimer(timerid[playerid]);
}



Re: Help with key - Ritzy2K - 24.12.2015

https://sampforum.blast.hk/showthread.php?tid=490436

OnPlayerHoldingKey will make your life easier


Re: Help with key - Vince - 24.12.2015

That's not really necessary. Gammix has the right idea, but the timer should be set under PRESSED and killed under RELEASED. Then for the crouching, you can also use KEY_CROUCH;

PHP код:
if(PRESSED(KEY_SPRINT))
{
    
timer[playerid] = SetTimerEx(...);
}

if(
RELEASED(KEY_SPRINT))
{
    
KillTimer(timer[playerid]);
}

if(
PRESSED(KEY_CROUCH)) // when crouch is pressed
{
    if(
HOLDING(KEY_SPRINT)) // if key sprint was already held in when key crouch was pressed
    
{

    }
    else
    {

    }




Re: Help with key - vassilis - 24.12.2015

Quote:
Originally Posted by Vince
Посмотреть сообщение
That's not really necessary. Gammix has the right idea, but the timer should be set under PRESSED and killed under RELEASED. Then for the crouching, you can also use KEY_CROUCH;

PHP код:
if(PRESSED(KEY_SPRINT))
{
    
timer[playerid] = SetTimerEx(...);
}
if(
RELEASED(KEY_SPRINT))
{
    
KillTimer(timer[playerid]);
}
if(
PRESSED(KEY_CROUCH)) // when crouch is pressed
{
    if(
HOLDING(KEY_SPRINT)) // if key sprint was already held in when key crouch was pressed
    
{
    }
    else
    {
    }

I can't understand it. So where should i put progress bar to be updated? at the function or what?
Should i work it like that:
PHP код:
    if(PRESSED(KEY_SPRINT))
    {
        
holdingkey[playerid] = SetTimerEx("HoldingSprintKey"1000true"ii"playerid1);
    }
    if(
RELEASED(KEY_SPRINT))
    {
        
KillTimer(holdingkey[playerid]);
    }
    if(
PRESSED(KEY_CROUCH)) // when crouch is pressed
    
{
        if(
HOLDING(KEY_SPRINT)) // if key sprint was already held in when key crouch was pressed
        
{
        }
        else
        {
        }
    } 
Function
PHP код:
function HoldingSprintKey(playerid,value)
{
    if(
armytraining[playerid] == 1
        { 
            
SetPlayerProgressBarValue(playerid,testbar[playerid], GetPlayerProgressBarValue(playerid,testbar[playerid])+value);
            
UpdatePlayerProgressBar(playerid,testbar[playerid]); 
                if(
GetPlayerProgressBarValue(playeridtestbar[playerid]) == 99
                { 
                    
GameTextForPlayer(playerid,"~r~MISSION FAILED",3000,5); 
                    
PlayAudioStreamForPlayer(playerid,"http://k003.kiwi6.com/hotlink/7o7bkddi9b/Tornado_Siren_II-Delilah-747233690.mp3"); 
                    
SetPlayerSpecialAction(playerid,SPECIAL_ACTION_HANDSUP); 
                } 
           if(
GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_DUCK
            { 
                
SetPlayerProgressBarValue(playerid,testbar[playerid],GetPlayerProgressBarValue(playerid,bar[playerid]) -value); 
                
UpdatePlayerProgressBar(playerid,testbar[playerid]); 
                if(
GetPlayerProgressBarValue(playeridtestbar[playerid]) == 0
                { 
                    
GameTextForPlayer(playerid,"~G~ keep stealth",3000,5); 
                } 
            } 
        } 
    return 
1;




Re: Help with key - vassilis - 26.12.2015

PHP код:
    if(PRESSED(KEY_SPRINT))
    {
        if(
armytraining[playerid] == 1
        { 
            
holdingkey[playerid] = SetTimerEx("HoldingSprintKey"500true"i"playerid);
        }    
    }

    if(
RELEASED(KEY_SPRINT))
    {
        
KillTimer(holdingkey[playerid]);
    }

    if(
PRESSED(KEY_CROUCH)) // when crouch is pressed
    
{
        if(
crouching[playerid] == 0)
        { 
            
crouching[playerid] = 1;        
            
holdingkey[playerid] = SetTimerEx("PlayerCrouching"500true"i"playerid);
        }
        else if(
crouching[playerid] == 1)
        {
            
crouching[playerid] = 0;
            
KillTimer(holdingkey[playerid]);
        }
    }  

 
        return 
1;
}

function 
HoldingSprintKey(playerid)
{
            
SetPlayerProgressBarValue(playerid,testbar[playerid], GetPlayerProgressBarValue(playerid,testbar[playerid])+3);
            
UpdatePlayerProgressBar(playerid,testbar[playerid]); 
                if(
GetPlayerProgressBarValue(playeridtestbar[playerid]) >= 99
                { 
                    
GameTextForPlayer(playerid,"~r~MISSION FAILED",3000,5); 
                    
PlayAudioStreamForPlayer(playerid,"http://k003.kiwi6.com/hotlink/7o7bkddi9b/Tornado_Siren_II-Delilah-747233690.mp3"); 
                    
SetPlayerSpecialAction(playerid,SPECIAL_ACTION_HANDSUP); 
                }  
    return 
1;
}

function 
PlayerCrouching(playerid)
{
    if(
GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_DUCK
    { 
        
SetPlayerProgressBarValue(playerid,testbar[playerid],GetPlayerProgressBarValue(playerid,bar[playerid]) -3); 
        
UpdatePlayerProgressBar(playerid,testbar[playerid]); 
        if(
GetPlayerProgressBarValue(playeridtestbar[playerid]) == 0
        { 
                    
GameTextForPlayer(playerid,"~G~ keep stealth",3000,5); 
        } 
    }
    return 
1;

So i did another way trying to mix both to understand how it works and The result is that it updates progress bar as value +3 at times i hold sprint but at times i am crouched it resets the progress bar to 0 and shows GameText Keep Stealth


Re: Help with key - SaltySandy - 26.12.2015

You use the KillTimer function. The script is doing what it is told to do. Replace that with what you want it to do.


Re: Help with key - vassilis - 27.12.2015

Didn't really help what you said. My mind is frozen if you could give me a snippet of how i could fix it, i would be glad!


Re: Help with key - vassilis - 28.12.2015

Bump!