Raised This Month: $ Target: $400
 0% 

Help with float/int enum params


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Netsys
Senior Member
Join Date: Feb 2010
Old 12-15-2015 , 09:58   Help with float/int enum params
Reply With Quote #1

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 
Netsys is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-15-2015 , 20:38   Re: Help with float/int enum params
Reply With Quote #2

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;

__________________

Last edited by fysiks; 12-15-2015 at 20:41.
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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