Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rounding off.
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#1
Rounding off.

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/th...ight=round
But did not understand the answer.

Can you explain to me how it works?

Trying is the first step to success.
08-24-2014, 04:33 PM
Find
Romulator Offline
Not Tech Support ;-)

Posts: 3,628
Threads: 63
Joined: Jan 2013
Reputation: 195
#2
RE: Rounding off.

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: (Select All)
int floor(float &in x) { return int(x); } 

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

Discord: Romulator#0001
[Image: 3f6f01a904.png]
(This post was last modified: 08-24-2014, 04:56 PM by Romulator.)
08-24-2014, 04:55 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#3
RE: Rounding off.

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: (Select All)
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.

(This post was last modified: 08-25-2014, 12:25 AM by Mudbill.)
08-24-2014, 05:55 PM
Find
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#4
RE: Rounding off.

What, so the code

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

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

Trying is the first step to success.
08-24-2014, 07:07 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#5
RE: Rounding off.

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

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

    
SetPlayerHealth(fHealth); 
}

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


(This post was last modified: 08-24-2014, 07:58 PM by Mudbill.)
08-24-2014, 07:58 PM
Find
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#6
RE: Rounding off.

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

Trying is the first step to success.
08-24-2014, 10:27 PM
Find
MrBehemoth Offline
Senior Member

Posts: 408
Threads: 19
Joined: Feb 2014
Reputation: 40
#7
RE: Rounding off.

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

(This post was last modified: 08-24-2014, 11:16 PM by MrBehemoth.)
08-24-2014, 11:16 PM
Find
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#8
RE: Rounding off.

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

(.4 gets eaten, 2 survives the conversion)

You can also do:

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

What Behe said.

(This post was last modified: 08-24-2014, 11:41 PM by Daemian.)
08-24-2014, 11:40 PM
Find
MrBehemoth Offline
Senior Member

Posts: 408
Threads: 19
Joined: Feb 2014
Reputation: 40
#9
RE: Rounding off.

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

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

(This post was last modified: 08-30-2014, 02:01 PM by MrBehemoth.)
08-25-2014, 12:25 AM
Find
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#10
RE: Rounding off.

Ok, so this is interesting.

This is how my script looks.

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

Damage calculation
PHP Code: (Select All)
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.

Trying is the first step to success.
(This post was last modified: 08-25-2014, 12:46 PM by FlawlessHappiness.)
08-25-2014, 12:00 PM
Find




Users browsing this thread: 1 Guest(s)