Help with key
#1

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 ..
Reply
#2

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
Reply
#3

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]);
}
Reply
#4

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

OnPlayerHoldingKey will make your life easier
Reply
#5

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
    {

    }

Reply
#6

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;

Reply
#7

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
Reply
#8

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

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!
Reply
#10

Bump!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)