Setting parameters of global objects in onMouseMove?
by Bruno Campolo · in Torque Game Builder · 06/23/2007 (6:58 am) · 3 replies
Hi, I am new to TGB but not to game development and had a simple (hopefully) scripting question (I am using 1.5 beta):
I have a global variable $player which points to my player object.
I am trying to set a member variable of $player that doesn't yet exist like so:
sceneWindow2D::onMouseMove(normal parameters here)
{
/*
I get the mouse position and the old position and calculate mouseYDelta here
*/
$player.gunAngle += %mouseYDelta; // mouseYDelta has been echo'ed out and is correct
echo("Gun Angle" @ $player.gunAngle); // This echo's out with the value of mouseYDelta
}
***What I am seeing is that the value of $player.gunAngle is no longer set when this function returns as if it were a local variable. So here are a few questions:
1) When an object is global such as $player and I set a member variable that doesn't currently exist like gunAngle, does gunAngle act as a permanent (or global) variable that is part of $player or is it considered active only in the local scope?
2) Is += valid in TorqueScript? I thought I saw it somewhere in the docs, but I can't find it now
3) If I put the same code in the onUpdate function of the behavior that is attached to the player it works fine, but I read that you shouldn't put any unnecesary code in onUpdate because it gets called every tick and would impact performance. What is different about the way I did it above that makes it work/stop working?
4) Is there a better way to do what I am doing or a better place to put the code?
Thanks,
Bruno Campolo
Bantam City Games
I have a global variable $player which points to my player object.
I am trying to set a member variable of $player that doesn't yet exist like so:
sceneWindow2D::onMouseMove(normal parameters here)
{
/*
I get the mouse position and the old position and calculate mouseYDelta here
*/
$player.gunAngle += %mouseYDelta; // mouseYDelta has been echo'ed out and is correct
echo("Gun Angle" @ $player.gunAngle); // This echo's out with the value of mouseYDelta
}
***What I am seeing is that the value of $player.gunAngle is no longer set when this function returns as if it were a local variable. So here are a few questions:
1) When an object is global such as $player and I set a member variable that doesn't currently exist like gunAngle, does gunAngle act as a permanent (or global) variable that is part of $player or is it considered active only in the local scope?
2) Is += valid in TorqueScript? I thought I saw it somewhere in the docs, but I can't find it now
3) If I put the same code in the onUpdate function of the behavior that is attached to the player it works fine, but I read that you shouldn't put any unnecesary code in onUpdate because it gets called every tick and would impact performance. What is different about the way I did it above that makes it work/stop working?
4) Is there a better way to do what I am doing or a better place to put the code?
Thanks,
Bruno Campolo
Bantam City Games
About the author
Creator of Bantam City Games, a one-man independent game development studio. To learn more, check out 'A Game Developer's Saga', a game development blog at: http://www.bantamcity.com/blog
#2
I am setting up the $player var in the following behavior and then that behavior is assigned to the player sprite in the TGB IDE.
function PlatformerPlayerBehavior::onBehaviorAdd(%this)
{
// Set the $player variable
$playerBehavior = %this;
$player = %this.owner;
// Enable the update callback.
$player.enableUpdateCallback();
// Set some collision code
$player.setCollisionMaxIterations(2);
// Enable mouse events for this object
$player.setUseMouseEvents(true);
}
06/24/2007 (11:26 am)
Hi Dan, thanks for the reply.I am setting up the $player var in the following behavior and then that behavior is assigned to the player sprite in the TGB IDE.
function PlatformerPlayerBehavior::onBehaviorAdd(%this)
{
// Set the $player variable
$playerBehavior = %this;
$player = %this.owner;
// Enable the update callback.
$player.enableUpdateCallback();
// Set some collision code
$player.setCollisionMaxIterations(2);
// Enable mouse events for this object
$player.setUseMouseEvents(true);
}
#3
Sorry for wasting your time Dan, but thanks for the better understanding of member vars.
Thanks,
Bruno
06/24/2007 (11:37 am)
I just tried the same code again and now it works! There must have been an issue with the IDE not refreshing something when I was trying it the other day. Sorry for wasting your time Dan, but thanks for the better understanding of member vars.
Thanks,
Bruno
Torque Owner Dan Maruschak
There's no "special scoping" for dynamic fields of an object. If the object exists, and you set a field in it, then the field is set. Can you post the code for how you create the object in question, and put the object reference in the $player global variable?
+= is valid in TorqueScript.