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
Script Help Math problem - Percentage [SOLVED]
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#1
Math problem - Percentage [SOLVED]

Hello.

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

So I came up with this:

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


And I'm using it like this:

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

Trying is the first step to success.
(This post was last modified: 08-30-2014, 02:23 AM by FlawlessHappiness.)
08-30-2014, 12:48 AM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#2
RE: Math problem - Percentage

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.

08-30-2014, 01:49 AM
Find
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#3
RE: Math problem - Percentage

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

Trying is the first step to success.
(This post was last modified: 08-30-2014, 01:54 AM by FlawlessHappiness.)
08-30-2014, 01:51 AM
Find
MrBehemoth Offline
Senior Member

Posts: 408
Threads: 19
Joined: Feb 2014
Reputation: 40
#4
RE: Math problem - Percentage

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: (Select All)
int PercentageXY(int &in xint &in y)
{
    return 
int((float(x)/float(y))*100.0f);


08-30-2014, 02:18 AM
Find
FlawlessHappiness Offline
Posting Freak

Posts: 3,980
Threads: 145
Joined: Mar 2012
Reputation: 171
#5
RE: Math problem - Percentage

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

Trying is the first step to success.
08-30-2014, 02:21 AM
Find




Users browsing this thread: 1 Guest(s)