AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help with float/int enum params (https://forums.alliedmods.net/showthread.php?t=276110)

Netsys 12-15-2015 09:58

Help with float/int enum params
 
I have more stats, this is just an example. How it should be done so that the float parameters work well?

PHP Code:

#include <amxmodx>

enum _structStats
{
    
FloatstatDamage,
    
statKills
}

new 
g_iStats[structStats];
new 
g_iStatsRound[structStats];

public 
plugin_init()
{
    
register_concmd("float_test""commandTest")
}

public 
commandTestiPlayerID )
{
    
console_print(iPlayerID"[0] statDamage: %.2f - statKills: %d"g_iStats[statDamage], g_iStats[statKills]);

    new 
szStat[2], szValue[12];
    
read_argv(1szStatcharsmax(szStat));
    
read_argv(2szValuecharsmax(szValue));

    if (
str_to_num(szStat) == 0)
    {
        
UpdateStats(statDamagestr_to_float(szValue));
    }
    else
    {
        
UpdateStats(statKillsstr_to_num(szValue));
    }

    
console_print(iPlayerID"[1] statDamage: %.2f - statKills: %d"g_iStats[statDamage], g_iStats[statKills]);
}

UpdateStats( const iStatID, const {Float_}: iValue 1.0 )
{
    
g_iStats[iStatID]      += _iValue;
    
g_iStatsRound[iStatID] += _iValue;


Output:
PHP Code:

11:50:41 float_test 0 "12.1"
11:50:41 [0statDamage0.00 statKills0
         
[1statDamage12.10 statKills0
11
:50:43 float_test 0 "12.1"
11:50:44 [0statDamage12.10 statKills0
         
[1statDamage: -0.00 statKills0
11
:52:44 float_test 0 "12.1"
11:52:44 [0statDamage: -0.00 statKills0
         
[1statDamage: -393.60 statKills0
11
:55:54 float_test 1 "1"
11:55:54 [0statDamage: -393.60 statKills0
         
[1statDamage: -393.60 statKills1
11
:56:00 float_test 1 "3"
11:56:00 [0statDamage: -393.60 statKills1
         
[1statDamage: -393.60 statKills4
11
:56:03 float_test 1 "12.1"
11:56:04 [0statDamage: -393.60 statKills4
         
[1statDamage: -393.60 statKills16 


fysiks 12-15-2015 20:38

Re: Help with float/int enum params
 
Floating-point math and integer math are two very different operations. This is why you have to tag your floating-point variable with "Float:". So, you are doing integer math on floating-point values which will corrupt the floating-point value.

You should do something like this:

PHP Code:

#include <amxmodx>

enum _structStats
{
    
FloatstatDamage,
    
statKills
}

new 
g_iStats[structStats];
new 
g_iStatsRound[structStats];

public 
plugin_init()
{
    
register_concmd("float_test""commandTest")
}

public 
commandTestiPlayerID )
{
    
console_print(iPlayerID"[0] statDamage: %.2f - statKills: %d"g_iStats[statDamage], g_iStats[statKills]);

    new 
szStat[2], szValue[12];
    
read_argv(1szStatcharsmax(szStat));
    
read_argv(2szValuecharsmax(szValue));

    
server_print("%s (%d) ... %s (%f)"szStatstr_to_num(szStat), szValuestr_to_float(szValue))
    
    if (
str_to_num(szStat) == 0)
    {
        
AddDamage(str_to_float(szValue))
    }
    else
    {
        
AddKills(str_to_num(szValue))
    }

    
console_print(iPlayerID"[1] statDamage: %.2f - statKills: %d"g_iStats[statDamage], g_iStats[statKills]);
}

stock AddKills(iKills)
{
    
g_iStats[statKills]      += iKills;
    
g_iStatsRound[statKills] += iKills;
}

stock AddDamage(Float:fDamage)
{
    
g_iStats[statDamage]      += fDamage;
    
g_iStatsRound[statDamage] += fDamage;




All times are GMT -4. The time now is 17:51.

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