Author Topic: "String always evaluates to 0" and finding a ratio  (Read 1560 times)

When ever I try to execute an add-on I made (for megashot and giving more stats about screen resolution) always gives this in the console when it checks:
Code: [Select]
Client checking Add-On: Client_Winshot
Loading Add-On: Client_Winshot (CRC:-1582617125)
Add-Ons/Client_Winshot/client.cs (13): string always evaluates to 0.
Add-Ons/Client_Winshot/client.cs (13): string always evaluates to 0.
Executing Add-Ons/Client_Winshot/client.cs.
Add-Ons/Client_Winshot/client.cs (13): string always evaluates to 0.
Heres the code
Code: [Select]
function setmegashot(%num)
{
 if(%num<=8 && %num>=0.2)
 {
    $megashotScaleFactor = %num;
    %hres = getword($pref::Video::resolution,0);
    %vres = getword($pref::Video::resolution,1);
    %hr = mCeil(%hres - 0.5);
    %vr = mCeil(%vres - 0.5);
    echo("Megashot "@%num@" | "@%hr@" x "@%vr);
    %ratio = %hr/%vr;
    //%rat = getratio(%ratio);
    if(%rat=="")
    {
     %rat=%ratio;
    }
    echo("Aspect Ratio "@%rat);
 }
 else
 {
    echo("Invalid Value");
 }
}
function getratio(%ratio)
{
 for(%i=1;%i<45;%i++)
 {
  %raat = %ratio*%i;
  %rate = mCeil(%raat - 0.5);
  %diff = %raat - %rate;
  if(%diff <= 0.1)
  {
   return %rate@":"@%i;
  }
 }
}
function getres()
{
    %hr = getword(1,$pref::Video::resolution);
    %vr = getword(2,$pref::Video::resolution);
    echo(%hr@" x "@%vr);
    %rat = %hr/%vr;
    echo("Aspect Ratio "@%rat);
    return %hr@" "@%vr;
}
I commented out the part where it calls getratio(//%rat = getratio(%ratio);) because I though that was what was causing the problem. If anyone could tell me what I am doing wrong and also tell me how to make getratio work, I would really appreciate it.

What I'm trying to make getratio do is you give it a ratio over one (like 1.777... which is 16:9) for example and attempt to find 16:9. I don't know if there's a function to turn 1.33333:1 into 4:3, so I tried making one.

%rat == "" should be %rat $= ""

Crycigarette! :D ^ is correct btw


edit:

This is unforgivable.
« Last Edit: July 01, 2011, 03:21:35 AM by TripleNickels »

What I'm trying to make getratio do is you give it a ratio over one (like 1.777... which is 16:9) for example and attempt to find 16:9. I don't know if there's a function to turn 1.33333:1 into 4:3, so I tried making one.

I'm not exactly sure what I did but this turns 1.333 into 4:3 and 1.777 into 16:9.

Code: [Select]
function getRatio(%width)
{
%multi = 2 / (mCeil(%width) - %width);
return mFloatLength(%width * %multi,0) @ ":" @ mFloatLength(%multi,0);
}

I'm not exactly sure what I did but this turns 1.333 into 4:3 and 1.777 into 16:9.

Code: [Select]
function getRatio(%width)
{
%multi = 2 / (mCeil(%width) - %width);
return mFloatLength(%width * %multi,0) @ ":" @ mFloatLength(%multi,0);
}

What if an integer troll makes the game divide by zero?

I just tried 2.25
%multi = 2 / (3-2.25) = 2.666667
Ratio would return 6:2
good for approximating though, I suppose.
« Last Edit: July 06, 2011, 06:26:30 PM by Nexus »

Well here's what I got, doesn't work very well:
Code: [Select]
function mHighestCommonDenominator(%x,%y)
{
if(%x == %y)
return %x;
while(%x != 0 && %y != 0)
{
%ox = %x;
%oy = %y;

%x = %oy % %ox;
%y = %ox;
echo(%ox SPC %oy);
}
return %x == 0 ? %y : %x;
}
function getRatio(%width,%height)
{
%ratio = %width / %height;

%lhs = getSubStr(%ratio,0,7); //get the first 5 digits, ie. 0.*****
%lhs *= 100;
%rhs = getSubStr(%ratio,0,5); //first 3 digits

%rational = %lhs - %rhs; //%rational / 99 now == %ratio

%denominator = mHighestCommonDenominator(mFloatLength(%rational,0),99);
return mFloatLength(%rational / %denominator,0) @ ":" @ 99 / %denominator;
        //works for widescreens if I do return mFloatLength(%rational / 11,0) @ ":" @ 9;
}

Also, so you know, getRes is already defined by the game.

What if an integer troll makes the game divide by zero?
It'll return 0:0.

It'll return 0:0.
But wouldn't 2 / (mCeil(0) - 0) be 2 / 0, which would crash it.

But wouldn't 2 / (mCeil(0) - 0) be 2 / 0, which would crash it.
Type echo(2/0); in your console and see if it crashes you.

Oh I already solved this, yet the function I made checks every denominator and sees if it gets a fraction 0.000001 from the number you enter.
Code: [Select]
function getratio(%ratio,%i)
{
 if(%i $="")
 {
  %i=1;
 }
 else if(%i<99999)
 {
  %raat = %ratio*%i;
  %rate = mCeil(%raat - 0.5);
  %diff = %raat - %rate;
  if(%diff <= 0.000001 && %diff >= -0.000001)
  {
   %raat = mCeil(%raat - 0.5);
   echo(%raat@":"@%i);
   return %raat@":"@%i;
  }
  else
  {
   getratio(%ratio,%i++);
  }
 }
 else
 {
   echo("Denominator is greater than 100000");
   echo(%ratio@":1");
   return %ratio@":1";
 }
}


Quote
What if an integer troll makes the game divide by zero?

I just tried 2.25
%multi = 2 / (3-2.25) = 2.666667

That's only half of it - look at the "return" statement.

mFloatLength(%width * %multi,0) : mFloatLength(%multi,0)
mFloatLength(2.25 * 2.666667,0) : mFloatLength(2.666667,0)
mFloatLength(6.00000075,0)      : 2
                              6 : 2


(actual string "6:2" returned)

Ratio would return 6:2

That's only half of it - look at the "return" statement.

(actual string "6:2" returned)

I'm a little confused, did you overlook that or am I overlooking what you are saying?

You're overlooking what he's saying.