Extremely confused! Can anyone help me understand this?
by Dan Prati · in Torque Game Builder · 09/18/2006 (3:35 am) · 7 replies
Hi everyone, if anyone can help me to understand how this is supposed to work, I'd be very grateful!
I've been tinkering with TGB (trial version) for a few days now, and while most of it makes sense, there's a few things that baffle me completely, and I have been unable to find answers in the docs or the public forum.
I've created a new level using the "TGB" sample project. I've added a spaceship to the level,linked it to a class script (player.cs - the script is copied from the shooter tutorial) and have it moving around with the keyboard. This all works fine. I then added a GG logo to the level, named it ballA, and set up the player.cs script to handle spacebar presses and releases so that pressing the spacebar will move ballA to the players coordinates. This is where things start to confuse me...
Firstly, to be sure I understand: when you place an image in the level builder, am I correct in assuming that it is automatically created as an object of class t2dSceneObject? And thus if I assign it a name (eg: 'ballA') in the editor, but not a class, I should still be able to reference it by script such as: $ballA.setPosition(20,15); ?
I ask because it doesn't seem to work, and all the tutorials create new classes for everything which seems a bit excessive if you only need to be able move an object to a new position and nothing else. (I say excessive because the functionality is already provided for by t2dSceneObject, and so making a new class seems redundant)
To continue: In an attempt to follow the methodology demonstrated in the tutorials, I instead created a new script 'ball.cs', exec'ed it from the game.cs, and then called it from player.cs - this now works: when the space bar is pressed, ballA moves to the spaceship's location... But then I added a second ball, "ballB" - the idea is that when the spacebar is pressed, ballA moves to the player's location, then when the spacebar is released, ballB moves to the players location - and I cannot get this to work at all!
How do you refence 2 different objects of the same class by their individual names? At the top of ball.cs I had the line: "$ballA = %this;" which worked fine when it was just one ball on its own, but now that I also have ballB, how do I work with them both? Adding "$ballB = %this;" just creates two references to the same object (which moves on both occasions, with the other ball static), whereas removing both lines yields no responses from either ball.
I'm sure I'm missing something blindingly obvious, but I've been facing this for a couple of days now and I just don't get it, and seeing as this is a fundamental aspect of the entire system, I'm kinda stuck... The tutes don't do anything like this, and the reference docs are great for syntax, they lack explanations or code samples so they're no help either!
Help!!!
- Dan
I've been tinkering with TGB (trial version) for a few days now, and while most of it makes sense, there's a few things that baffle me completely, and I have been unable to find answers in the docs or the public forum.
I've created a new level using the "TGB" sample project. I've added a spaceship to the level,linked it to a class script (player.cs - the script is copied from the shooter tutorial) and have it moving around with the keyboard. This all works fine. I then added a GG logo to the level, named it ballA, and set up the player.cs script to handle spacebar presses and releases so that pressing the spacebar will move ballA to the players coordinates. This is where things start to confuse me...
Firstly, to be sure I understand: when you place an image in the level builder, am I correct in assuming that it is automatically created as an object of class t2dSceneObject? And thus if I assign it a name (eg: 'ballA') in the editor, but not a class, I should still be able to reference it by script such as: $ballA.setPosition(20,15); ?
I ask because it doesn't seem to work, and all the tutorials create new classes for everything which seems a bit excessive if you only need to be able move an object to a new position and nothing else. (I say excessive because the functionality is already provided for by t2dSceneObject, and so making a new class seems redundant)
To continue: In an attempt to follow the methodology demonstrated in the tutorials, I instead created a new script 'ball.cs', exec'ed it from the game.cs, and then called it from player.cs - this now works: when the space bar is pressed, ballA moves to the spaceship's location... But then I added a second ball, "ballB" - the idea is that when the spacebar is pressed, ballA moves to the player's location, then when the spacebar is released, ballB moves to the players location - and I cannot get this to work at all!
How do you refence 2 different objects of the same class by their individual names? At the top of ball.cs I had the line: "$ballA = %this;" which worked fine when it was just one ball on its own, but now that I also have ballB, how do I work with them both? Adding "$ballB = %this;" just creates two references to the same object (which moves on both occasions, with the other ball static), whereas removing both lines yields no responses from either ball.
I'm sure I'm missing something blindingly obvious, but I've been facing this for a couple of days now and I just don't get it, and seeing as this is a fundamental aspect of the entire system, I'm kinda stuck... The tutes don't do anything like this, and the reference docs are great for syntax, they lack explanations or code samples so they're no help either!
Help!!!
- Dan
#2
I knew it had to be something really simple - thank you very much!
I hadn't seen anything in the tutes without either the local or global assignment, so I figured one or the other was required.
Thanks again!
09/18/2006 (4:44 am)
Oh... My... God....I knew it had to be something really simple - thank you very much!
I hadn't seen anything in the tutes without either the local or global assignment, so I figured one or the other was required.
Thanks again!
#3
There are a couple of ways to reference an object in TorqueScript, but they all come down to one thing: the object's (randomly generated per run) ObjectID.
When you create an object via TorqueScript, you can immediately "catch" the ObjectId of the newly created object in a local (%) or global ($) variable, and then use it in the future (as long as it's in scope).
TGB's TorqueScript also allows you to access any object by it's name (not as a variable, so no $ or % needed), if you named the object in the level builder. TorqueScript then turns that name into the correct ObjectID, and does what it needs to do.
Since there is no way to store the ObjectId of an object created by the level builder (directly anyway), to script objects made in the level builder, you should give them a name and use it directly as Philip demonstrates.
09/18/2006 (8:03 pm)
For clearness sake:There are a couple of ways to reference an object in TorqueScript, but they all come down to one thing: the object's (randomly generated per run) ObjectID.
When you create an object via TorqueScript, you can immediately "catch" the ObjectId of the newly created object in a local (%) or global ($) variable, and then use it in the future (as long as it's in scope).
TGB's TorqueScript also allows you to access any object by it's name (not as a variable, so no $ or % needed), if you named the object in the level builder. TorqueScript then turns that name into the correct ObjectID, and does what it needs to do.
Since there is no way to store the ObjectId of an object created by the level builder (directly anyway), to script objects made in the level builder, you should give them a name and use it directly as Philip demonstrates.
#4
09/18/2006 (8:07 pm)
What Stephen explained is something that bugged me a little bit about TGB (coming from T2D), in that I can't seem to find a way to access the objects, etc, created in the Level Builder from anywhere but the Level Builder. I spent a good bit of time hunting through the scripts to find some sort of reference to the objects, etc, but finally realised they were all in the level file. Are there any plans to make this a little more transparent, or is this the final model?
#5
It's not really an expected workflow technique, but there is a good callback for exactly what you're asking:
t2dSceneObject::onLevelLoaded(%this, %sceneGraph)
You can namespace this to whatever you need--for example, say you want to put all of your objects that are of the superclass Enemy (set in LevelBuilder) into a SimSet:
09/18/2006 (10:30 pm)
Disclaimer: This is from memory, and from some script I did 6 months ago--your milage may vary, but it's worth checking out!It's not really an expected workflow technique, but there is a good callback for exactly what you're asking:
t2dSceneObject::onLevelLoaded(%this, %sceneGraph)
You can namespace this to whatever you need--for example, say you want to put all of your objects that are of the superclass Enemy (set in LevelBuilder) into a SimSet:
$EnemySet = new SimSet();
function Enemy::onLevelLoaded(%this, %sceneGraph)
{
echo("Adding object (" @ %this @ ") to EnemySet!");
$EnemySet.add(%this);
}
#6
09/18/2006 (11:18 pm)
Aah, thanks Stephen, never even thought of that! :)
#7
09/18/2006 (11:33 pm)
Hehe..I didn't think of it either--I found it in one of the tutorials--either checkers, or Whack-a-Mole, can't remember which!
Torque Owner Philip Mansfield
Default Studio Name