Frictional Games Forum (read-only)
[SCRIPT] Math problem - Percentage [SOLVED] - 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: [SCRIPT] Math problem - Percentage [SOLVED] (/thread-25998.html)



Math problem - Percentage [SOLVED] - FlawlessHappiness - 08-30-2014

Hello.

I'm trying to create a script that finds the percentage of something.

So I came up with this:

PHP Code:
int PercentageXY(int &in xint &in y)
{
    return 
int((x/y)*100);


And I'm using it like this:

PHP Code:
void RunTimer(string &in asTimer)
{
    if(
asTimer == "LeaveBattle")
    {
        
SetPlayerActive(false);
        
AfterBattle();
        
AddTimer("Exp"1"ExperienceTimer");
    }
    
    if(
asTimer == "AttemptRun")
    {
        
SetLocalVarInt("AttemptRunVar"RandInt(1,100));
        
        if(
GetGlobalVarInt("FriendlyMonsterLevel") > GetLocalVarInt("EnemyMonsterLevel"))
        {
            
SetLocalVarInt("LevelDifferenceRun"Floor(PercentageXY(GetLocalVarInt("EnemyMonsterLevel"), GetGlobalVarInt("FriendlyMonsterLevel"))));
            
            
AddDebugMessage("Difference in percentage = "+GetLocalVarInt("LevelDifferenceRun"), false);
            
            if(
GetLocalVarInt("AttemptRunVar") < 50*((GetLocalVarInt("LevelDifferenceRun")/100)+1))
            {
                
FadeIn(1);
                
TeleportPlayer("AfterBattle");
                
AfterBattle();
                
SetMessage("Messages""RunSuccess"0);
            }
            else
            {
                
FadeIn(1);
                
TeleportPlayer("CantEscape");
                
SetEntityActive("AreaLeaveBattle"false);
                
SetMessage("Messages""RunFailure"0);
            }
        }
        
        if(
GetGlobalVarInt("FriendlyMonsterLevel") < GetLocalVarInt("EnemyMonsterLevel"))
        {
            
SetLocalVarInt("LevelDifferenceRun"Floor(PercentageXY(GetGlobalVarInt("FriendlyMonsterLevel"), GetLocalVarInt("EnemyMonsterLevel"))));
            
            
AddDebugMessage("Difference in percentage = "+GetLocalVarInt("LevelDifferenceRun"), false);
            
AddDebugMessage(GetGlobalVarInt("FriendlyMonsterLevel")+"/"+GetLocalVarInt("EnemyMonsterLevel")+"* 100 = "+((GetGlobalVarInt("FriendlyMonsterLevel")/GetLocalVarInt("EnemyMonsterLevel"))*100), false);
            
            if(
GetLocalVarInt("AttemptRunVar") < 50*(GetLocalVarInt("LevelDifferenceRun")/100))
            {
                
FadeIn(1);
                
TeleportPlayer("AfterBattle");
                
AfterBattle();
                
SetMessage("Messages""RunSuccess"0);
            }
            else
            {
                
FadeIn(1);
                
TeleportPlayer("CantEscape");
                
SetEntityActive("AreaLeaveBattle"false);
                
SetMessage("Messages""RunFailure"0);
            }
        }
    }


But no matter what the LevelDifferenceRun always returns 0, when it's supposed to return the difference between my level and the enemy level in percentage.

Is my math function wrong?

EDIT
OK I still need help!

Sorry about bumping this quick, but I had an edit that said I didn't need help anymore, but it turns out I still need help.


It's returnin 0 no matter what. Even the debug message returns 0.


RE: Math problem - Percentage - Mudbill - 08-30-2014

Well, there are a bunch of variables here so I won't get too deep into this right now, but if the value returns 0, then it can only mean that either X or Y is 0 (x*0*100=0). Either of your variables are empty.


RE: Math problem - Percentage - FlawlessHappiness - 08-30-2014

Remember it's not x*y it's x/y.

Dividing by 0 would be a bad idea.

Both variables are int levels, and what I'm testing it with is:

FriendlyMonsterLevel = 1
EnemyMonsterLevel = 2 or 3

I know this for sure since I have billboards going active showing the exact level.

In all cases it returns 0.


Also, if you take a look at this line:
PHP Code:
AddDebugMessage(GetGlobalVarInt("FriendlyMonsterLevel")+"/"+GetLocalVarInt("EnemyMonsterLevel")+"* 100 = "+((GetGlobalVarInt("FriendlyMonsterLevel")/GetLocalVarInt("EnemyMonsterLevel"))*100), false); 

it's showing the exact thing happening.

[Image: e721e3953b.jpg]
(It's not showing a paranthesis in the debug message, but there is one, if you look in the code)


RE: Math problem - Percentage - MrBehemoth - 08-30-2014

I bet it's not always 0%, I bet it can also be 100%, 200% etc. if x is greater than y. Here's why....Remember what Cthulhu pointed out in the rounding thread?

If you do a calculation with integers, the answer is always going to be an integer.

Floats: 1.0f / 2.0f = 0.5f
Ints: 1 / 2 = 0

Mathematically, it doesn't make sense, but it's to do with how integers are stored and calculated. Basically it just chops off everything after the point.

You need to cast your variables as floats. Try this:

PHP Code:
int PercentageXY(int &in xint &in y)
{
    return 
int((float(x)/float(y))*100.0f);




RE: Math problem - Percentage - FlawlessHappiness - 08-30-2014

Oh my god you just solved everything!

Amazing!

Yes I knew about the floats and integers, but I didn't know that you could just 'make them' floats for a moment.

I was hoping that even though it was supposed to return an integer it could still calculate with decimals before it rounded off into an integer. Apparently not.

Thank you so much MrBehemoth! Saved me troubles and Monsters can continue in development Wink