Game Development Community

TGB Controlling Path Objects from Script

by Dustin Sims · in Torque Game Builder · 11/29/2006 (4:51 pm) · 14 replies

I am trying to wrap my simple brain around the script methods for pathed objects.
I am trying to use the setspeed(%object, %speed) Method and don't know exactly how to use it.
Currently I have some code that creates a Global object like this:

function PathObjectClass::onLevelLoaded(%this, %scenegraph)
{
//set the player's ship name to the instance
$PathObject = %this;
}


It is mounted to a path and it will move on the path when I run the Level.
I want to change it's speed via script.
I am trying to do this.

setspeed($Pathobject, 100);

Do I need something in front of that like

??????.setspeed($Pathobject, 100);

or

$PathObject.setspeed(????,100)

If someone could explain this, I would appreciate it...

#1
11/29/2006 (5:02 pm)
I'm assuming you used the documentation to find the setSpeed method. You call methods on objects (in this case the path object itself) and pass in the %object and the %speed for that object.

So, you could do:
$PathObject.setSpeed( %this, 100);   //If you're not inside the mounted object's
                                                          //methods, replace %this with another objectID

If that was confusing, let me know and I'll give it another shot.
#2
11/29/2006 (5:10 pm)
Ok,
Ive tried this, but still no luck.

function PathObjectClass::onLevelLoaded(%this, %scenegraph)
{

$PathObject = %this;

$PathObject.setSpeed(%this, 70);

}

Can you explain what you meant by:

"//If you're not inside the mounted object's
//methods, replace %this with another objectID"

And where can I get the ObjectID. (I found ID# under Project)
Or could I name it in the Levelbuilder and use that.

I have been having trouble using the name (under scripting in Levelbuilder0
It don't seem to let me use that "name" for anything in Script.
The only way I can get anything to work is the name the Class and then
use the OnLevelloaded Method and then give my object a Global Variable..

Thanks for the help...
#3
11/29/2006 (5:20 pm)
The first parameter you pass into the setSpeed method is the ObjectID of the object that's on the path and you're trying to change the speed of. ObjectID's in Torque are just numbers, but they're assigned when the object is created, so you (almost) never want to refer to objects by their numbers. There are better ways.

For instance, in the code you just posted, %this is passed into the method - %this represents the instance of PathObjectClass that was loaded into the level.

The global $PathObject variable also holds an objectID.

Naming an object simply makes a single word resolve in code into the ObjectID of that object. So, you could name the object on your path "bob" and then change your code above to:

function PathObjectClass::onLevelLoaded(%this, %scenegraph)
{

$PathObject = %this;

$PathObject.setSpeed(bob, 70);

}

If this is holding you up, I'd recommend doing a few more tutorials (perhaps waiting for the new docs though) so that you get a better feel for functions.

Hope that helps!
#4
11/29/2006 (5:29 pm)
If you could answer a couple more questions for me.
Lets say I create 2 or 3 StaticSprites in LevelBuilder(LB) and give them all the same Class name. I'll call it ObjectClass.
And then use this code:
function ObjectClass::onLevelLoaded(%this, %scenegraph)
{
$Object1 = %this;

}

Which of my StaticSprites are being referenced by the Global $Object1
#5
11/29/2006 (5:35 pm)
Quote:Which of my StaticSprites are being referenced by the Global $Object1

The last one added would be referenced. You can see this easily by adding a line in your function saying
echo("$Object1 is now equal to: " @ %this);

Then, when the game is running, press "~" to open the console and find where it prints out that.

If you want to make a lot of similar objects and keep track of them, use a SimSet.
#6
11/29/2006 (5:42 pm)
So the reason for using a class is not to keep track of the individual objects...???
I thought that it followed a model like this..

Let's say I have a Dog Class.

I can then create an instance of that class and name it Fido.
Then another instance named Pluto.. etc..
And then track each Dog individually with each having the it's own properties..
Do I have to use a SimSet for this in Torque?
I appreciate the help.
#7
11/29/2006 (6:02 pm)
What you said would be fine. Naming each object is an ok solution - but when you want to dynamically create them or have a lot, naming each one by hand would be annoying. In that case, you could put each Dog in a Simset (kinda like putting a lot of puppies in a bag) and when you wanted to check if any of them are hungry, you'd just go through the bag and check which ones are emaciated ;) The class is used to give them all some properties (like being hungry and being able to eat things), but it would be unweildy to name them all if there were 1000 of them.

Hope that made some sense ;)
#8
11/29/2006 (6:13 pm)
Perfect Sense...
Thanks for all the help.
I still haven't been able to change the speed but I think I can figure it out now.
It maybe some of my other code screwing it up.
#9
11/29/2006 (8:25 pm)
I'm Back.

I have finally realized how to make it work

When you call any of the t2dPath Methods (setspeed, getspeed, etc..)
You have to call it like this Pathobject.method(Mountedobject, parameters...)
I was trying to setspeed on the Mountedobject.
This code worked:
$PathObject.setspeed($MountedObjectName,70)
Tom: You told me this in your first post but it just so happened that my MOUNTED object was called
$Pathobject so I was trying to use it to call the setspeed method.
Anyway, it works now.. Thanks
#10
11/29/2006 (8:38 pm)
One Problem solved.
I now have a new problem.

I can now set the speed of an object mounted to a path from script.

When the game is started the object starts moving around the path.
The problem is that when I change the speed on the fly using script the object
jumps back to it's starting position and then changes to the new speed.
I assumed that it would just increase speed and keep on going on the path.
Is there anything I can do about this. If not, the Paths are pretty much useless for
what I was wanting to use them for.
#11
11/29/2006 (8:50 pm)
I remember someone having that problem recently. You might try and see what happened with them.

I have yet to use paths for anything, but it might be possible to say, save the current position on the path, change the speed, and move the object to the saved position on the path.

Edit: nevermind - looks like that workaround can't be done. I wonder if the resetting behavior is a bug. What effect are you trying to create?

Another edit: Yeah, looks like either something silly in the code or a bug - looks like it would be easy to fix in source (if you have the Pro version and know how to compile the engine).
#12
12/01/2006 (4:43 pm)
Just checking back in.
Thanks for the Last Post Tom.

All I was doing was checking out what could possibly be done with it. It seemed like a simple thing for it do do (Change speed, position, direction while on a path), but I guess it still has some quirks..

Does anyone know if this was fixed in the 1.1.3 Release? It did say that t2DPath was updated with some new methods.
Also, is 1.1.3 available for the Demo Download yet or is it just for owners....
#13
12/01/2006 (5:00 pm)
I believe all the links point to 1.1.3 - and the problem still looks like it's there in 1.1.3

It would be a really easy fix if you had the Pro version though.
#14
09/01/2010 (9:49 am)
Asalm O alkum, I want to make an object that is moving in jump condition.. I have this function moveMap.bindCmd(keyboard, "space", "playerJump();", ""), can you give me any totorial about this that how can I use this function so that my object is moving in jump condition continuosly...


If you have any answer regarding this then snd it to my mail id...

rejected_boy@live.com