Error compiling this piece of code... [SOLVED]

Started by Sparkplug.Creations, Sun 27/07/2008 21:48:07

Previous topic - Next topic

Sparkplug.Creations

Hello,

I'm getting error when I use the following code in the repeatedly execute funtion of my room:

Code: ags

intDistObject01 = Maths.Sqrt((oClearArea.X - oTestObject01.X)*(oClearArea.X - oTestObject01.X)*(oClearArea.Y - oTestObject01.Y)*(oClearArea.Y - oTestObject01.Y));

lblDistObj01.Text = String.Format("Distance: %d",intDistObj01);


The error is:

Quote
Failed to save room room12.crm; details below
room12.asc(524): Error (line 524): Type mismatch: cannot convert 'int' to 'float'

Truthfully, I copied this code from another thread here and changed the object names to my own. I am trying to caculate the distance between objects and display the distance in a label. Ultimately, I want to fade out an object the further away it is using transparency.

I have tried using a float variable to save the result of the equation and tried using FloatToInt() on the equation, saving it to an int variable, but neither worked.

I appreciate your time, thanks,

-Reid
Reid Kimball - Game Designer
__________________________________
Sparkplug Creations - Play for a Change!
http://sparkplugcreations.org/

Khris

#1
Code: ags
intDistObject01 = Maths.Sqrt( IntToFloat( (oClearArea.X - oTestObject01.X)*(oClearArea.X - oTestObject01.X) + (oClearArea.Y - oTestObject01.Y)*(oClearArea.Y - oTestObject01.Y) ) );
//                      note it's IntToFloat, not FloatToInt                                     note the + ^


As a side note, keep in mind that an object is positioned using its bottom left corner.

Sparkplug.Creations

#2
Thanks, now I have:

Code: ags

intDistObject01 = IntToFloat(Maths.Sqrt((oClearArea.X - oTestObject01.X)*(oClearArea.X - oTestObject01.X)+(oClearArea.Y - oTestObject01.Y)*(oClearArea.Y - oTestObject01.Y)));
lblDistObj01.Text = String.Format("Distance: %d",intDistObj01);


and I still get the same error msg:

Quote
Failed to save room room12.crm; details below
room12.asc(524): Error (line 524): Type mismatch: cannot convert 'int' to 'float'

Also, why do I need to find the sqrt of the main equation? Doesn't the main equation give me the distance? The square root will make the distance number smaller I think. If that's the case, then I think I can get this work since it won't be creating floats.
Reid Kimball - Game Designer
__________________________________
Sparkplug Creations - Play for a Change!
http://sparkplugcreations.org/

Gilbert

That's because the output of Maths.Sqrt() is a float not an int, you should not write use IntToFloat() there. But instead:
1. As the varables involved in the expression inside Maths.Sqrt() are of the int type (object coordinates), the result will be an int.  You need to convert the result to a float so that Maths.Sqrt() will accept it as a parameter. KhrisMUC had already nailed this down, why don't you follow?
2. Because the output of Maths.Sqrt() is a float not an int, and you want to assign it to intDistObject01, which (I guess) is an int, so you need to use FloatToInt() to convert the result after taking square root into an int.

i.e.
Code: ags

intDistObject01 = FloatToInt(Maths.Sqrt(IntToFloat((oClearArea.X - oTestObject01.X)*(oClearArea.X - oTestObject01.X)+(oClearArea.Y - oTestObject01.Y)*(oClearArea.Y - oTestObject01.Y))));
lblDistObj01.Text = String.Format("Distance: %d",intDistObj01);


Sparkplug.Creations

When I read Khris' post I missed seeing the "IntToFloat" was inside the Maths.sqrt function and not outside of it. I fixed that, but then read Gilbot's post about using FloatToInt outside everything to match it to my variable and now everything works beautifully.

Next I need to figure out how to convert the distance value to a number that can be used for setting transparency on the object.

Thanks for your help Khris and Gilbot!
Reid Kimball - Game Designer
__________________________________
Sparkplug Creations - Play for a Change!
http://sparkplugcreations.org/

Khris

Yeah, I forgot to turn the Sqrt back into an int :)

Quote from: Sparkplug.Creations on Mon 28/07/2008 01:33:57Also, why do I need to find the sqrt of the main equation? Doesn't the main equation give me the distance? The square root will make the distance number smaller I think. If that's the case, then I think I can get this work since it won't be creating floats.
According to Pythagoras, it's c² = a² + b². c is the distance you're looking for, so you'll indeed need the square root of a² + b².
Little example: think of two points with a delta x of 30 and a delta y of 40: c² = 30² + 40² = 2500.
Seems a bit big, doesn't it? Yet 50 sounds much more like it.

Btw, the usual method is to convert all initial values to floats (if necessary), then do all calculations, then in the end convert the floats back to ints to display stuff.
It's a) more accurate and b) faster. (CPUs are built to handle floats.)

To convert the distance to a transparency value, you'll need a little more math.
Say you want the following relationship:
Distance = 0 -> Transparency = 0 (opaque)
Distance = x -> Transparency = x (fading)
Distance >= 200 -> Transparency = 100 (invisible)

Code: ags
  int t = intDistObject01/2;
  if (t > 100) t = 100;
  oTestObject01.Transparency = t;

Sparkplug.Creations

Quote from: KhrisMUC on Mon 28/07/2008 10:20:18
To convert the distance to a transparency value, you'll need a little more math.
Say you want the following relationship:
Distance = 0 -> Transparency = 0 (opaque)
Distance = x -> Transparency = x (fading)
Distance >= 200 -> Transparency = 100 (invisible)

Code: ags
  int t = intDistObject01/2;
  if (t > 100) t = 100;
  oTestObject01.Transparency = t;


This is what I wrote before I saw your post Khris and it worked.

Code: ags

intTransTestObj01 = intDistObject01;

  if (intTransTestObj01 < 10)
  {
    intTransTestObj01 = 10;
  }
  else if( intTransTestObj01 > 90)
  {
    intTransTestObj01 = 90;
  }
  else
  {
    oTestObject01.Transparency = intTransTestObj01;
  }


Your approach to divide the distance by 2 works better, considering on the screen the distances can be 200+ and that doesn't match up 1:1 with the transparency values. Yay, I'm nearly done with this!
Reid Kimball - Game Designer
__________________________________
Sparkplug Creations - Play for a Change!
http://sparkplugcreations.org/

SMF spam blocked by CleanTalk