Game Development Community

Variable's value is changed when passed to a function

by Matthew thisisunique! · in Technical Issues · 05/21/2011 (12:06 pm) · 2 replies

%a=getWord($currentOverlayPosition, 0);
%b=getWord($currentOverlayPosition, 1);
echo("x: " @ %a);
echo("y: " @ %b);
%tilePosition=%this.getTileGridPosition(%a,%b);

followed by...

function Player::getTileGridPosition(%x, %y)
{
	//%x=getWord($currentOverlayPosition, 0);
	//%y=getWord($currentOverlayPosition, 1);
	echo("now in getTileGridPosition");
	echo("x: " @ %x);
	echo("y: " @ %y);

In this case, both x and y should be 7, just because of where the $currentOverlayPosition is set to. The echos before calling getTileGridPosition show that indeed, they equal 7. However, the echoes within getTileGridPosition show that y is still 7, but x is 1368. As you can imagine, this completely messes up the position the function is supposed to be calculating. I could work around this by calculating %x and %y within getTileGridPosition, but I would also like to use this function to calculate other positions as well. What is going on here?

About the author

Recent Threads


#1
05/21/2011 (12:33 pm)
Which engine are you using?

1368 looks/seems like an object id to me.

I believe that your function:
function Player::getTileGridPosition(%x, %y)
should be
function Player::getTileGridPosition(%this, %x, %y)]
Due to being called here
%tilePosition=%this.getTileGridPosition(%a,%b);
is passing %this as the first argument of the function, %a and %b are second and third parameters.

Try this
function Player::getTileGridPosition(%this, %x, %y)
{ 
   echo("Player::getTileGridPosition("@ %this @", "@ %x @", "@ %y @")");
}
#2
05/21/2011 (1:01 pm)
Thank you Mr. Hall, you are exactly correct. I'm still learning torque2D and I didn't realize %this would always be passed.