Game Development Community

Using onUpdate to apply movement

by rennie moffat · in Torque Game Builder · 10/27/2009 (9:27 pm) · 9 replies

Hi there I have essentially coped the shootsBehavior.cs, but typed it in and made it my own, understanding each line as I go. The script contains no errors ( i promise) but does not work. What I am wondering is perhaps is my use of onUpdate flawed. I am using it like this, please if you can contribute to my understanding here, please post.

Thank You


function SpacerTronicsControls::onUpdate(%this)
{	
	%this.updateMovement();
}

function SpacerTronicsControls::updateMovement(%this)
{
	 %this.owner.setLinearVelocity((%this.forward * %this.Gas) + (%this.backward * %this.BreakReverse));
     %this.owner.setRotation((%this.right * %this.RotationSpeedRight) + (%this.left * %this.RotationSpeedLeft));
}


I am calling the movements here
function SpacerTronicsControls::rotateRight(%this, %val)
{
	%this.right = %val;
	%this.updateMovement();
}

I have set everything else up pretty standard with my bind cmds also being sure to unbind them.

moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "rotateRight", %this);

%this.right = 0;

and I believe my behavior fields are defined properly
%template.addBehaviorField(leftKey, "Turn Spacer Left", keybind, "keyboard left");
   %template.addBehaviorField(rightKey, "Turn Spacer Right", keybind, "keyboard right");
   %template.addBehaviorField(forwardKey, "Apply Positive force vector", keybind, "keyboard up");
   %template.addBehaviorField(backwardKey, "Apply Negative force vector", keybind, "keyboard down");
   
   %template.addBehaviorField(Gas, "Vector controling posVel", float, -25);
   %template.addBehaviorField(BreakReverse, "Vector controling NegVel", float, 25);
   %template.addBehaviorField(RotationSpeedLeft, "Rotation/Turn Speed Left", float, -25);
   %template.addBehaviorField(RotationSpeedRight, "Rotation/Turn Speed Right", float, 25);
But it does not work, just glancing at these chunks does anyone have any insight as to what I might be doing wrong?


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.


#1
10/28/2009 (4:41 am)
In what way was it not working? Was your player not rotating? Was it not moving?

Did you put echo-statements inside your functions to make sure they were being called?
#2
10/28/2009 (8:24 am)
@Rennie: I would suggest that to help you move forward in your learning quicker, would be to try to learn/ask about tips on how to DEBUG programs. As a beginner, it's one of those skills that is as important as the understanding the code you're writing. :)

Most experienced developers tend to take those skills for granted but they're not immediately obvious when starting. It's such an important and sometimes complex process that there are even books on debugging!

The best way to start this process of understanding is to be able to quickly check your assumptions on values of variables, what's being executed etc. If it doesn't work as expected, it's hard to get it working by just changing the code and hoping for the best.

In Torque, your best friend should be the "echo()" command. You can use:
echo( "Some output" );
echo( %this.right );

If you place lines like this in places where you expect certain values or simply that you expect a bit of code to be executed then you can quickly verify if something works as you expect.

I think you'll find that a majority on people who want to help you on the forums will assume you've done that before they want to jump in and help.

If you have no idea on what to expect or what the code does then you're completely lost. In that case I would suggest not copying lots of code you don't understand but start simpler on the basics of the language e.g. how to do loops, how to output variables to the console etc. Perhaps even a really basic test-bed.

I do say the above with complete respect that you're trying to learn this stuff but I sense from your posts that you could do with some debugging help more than anything else.

Quite happy to help you there as I am sure others are too.

Hope this suggestion helps in some way.

- Melv.

#3
10/28/2009 (11:16 am)
@ William,
It just did not respond to any input. I did not put in echo calls yet, I must figure out how, tho @ Melv, ok, I see you did an example, thanks.




so just guessing here as I am leaving for a few days and wont have much time to work on torque till monday, just a rough cut of What I think I should do to use echo, please correct me if I am wrong.


function %this.moveRight()
{
%this.right = %val;
%this.updateMovement();
/// I am not actually sure how I would apply it (echo to this), please he;p if you can. Thanks.


@Melv, yes thank you, debugging was something that I was completely unaware of. As I say if you or someone can give me a good example of it used in practice I am sure would help a lot.

My understanding of the code is coming along. My approach to seeing the patterns used, how one thing relates to another and where it should go etc is paying off as I seem to be making nice jumps each day tho, still on the shore really, no luxury cruises yet. Tho you are right, I have no idea how to perform the basics like you say (i did not know these were basics) loops, output variables to the console. So I need to learn up. Cant build a tower on shakey foundations!



Thanks guys and again I really do appreciate all of your help, it will certainly help me to achieve my goals and I am very thankful for that.

Thanks again,
Ren


#4
10/28/2009 (12:05 pm)
Your fragment of code:
function %this.moveRight()  
{  
%this.right = %val;  
%this.updateMovement();

I might be wrong but I'd do it that way :

function %this.moveRight()  
{
echo("Starting function moveRight");
echo("%val is equal to " @ %val);
%this.right = %val;
%this.updateMovement();
// next part of your function
echo("End of function moveRight");
}

and for updateMovement function :

function SpacerTronicsControls::updateMovement(%this)  
{
  echo("Starting function updateMovement");
  echo("forward is equal to: " @ %this.forward);
  echo("gas is equal to: " @ %this.Gas);
  echo("backward is equal to: " @ %this.backward);
  echo("BreakReverse is equal to: " @ %this.BreakReverse);
     %this.owner.setLinearVelocity((%this.forward * %this.Gas) + (%this.backward * %this.BreakReverse));  
  echo("LinearVelocity set");
  echo("right is equal to: " @ %this.right);
  echo("RotationSpeedRight is equal to: " @ %this.RotationSpeedRight);
  echo("left is equal to: " @ %this.left);
  echo("RotationSpeedLeft is equal to: " @ %this.RotationSpeedLeft);
     %this.owner.setRotation((%this.right * %this.RotationSpeedRight) + (%this.left * %this.RotationSpeedLeft));  
  echo("Rotation set");
  echo("End of function updateMovement set");
}

Fire your code and then check console.log to see what is going wrong.

Of course, I have put a massive amount of echo commands. You can do with a lot less in normal conditions but I don't know what you're trying to achieve, the rest of your code and what is the problem except that it doesn't work.

If someone can explain better than I did or catch an error I made, don't hesitate to speak: I'm rushing this post a little bit because of work, so it might not be 100% accurate...
#5
10/28/2009 (12:33 pm)
thanks Seb.
I am currently going thru this code I installed but am getting errors so I can nit see the response. My question is, when working, what should I expect to see, and how do I test it?



So in other words, if it is without syntax errors, and I test my code as normal in the compiler, what should I see, or how can I "test" the echo's?
#6
10/28/2009 (12:43 pm)
Quote:
Fire your code and then check console.log to see what is going wrong.

Not sure if you should see anything during the runtime. Probably not. As I said, I'm a newbie myself and haven't gone far enough to need to debug anything (tutorials usually work ;).
But as I said, check your console.log file in your project folder. Everything made by the echo command is there.

If you're not sure what is the echo command for, check tdn.garagegames.com/wiki/TorqueScript#The_Console_and_Simple_Scripts.
#7
10/28/2009 (2:53 pm)
Yes I understand that, thats what i do when ever I want to see the amount of code, right or wrong. Check it or what have you, what I am/will be when i get back is trying to see how the echo will benefit in checking. I am sure it does, it must, the pros do it, so why wouldn't I? I just want to better understand the result, what I will see once I begin to use it and how it will make life easier.
#8
10/29/2009 (5:28 am)
With above code, you'd see in console.log file something like (echo output only):

Quote:
Starting function moveRight
%val is equal to 5
Starting function updateMovement
forward is equal to: 1
gas is equal to: 10
backward is equal to: 0
BreakReverse is equal to: 0
LinearVelocity set
right is equal to: 32
RotationSpeedRight is equal to: 23
left is equal to: 0
RotationSpeedLeft is equal to: 0
Rotation set
End of function updateMovement set
End of function moveRight

Note that values are just examples.

Of course this is assuming that both function will run without errors.
If, for some reason you see something like that (echo output only):

Quote:
Starting function moveRight
%val is equal to 5
Starting function updateMovement
forward is equal to: 1
gas is equal to: 10
backward is equal to: 0
BreakReverse is equal to: 0

you would know that there is a problem in updateMovement at the moment the linear velocity should be set.
Why? Because after you set the linear velocity, there is the line:

echo("LinearVelocity set");

which should display

Quote:
LinearVelocity set

in your console.log file. As it is not displayed, then the code stopped before that precise echo command.
I've put echo commands to display variable values so that you know what value are assigned to each variable used, which could help to know where is the problem with the
%this.owner.setLinearVelocity((%this.forward * %this.Gas) + (%this.backward * %this.BreakReverse));
code (in my example; I don't know if there is an error or not).

Do you see what I mean?
Try it by yourself and I'm sure you will.
#9
10/29/2009 (5:50 am)
Seb offers some great advice.

Another example to hammer it home; say I wanted to know if the following (totally made-up) function was being called (note that "%player" and the "%player.kill()" are made-up, it's only an example and isn't that relevant to the point I'm trying to get across:
function KillThePlayer( %player )
{
   %player.kill();  
}

How would I check that this function is being called? Well I could run the program under Torsion and set a breakpoint but putting that obvious sales-pitch aside, a really quick way would be to modify the function to output something to the torque in-game drop-down console so I could see it while the game was running or look at it afterwards in the "console.log" file.

To do this, one way is to add an "echo" statement like so:
function KillThePlayer( %player )
{
   echo( "KillThePlayer has been called!" );
   %player.kill();  
}
Now if I look at the console I should see the line "KillThePlayer has been called!". If I don't then it wasn't called. Quite often the console is full of other output (sometimes called console spam) so it can be hard to find your line. There's a certain art in formatting the echo statements so that they are obvious and different folks use different things to make it stand out like:
function KillThePlayer( %player )
{
   echo( "KillThePlayer has been called ****************************" );
   %player.kill();  
}

Now putting in a fixed string is only so useful, the next stage to it is to not only output something but output some values that will help you check your assumptions.

Let's create a scenario: say I have tried the above and I can see the function being called but my player isn't being deleted. After knowing the function has been called I could go to the forums and ask for help but most likely people won't be able to help without further info. The best thing is to dig a little deeper. The most obvious next step is to check the "%player" object that is being passed into the function.

I need to check to see if the parameter to the function (%player) is correct. The best way to do something like that is to do it in stages. The first step would be to check that the "%player" parameter is an object, then check to see that the object is correct.

In TorqueScript I could check to see if it's a real object using the "isObject(%player)". After I've verified that I (say) want to check that the name is "rennie" and let's say we've got a "%player.getName()" to check that.

Here's the debugging code:
function KillThePlayer( %player )
{
   // Check this is a valid object!
   if ( !isObject( %player ) )
   {
      echo( "KillThePlayer called but player is invalid!" );
      return;
   }

   // Check the "player" object is correct.
   if ( %player.getName() !$= "rennie" )
   {
      echo( "KillThePlayer called but player is not rennie!" );
      return;
   }

   echo( "Killing player" SPC %player.getName() );

   %player.kill();  

   echo( "Successfully killed the player!" );
}

Note the line:
echo( "Killing player" SPC %player.getName() );
You can compose a string and add together other strings or add in results of other functions, parameters etc. This is where "echo()" is most useful. You can output values that you either expect or just want to check on. Note the use of "SPC" which adds the stuff on the left to the stuff on the right with a space inbetween. If you want to do that without a space use the "@" symbol.

Hope this helps get you started on the first steps of debugging. Just be careful with the additional debugging code as it can get out of hand and if you put too much in while trying to isolate a problem, it can cause problems so always remember to remove it afterwards. Practice most certainly makes perfect here.

Production ready code should have this kind of debugging code in there permanently however it shouldn't output anything unless there's an exceptional (bad) condition. This is also know as assertions e.g. you're asserting that something is correct and outputing something (or failing) if that assertion is invalid. This is also known as "defensive" programming where you have code in place to constantly check the function parameters, conditions and program flow as your program evolves. You don't want to be doing this for every function , just the "important" ones. Unfortunately it's up to you to decide what the important ones are so it's obviously subjective. Experience will give you that knowledge.

This is the final point to be made: your function might work now but as you change your program you might cause a condition to arise where the function fails again. Defensive programming similar to the code highlighted above will let you know immediately that something has gone wrong. This is part of the art of programming and you'll get better and better at it as you practice.

Good luck, I've been doing it for over 20 years and I still learn stuff almost every day.

Hope this helps,

- Melv.