### IS TorqueScript Object Oriented

Scripting questions, discussions, etc

#### IS TorqueScript Object Oriented

bsisko
Posts: 83
Joined: Mon Jun 19, 2017 7:28 pm
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!

#### Re: IS TorqueScript Object Oriented

fLUnKnhaXYU
Posts: 164
Joined: Wed Nov 15, 2017 8:19 am
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)
{

<datablock = DatablockIdentifier;>

[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

#### Re: IS TorqueScript Object Oriented

ohmtal
Posts: 2
Joined: Tue Mar 17, 2020 7:30 pm
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!

#### Re: IS TorqueScript Object Oriented

bsisko
Posts: 83
Joined: Mon Jun 19, 2017 7:28 pm
Thanks, Mr. _xyu. (I hope you don't mind me shortening your name. I do not mean to be rude but its easier for me to spell.)
Thanks, Mr. ohmtal.
BTW, if I mis-gender the two of you, please let me know, and I will correct myself.

#### Re: IS TorqueScript Object Oriented

Azaezel
DEVGRU
Posts: 496
Joined: Tue Feb 03, 2015 9:50 pm

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;

#### Who is online

Users browsing this forum: No registered users and 1 guest