Frictional Games Forum (read-only)
Rounding off. - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Rounding off. (/thread-25947.html)

Pages: 1 2


Rounding off. - FlawlessHappiness - 08-24-2014

Hello.

I searched about rounding off.

If I have decimal, say 2.4, I want to round it down to 2.
If it's 2.7 it should also be rounded down to 2.

I found this thread: https://www.frictionalgames.com/forum/thread-15621.html?highlight=round
But did not understand the answer.

Can you explain to me how it works?


RE: Rounding off. - Romulator - 08-24-2014

Well, rounding down is known as "floor" in most codes.
In the Math Functions page on the wiki, you can find this riiiiight down the bottom, which with a few clicks, is a copy+paste from the links you were looking at.

PHP Code:
int floor(float &in x) { return int(x); } 

Only problem is, I got no idea how to use it Tongue


RE: Rounding off. - Mudbill - 08-24-2014

My guess would be for such a function to work like so:

You input the floor(2.7f) into where you need it to be rounded, and it will return 2. For example:

PHP Code:
int iHealthRounded floor(50.5f); //or whatever number you want to round.
float fHealth iHealthRounded;

SetPlayerHealth(fHealth); 

As you probably know, integers can't contain decimals, whereas floats can. It looks like this script simply converts the float (decimal) value to an integer, which kills off the decimal. I'm unsure if this conversion will automatically assume the closest full number or just ignore the decimal though.

Don't forget to have that line Rom posted somewhere in your file.


RE: Rounding off. - FlawlessHappiness - 08-24-2014

What, so the code

PHP Code:
int floor(float &in x) { return int(x); } 

Actually needs to be in my file, but with nothing editted into it?


RE: Rounding off. - Mudbill - 08-24-2014

Pretty much. It doesn't even need to be named floor. Can be anything. Look at this:

PHP Code:
void Callback()
{
    
int iHealthRounded ConvertFloatToInt(50.5f);
    
float fHealth iHealthRounded;

    
SetPlayerHealth(fHealth); 
}

int ConvertFloatToInt(float &in x)
{
    return 
int(x);




RE: Rounding off. - FlawlessHappiness - 08-24-2014

If this works, my damage multiplier will be awesome! Smile


RE: Rounding off. - MrBehemoth - 08-24-2014

Can't you just use something like
Code:
iHealthRounded = int(50.5f);
?


RE: Rounding off. - Daemian - 08-24-2014

Just go like this:
PHP Code:
int AttackType = ( 2.4 ); 
AttackType=2

(.4 gets eaten, 2 survives the conversion)

You can also do:

PHP Code:
    float CATS 2.5;
    
AddDebugMessage"I have " int(CATS) + " cats"false ); 
I have 2 cats

What Behe said.


RE: Rounding off. - MrBehemoth - 08-25-2014

Yup. Also, protip: if you want to round of to the nearest whole number, use
Code:
MyRoundedInt = int(MyFloat + 0.5f);

Or if you want to round to a given number of decimal places:
Code:
NearestHundred = int((MyFloat + 0.5f) / 100) * 100;
NearestHundredth = float(int((MyFloat + 0.5f) * 100)) / 100;



RE: Rounding off. - FlawlessHappiness - 08-25-2014

Ok, so this is interesting.

This is how my script looks.

Base damage
PHP Code:
if(GetGlobalVarString("Move") == "Dust")
    {
        
SetLocalVarInt("BaseDamage"RandInt(1,2));
    } 

Damage calculation
PHP Code:
if(GetGlobalVarInt("FriendlyMonsterLevel") != GetLocalVarInt("EnemyMonsterLevel"))
        {
            
SetLocalVarInt("levelDifference"GetGlobalVarInt("FriendlyMonsterLevel")-GetLocalVarInt("EnemyMonsterLevel"));
            
            
SetLocalVarFloat("damageMult", (GetLocalVarInt("levelDifference")/3)*0.5);
        }
        else
        {
            
SetLocalVarFloat("damageMult"0);
        }

        
SetLocalVarFloat("Damage"GetLocalVarInt("BaseDamage")+GetLocalVarFloat("DamageMult"));
        if(
GetLocalVarFloat("Damage") <= 0)
        {
            
//SetLocalVarFloat("Damage", 0);
        
}
        
AddDebugMessage("DAMAGE BEFORE = "+GetLocalVarFloat("damage"), false);
        
AddLocalVarInt("FriendlyMonsterHealth", -Floor(GetLocalVarFloat("damage")));
        
AddDebugMessage("DAMAGE = "+GetLocalVarFloat("damage"), false); 

For some reason end damage = 0
It really shouldn't


EDIT: NEVERMIND! I got it to work.