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?
#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.
Associate Michael Hall
Distracted...
1368 looks/seems like an object id to me.
I believe that your function:should beDue to being called hereis 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 @")"); }