Simple Torque script question...
by Jason Cahill · in Torque Game Builder · 02/26/2005 (8:48 am) · 13 replies
Hi all,
Sorry if this is a stupid / obvious question. I'm very familiar with C++ syntax, but there's a few things about Torque script (which is pitched as typeless C++) that don't make sense. What does the following mean:
Technically, I can read it and it says "create a new global variable named player of type fxStaticSprite2D", but what is that next part? I can read this in one of two ways:
1. you are inlining the constructor of $player somehow?
2. you put a braced subsection of code mid function (this is the only legal C++ read, I assume), but then the semi-colons are off.
I understand this:
Sorry if this is a stupid / obvious question. I'm very familiar with C++ syntax, but there's a few things about Torque script (which is pitched as typeless C++) that don't make sense. What does the following mean:
$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };Technically, I can read it and it says "create a new global variable named player of type fxStaticSprite2D", but what is that next part? I can read this in one of two ways:
1. you are inlining the constructor of $player somehow?
2. you put a braced subsection of code mid function (this is the only legal C++ read, I assume), but then the semi-colons are off.
I understand this:
$player = new fxStaticSprite2D();
{ // braces denote a new code scope but are effectively irrelevant?
scenegraph = t2dSceneGraph;
}Anyway, if anyone can explain this, thanks so much!About the author
#2
02/26/2005 (9:00 am)
Got it. Thanks for the quick reply!
#3
I prefer the one-line method because it applies to all objects going into that scene. T2D allows you to assign stuff like this in many ways. For instance, these are three ways you can create such an object and get it into a scene:-
I tried to cover most bases with stuff like this. Didn't want to force people to think like I do. Use whatever way you see best. :)
- Melv.
02/26/2005 (9:44 am)
@Jason: Pretty much it. The assignments in the braces are to fields of the object. All T2D objects have only two fields, "scenegraph" and "config". The "scenegraph" field just provides a nice shortcut to assign the object to a specific scene and the "config" field provides a nice way to assign a config-datablock for the object, in this case, it'd be a "fxStaticSpriteDatablock2D".I prefer the one-line method because it applies to all objects going into that scene. T2D allows you to assign stuff like this in many ways. For instance, these are three ways you can create such an object and get it into a scene:-
// One liner.
$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };or// Object-centric. $player = new fxStaticSprite2D(); $player.addToScene( t2dSceneGraph );or
// Scene-centric. $player = new fxStaticSprite2D(); t2dSceneGraph.addToScene( $player );
I tried to cover most bases with stuff like this. Didn't want to force people to think like I do. Use whatever way you see best. :)
- Melv.
#4
02/26/2005 (10:50 am)
You might also want to look at Scripting docs which is "preview" documentation. I'm not sure how to access the "real" documentation. Anybody?
#5
I would also like to ask another stupid but related question. I'm a little rusty on my C++ but why couldn't you just pass the scenegraph to the constructor?
It seems like very odd language design to prefer:
$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
over
$player = new fxStaticSprite2D( t2dSceneGraph);
But perhaps I'm forgetting a limitation of the language or something...
02/28/2005 (9:08 am)
Firstly, I'd like to echo Jason's question regarding accessing the "real" documentation on scripting.I would also like to ask another stupid but related question. I'm a little rusty on my C++ but why couldn't you just pass the scenegraph to the constructor?
It seems like very odd language design to prefer:
$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
over
$player = new fxStaticSprite2D( t2dSceneGraph);
But perhaps I'm forgetting a limitation of the language or something...
#6
02/28/2005 (9:14 am)
It's because the number of parameters to each constructor can vary. Their scripting concept is effectively a nice way of taking "optional parameters." In the braces you can set whatever fields you like, but only the set you want. Some objects have like 30 possible fields, yet you may only want to set 3 of them.
#7
That will name your new sprite as mySprite, which would allow you to refer to it by name like the following.
02/28/2005 (9:23 am)
$player = new fxStaticSprite2D( mySprite);
That will name your new sprite as mySprite, which would allow you to refer to it by name like the following.
mySprite.setGroup (1);
#8
02/28/2005 (11:43 am)
Anyone can access the TGE scripting docs as well as all these other nice docs.
#10
02/28/2005 (2:45 pm)
Drew can you see these docs? And this and this? Let me know if not, you should be able to.
#11
03/01/2005 (1:32 pm)
Are there any plans to let owners of just Torque 2D see more sections of the official documentation, for example the scripting appendix?
#12
03/01/2005 (5:49 pm)
@josh - yea I can see those. I see they provide the same info I would have found in the off-limits section? Thanks!
#13
03/01/2005 (6:38 pm)
Aldo, yes there are. We will be rolling out some brand new documentation features soon. We'll keep you posted.
Torque 3D Owner Stephen Zepp
If you can think of it in your mind as kind of a larger set of parameters, that are only used to provide data (datablock) values to the new operator, it should help to differentiate it's use for you. In other words, your "1" above is pretty correct.