Jump to content

IS TorqueScript Object Oriented


bsisko

Recommended Posts

Does anyone know if TorqueScript object oriented?


That is can I use standard C++ conventions in declaring objects?

Like for example


class Ball()

{

private:

int x;

int y;


public:

void bounce();

}


I realize that I would later have to setup this construction in a datablock!

Link to comment
Share on other sites

I got this little excerpt from some notes Id made . My notes titled TSoverview but I didnt see it at GG , Its probably there somewhere .

I dont think it exactly addresses your question to which Id say that I dont think that TS uses some of those commands . If you find it interesting just Google or whatever " torque script "


//The Excerpt :


Objects

Having covered the basics of the language, it's time to examine some of TorqueScript's more interesting details.


In Torque, every item in the game world is an object, and all game world objects can be accessed via script. For

example, Player, WheeledVehicle, Item, etc are all accessible via script, though they are defined in C++.


Objects are created in TorqueScript using the following syntax:


// In TorqueScript

%var = new ObjectType(Name : CopySource, arg0, ..., argn)

{




[existing_field0 = InitialValue0;]

...

[existing_fieldM = InitialValueM;]


[dynamic_field0 = InitialValue0;]

...

[dynamic_fieldN = InitialValueN;]

};


This syntax is simpler than it looks. Let's break it down:


%var- Is the variable where the object's handle will be stored.

new- Is a key word telling the engine to create an instance of the following ObjectType.

ObjectType- Is any class declared in the engine or in script that has been derived from SimObject or a subclass of

SimObject. SimObject-derived objects are what we were calling "game world objects" above.

Name (optional)- Is any expression evaluating to a string, which will be used as the object's name.

CopySource (optional)- The name of an object which is previously defined somewhere in script. Existing field values

will be copied from CopySource to the new object being created. Any dynamic fields defined in CopySource will also

be defined in the new object, and their values will be copied. Note: If CopySource is of a different ObjectType than

the object being created, only CopySource's dynamic fields will be copied.

arg0, ..., argn (optional)- Is a comma separated list of arguments to the class constructor (if it takes any).

datablock- Many objects (those derived from GameBase, or children of GameBase) require datablocks to initialize

specific attributes of the new object. Datablocks are discussed below.

existing_fieldM- In addition to initializing values with a datablock, you may also initialize existing class members

(fields) here. Note: In order to modify a member of a C++-defined class, the member must be exposed to the Console.

This concept is discussed in detail later.

dynamic_fieldN- Lastly, you may create new fields (which will exist only in Script) for your new object. These will

show up as dynamic fields in the World Editor Inspector.


*****************************


Due to the way Torque parses script files, the ObjectType must be known at compile time. In order to create types that

are only known post compile, use the eval() function. For example, this will not compile:


new %myObjectType(SomeObjectName);

Place this in an eval() function to force Torque to evaluate it properly:


eval("new %myObjectType(SomeObjectName);");


Let's create one object that doesn't use a datablock and one that does:


// create a SimObject w/o modifying any fields

$example_object = new SimObject();


// create a SimObject w/ dynamic fields

$example_object = new SimObject()

{

a_new_field = "Hello world!";

};


// create a StaticShape using a datablock

datablock StaticShapeData(MyFirstDataBlock)

{

shapeFile = "~/data/shapes/player/player.dts";

junkvar = "helloworld";

};


new StaticShape()

{

dataBlock = "MyFirstDataBlock";

position = "0.0 0.0 0.0";

rotation = "1 0 0 0";

scale = "1 1 1";

};


//END OF EXCERPT

Link to comment
Share on other sites

Kind of. To make an example:


function Ball::bounce(%this)

{

echo ("bouncing" SPC %this.x SPC %this.y);

}


$myBall = new ScriptObject(Ball);

$myBall.x = 10;

$myBall.y = 20;

$myBall.bounce();

 

Does anyone know if TorqueScript object oriented?


That is can I use standard C++ conventions in declaring objects?

Like for example


class Ball()

{

private:

int x;

int y;


public:

void bounce();

}


I realize that I would later have to setup this construction in a datablock!

Link to comment
Share on other sites

OOP, yes, polymorphic... iiiish

an object (or datablock for this talk) will itterate via first looking for the object name, then looking at that objects class, and superClass entries, then start going down the hierarchy chain c++ side.


you can link a pair of objects to the same class/superclass via either direct assignment for both, or by leveraging the copy-and-override-constructor

 

new classtype(foo)
{
entryA=1;
entryB=2;
}
 
new classtype(bar:foo)
{
entryB=3;
}

which ends up with the net result of bar.entryA ==1 and bar.entryB == 3;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...