IS TorqueScript Object Oriented

Scripting questions, discussions, etc
5 posts Page 1 of 1
bsisko
Posts: 76
Joined: Mon Jun 19, 2017 7:28 pm
  by bsisko » Tue Mar 24, 2020 2:06 am
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!
fLUnKnhaXYU
Posts: 129
Joined: Wed Nov 15, 2017 8:19 am
by fLUnKnhaXYU » Tue Mar 24, 2020 11:14 pm
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
ohmtal
Posts: 2
Joined: Tue Mar 17, 2020 7:30 pm
by ohmtal » Thu Mar 26, 2020 10:35 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!
bsisko
Posts: 76
Joined: Mon Jun 19, 2017 7:28 pm
by bsisko » Fri Mar 27, 2020 11:36 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.
Azaezel
Posts: 490
Joined: Tue Feb 03, 2015 9:50 pm
 
by Azaezel » Sat Mar 28, 2020 3:28 am
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;
5 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests