How to define an abstract, ex.TargetRotation
by rennie moffat · in Torque Game Builder · 10/15/2009 (11:12 pm) · 6 replies
I am having some problem in understanding how the programmer got this code to work. Let me be clear (excuse me Obama), my only quarrel is with the last line, %this.TargetRotation = - 15.
Target rotation is not a base code command like setLinearVelocity etc, it is simply an abstract idea. There is no reference to this in the the behavior templates, the only reference is to a %this.owner.rotateTo(%this.targetRotation, 60), but this is in the last line of code for the behavior and its moveDown command, nothing to do with %this.left.
Can someone break it down how this bit actually works?
You can find the full code here.
http://tdn.garagegames.com/wiki/TGB/Tutorials/Alien_Invasion#Making_the_behavior_for_the_UFO
Target rotation is not a base code command like setLinearVelocity etc, it is simply an abstract idea. There is no reference to this in the the behavior templates, the only reference is to a %this.owner.rotateTo(%this.targetRotation, 60), but this is in the last line of code for the behavior and its moveDown command, nothing to do with %this.left.
Can someone break it down how this bit actually works?
if(%this.left == 1)
{
%this.owner.setImpulseForce(%this.HorizontalSpeed * -1,0);
%this.TargetRotation = -15;You can find the full code here.
http://tdn.garagegames.com/wiki/TGB/Tutorials/Alien_Invasion#Making_the_behavior_for_the_UFO
About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
10/16/2009 (11:56 am)
Yes I understand that, I think. Many variables exist that are created by the programmer like say I want "fly" as a command. So %this.owner.fly, in which velocityX&Y are determined. My problem with this code and maybe I am missing something, I would have expected TargetRotation to be = to something since it is not a torqueScript function (base command).
#3
10/16/2009 (11:57 am)
ps, when I say equal to something I mean say = rotateTo for instacne which does have rotation and speed of rotation as built in parts.
#4
Conclusion, TargetRotation will be 15, -15, or zero. %this.owner.rotateTo(%this.TargetRotation, 60) will always be called. {} contains the code that is either for the function or condition. Read the code, if it does not make sense, read it again, if it still does not makes sense, read it again but slower. And yes a variable does need to be set to something otherwise it would be initalized to null.
10/16/2009 (12:13 pm)
If you look through the code you will notice that the onUpdate function is a bunch of if's. The author is assuming a direction is pressed and if so the %this.TargetRotation is set. You will also notice that the last line of code is not contained within and if statements. So what it means, each and every time the onUpdate function is called %this.owner.rotateTo(%this.TargetRotation, 60) will always be executed. The {} state which lines of code are contained within the function or conditional statement. Also up and down do not have the TargetRotation set because only one direction will be called at any given time. So if you were pushing down, left and right would not be pressed there for the TargetRotation would be set to zero. Like wise if both left and right are pressed the TargetRotation would be set to zero. Regardless the TargetRotation is always set within the onUpdate function and %this.owner.rotateTo(%this.TargetRotation, 60) will always be executed.Conclusion, TargetRotation will be 15, -15, or zero. %this.owner.rotateTo(%this.TargetRotation, 60) will always be called. {} contains the code that is either for the function or condition. Read the code, if it does not make sense, read it again, if it still does not makes sense, read it again but slower. And yes a variable does need to be set to something otherwise it would be initalized to null.
#5
No, in order to progress, you're going to need to understand the difference between a variable and a function and the above statement is evidence that you don't.
Think of a class as a pickle jar and variables as characteristics of that jar. Functions would be the actions you do on that jar.
10/16/2009 (12:19 pm)
Quote:
Many variables exist that are created by the programmer like say I want "fly" as a command. So %this.owner.fly, in which velocityX&Y are determined.
No, in order to progress, you're going to need to understand the difference between a variable and a function and the above statement is evidence that you don't.
Think of a class as a pickle jar and variables as characteristics of that jar. Functions would be the actions you do on that jar.
function pickleJar::onLevelLoaded(%this)
{
//Set the initial number of pickles
%this.numberOfPickles= 10;
}
//a function is an action performed on the pickle jar
function pickleJar::addPickle(%this)
{
//increase the number of pickles (variable) by 1
%this.numberOfPickles = %this.numberOfPickles + 1;
}
#6
I had noticed that but in my newbiness thought since it was at the bottom of the function it would not call something above it, major error I suppose.
Thanks.
@Patrick
Thanks,
I will study up. And eat the pickles.
10/16/2009 (12:38 pm)
@PubilyQuote:
You will also notice that the last line of code is not contained within and if statements. So what it means, each and every time the onUpdate function is called %this.owner.rotateTo(%this.TargetRotation, 60) will always be executed.
I had noticed that but in my newbiness thought since it was at the bottom of the function it would not call something above it, major error I suppose.
Thanks.
@Patrick
Thanks,
I will study up. And eat the pickles.
Torque Owner RollerJesus
Dream. Build. Repeat.
It is not a torquescript function (what you should be saying when you say "base command").
It is simply a variable that can hold information, then be called by its name to access that information. Having %this in front of it means that it belongs to the namespace and will be accessible from any function that are also defined with the same namespace as %this.
There's no documentation on it because it was created by a user in their own program. How could there be? The person who created that behavior could have named it %this.myVariable or %this.myRotationClassVariable or %this.A
As for what it does, I imagine that it is used as a target for the rotation code within that behavior but I can't go through that code for you. Sorry. You should put an echo statement right after that line:
echo("TargetRotation: " @ %this.TargetRotation);then watch your console to see what is happening with that variable when that code is executed.