SA-MP Forums Archive
Problem with Hold key to refuel. - 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: Problem with Hold key to refuel. (/showthread.php?tid=519763)



Problem with Hold key to refuel. - iOxide - 15.06.2014

I am trying to make the refuel system like when player is holding down the 'Fire' key, it will increase the fuel slowly and TogglePlayerControllable to 0 and if they release the button, it will take the refuel price from player then TogglePlayerControllable to 1. Its also taking money from players repeatedly.

My problem is, when they are holding the button, its spamming the chat with the successful refuel message and gametext + Toggling player controllable 0 and 1 every seconds. What is the problem here? Can anyone explain?

pawn Код:
#define HOLDING(%0) \
    ((newkeys & (%0)) == (%0))
#define RELEASED(%0) \
    (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
pawn Код:
if (IsPlayerInAnyVehicle(playerid) && GetPlayerVehicleSeat(playerid) == 0)
    {
        for (new i; i < sizeof(RefuelPickups); i++)
        {
            if(IsPlayerInRangeOfPoint(playerid, 2.5, RefuelPickups[i][pux], RefuelPickups[i][puy], RefuelPickups[i][puz]))
            {
                if(HOLDING(KEY_FIRE))
                {
                    if(pInfo[playerid][PlayerMoney] <= RefuelPrice) return SendClientMessage(playerid, RED, "You don't have enough money to refuel!");
                    TogglePlayerControllable(playerid, 0);
                    VehicleInfo[vehicleid][Fuel]++;
                    GameTextForPlayer(playerid, "~g~Refuelling your vehicle...", 3000, 5);
                    break;
                }
            }
        }
    }
    if(RELEASED(KEY_FIRE))
    {
        RewardPlayer(playerid, -RefuelPrice, 0);
        TogglePlayerControllable(playerid, 1);
        format(RefuelMsg, 128, "Vehicle refuelled! Refuel cost: $%i", RefuelPrice);
        SendClientMessage(playerid, BLUE, RefuelMsg);
    }
Fuel is increasing in fine way like i wanted. Only problem is that the TogglePlayerControllable, RewardPlayer and ClientMessage is executing everytime.


Re : Problem with Hold key to refuel. - S4t3K - 15.06.2014

Well, try replacing "if(HOLDING" by "while(HOLDING"


Re: Problem with Hold key to refuel. - iOxide - 15.06.2014

Nope, didn't work. Its still the same. ._.


Re : Problem with Hold key to refuel. - Chipardeur - 15.06.2014

That ?
pawn Код:
if (IsPlayerInAnyVehicle(playerid) && GetPlayerVehicleSeat(playerid) == 0)
{
    for (new i; i < sizeof(RefuelPickups); i++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 2.5, RefuelPickups[i][pux], RefuelPickups[i][puy], RefuelPickups[i][puz]))
        {
            if(HOLDING(KEY_FIRE))
            {
                if(pInfo[playerid][PlayerMoney] <= RefuelPrice) return SendClientMessage(playerid, RED, "You don't have enough money to refuel!");
                TogglePlayerControllable(playerid, 0);
                VehicleInfo[vehicleid][Fuel]++;
                GameTextForPlayer(playerid, "~g~Refuelling your vehicle...", 3000, 5);
                break;
            }
            if(RELEASED(KEY_FIRE))
            {
                RewardPlayer(playerid, -RefuelPrice, 0);
                TogglePlayerControllable(playerid, 1);
                format(RefuelMsg, 128, "Vehicle refuelled! Refuel cost: $%i", RefuelPrice);
                SendClientMessage(playerid, BLUE, RefuelMsg);
            }
        }
    }
}



Re: Problem with Hold key to refuel. - iOxide - 15.06.2014

Ugh, nope. Same problem. I have tried something like that before. Almost tried like 5 ways and its still spamming the shit out of my chat. The fuel is refuelling fine, only the Message, RewardPlayer and TogglePlayerControllable are the same. :\


Re: Problem with Hold key to refuel. - Lidor124 - 15.06.2014

I'm not sure but TogglePlayerControllable function freezes the player, which means that player's control are uncontrollable.


Re: Problem with Hold key to refuel. - iOxide - 15.06.2014

Quote:
Originally Posted by Lidor124
Посмотреть сообщение
I'm not sure but TogglePlayerControllable function freezes the player, which means that player's control are uncontrollable.
Well, i guess you haven't read my post fully. I said, TogglePlayerControllable is enabling and disabling every seconds while holding down the fire key.


Re: Problem with Hold key to refuel. - Konstantinos - 15.06.2014

pawn Код:
// global:
new PlayerTimer_Fuel[MAX_PLAYERS];

// OnPlayerConnect:
PlayerTimer_Fuel[playerid] = -1;

// OnPlayerKeyStateChange:
if(HOLDING(KEY_FIRE))
{
    if (!GetPlayerVehicleSeat(playerid))
    {
        for (new i; i < sizeof(RefuelPickups); i++)
        {
            if(IsPlayerInRangeOfPoint(playerid, 2.5, RefuelPickups[i][pux], RefuelPickups[i][puy], RefuelPickups[i][puz]))
            {
                if(pInfo[playerid][PlayerMoney] < RefuelPrice) return SendClientMessage(playerid, RED, "You don't have enough money to refuel!");
                TogglePlayerControllable(playerid, 0);
                PlayerTimer_Fuel[playerid] = SetTimerEx("RefuelVehicleForPlayer", 1000, true, "ii", playerid, GetPlayerVehicleID(playerid));
                break;
            }
        }
    }
}
if(RELEASED(KEY_FIRE))
{
    if (PlayerTimer_Fuel[playerid] != -1)
    {
        RewardPlayer(playerid, -RefuelPrice, 0);
        TogglePlayerControllable(playerid, 1);
        format(RefuelMsg, 128, "Vehicle refuelled! Refuel cost: $%i", RefuelPrice);
        SendClientMessage(playerid, BLUE, RefuelMsg);
        KillTimer(PlayerTimer_Fuel[playerid]);
        PlayerTimer_Fuel[playerid] = -1;
    }
}

forward RefuelVehicleForPlayer(playerid, vehicleid);
public RefuelVehicleForPlayer(playerid, vehicleid)
{
    VehicleInfo[vehicleid][Fuel]++;
    GameTextForPlayer(playerid, "~g~Refuelling your vehicle...", 3000, 5);
}



Re: Problem with Hold key to refuel. - iOxide - 15.06.2014

Edit: Nevermind its working now. Thanks a lot Konstantinos.


Re: Problem with Hold key to refuel. - Vince - 15.06.2014

This is not at all what the HOLDING macro implies. That is only triggered when a player is already holding the specific button and then pressing another button. You only need PRESSED and RELEASED.