Problem with substracting floats
#1

I want to make a nitrous indicator, and for that I created a variable with the duration.
I wanted to substract every second the nitrous button is pressed so I made a timer with a function.
It wasn't working well, so I included client messages to see the values changing while I keep the button pressed.
The code is below:
PHP код:
new string[64];
new 
amount 0;
new 
one 1;
forward amountfeed(playerid);
public 
amountfeed(playerid)
{
    
format(string,sizeof(string),"%d"amount);
    
SendClientMessage(playerid,0xFFFFFFAA,string);
    
amount floatsub(amountone);  ///////////////////
    
return 1;
}
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    
amount 10;
    
format(string,sizeof(string),"%d"amount);
    
SendClientMessage(playerid,0xFFFFFFAA,string);
    return 
1;
}
public 
OnPlayerExitVehicle(playeridvehicleid)
{
    
amount 0;
}
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if(
IsPlayerInAnyVehicle(playerid))
    {
        new 
timer;
        new 
veh GetPlayerVehicleID(playerid);
        if((
newkeys || newkeys 4))
        {
            
timer SetTimer("amountfeed"1000true);
            if (
amount == 0)
            {
                
KillTimer(timer);
            }
        }
        
        if((
oldkeys || oldkeys 4))
        {
            
KillTimer(timer);
        }
        
    }
    return 
1;

Basically when I enter a car I should get the value 10, I start holding the NOS button, the value should drop to 1 every second.
Clearly something is terribly wrong here, because Instead of 9, 8, 7, etc, I got the following:
BTW I also got a tag mismatch warning on the line marked with ///////////////////

PHP код:
//I entered the vehicle
[08:36:3410
//I started holding click (Nos button of course)
[08:36:4410
[08:36:451091567616
[08:36:461317150720
[08:36:471318913088
[08:36:481318926856
[08:36:501318926964
[08:36:511318926965
[08:36:521318926965
[08:36:531318926965
[08:36:541318926965
[08:36:551318926965
[08:36:561318926965
[08:36:571318926965
[08:36:581318926965
[08:37:001318926965
[08:37:011318926965
[08:37:021318926965
[08:37:031318926965
[08:37:041318926965
[08:37:051318926965
[08:37:061318926965
[08:37:071318926965
[08:37:081318926965
[08:37:091318926965 
Can someone point me in the right direction here?
Reply
#2

There's no need of using floatsub on an integer. You could simply do the following instead of subtracting it using floatsub:
pawn Код:
amount--;
;
Reply
#3

Thank you, that did the trick with the numbers. But now I'm facing another issue.
The timer should stop when I release the nos button or when the value reaches 0, but it doesn't. The values go below 0 and keep dropping. The new code is this:
PHP код:
#define FILTERSCRIPT
#include <a_samp>
#if defined FILTERSCRIPT
new string[64];
new 
amount 0;
new 
timer;
forward amountfeed(playerid);
public 
amountfeed(playerid)
{
    
format(string,sizeof(string),"%d"amount);
    
SendClientMessage(playerid,0xFFFFFFAA,string);
    
amount--;
    if (
amount == 0)
    {
KillTimer(timer);}
    return 
1;
}
public 
OnFilterScriptInit()
{
    print(
"\n--------------------------------------");
    print(
" Blank Filterscript by your name here");
    print(
"--------------------------------------\n");
    return 
1;
}
public 
OnFilterScriptExit()
{
    print(
"--------------------------------------\n");
    return 
1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    
amount 0;
    
KillTimer(timer);
}
public 
OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    
amount 10;
    
format(string,sizeof(string),"%d"amount);
    
SendClientMessage(playerid,0xFFFFFFAA,string);
    return 
1;
}
public 
OnPlayerExitVehicle(playeridvehicleid)
{
    
amount 0;
    
KillTimer(timer);
}
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if(
IsPlayerInAnyVehicle(playerid))
    {
        
        new 
veh GetPlayerVehicleID(playerid);
        if((
newkeys || newkeys 4))
        {
            
timer SetTimer("amountfeed"1000true);
            if (
amount == 0)
            {
                
KillTimer(timer);
            }
        }
        
        if((
oldkeys || oldkeys 4))
        {
            
KillTimer(timer);
        }
        
    }
    return 
1;

Reply
#4

I managed the floats, idk why but incase you need to subtract less than 1. Also try the timer now, i fixed ur bit masking.

Код:
#define RELEASED(%0) \
    (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))


new string[64]; 
new Float:amount = 0; 

forward amountfeed(playerid); 
public amountfeed(playerid) 
{ 
    format(string,sizeof(string),"%f", amount); 
    SendClientMessage(playerid,0xFFFFFFAA,string); 
    amount = floatsub(amount, 1.0);
    return 1; 
} 

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger) 
{ 
    amount = 10.0; 
    format(string,sizeof(string),"%f", amount); 
    SendClientMessage(playerid,0xFFFFFFAA,string); 
    return 1; 
} 

public OnPlayerExitVehicle(playerid, vehicleid) 
{ 
    amount = 0.0; 
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys) 
{ 
    if (IsPlayerInAnyVehicle(playerid)) 
    { 
        new timer; 
        new veh = GetPlayerVehicleID(playerid);

        if ( ((newkeys & 1) && !(oldkeys & 1)) || ((newkeys & 4) && !(oldkeys & 4)) )
        { 
            timer = SetTimer("amountfeed", 1000, true); 
            if (amount < 1) 
            { 
                KillTimer(timer); 
            } 
        } 
         
        if (RELEASED(1) || RELEASED(4))  
        { 
            KillTimer(timer); 
        }
    }
    return 1; 
}
Reply
#5

Yes, the timer appears to be working now, but I just decremented the amount value, no need to subtract.
The amount value doesn't seem to reach 0, tough. It stays at 1.

Edit: solved it guys. Thanks a lot, I added rep to both of you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)