Datablocks and Objects quesitons... plz help
by Daniel Kashtan · in Technical Issues · 02/14/2007 (7:21 am) · 12 replies
Http://tdn.garagegames.com/wiki/TorqueScript&needLogin=1#Datab
I've been reading up on TorqueScript basics and I finished some basic TGB tutorials. I feel pretty comfortable now but I am still confused about how objects work in TorqueScript and how Datablocks fit in. I have a good Java object oriented background and I know a little C. I read that the purpose behind Datablocks is for good networking code and they are a special type of object, which is great! But I am confused on some things...
1) My main question is, are datablocks full blown objects themselves with fields and methods? (I think they are called commands instead of methods in TorqueScript)
2) Can I do anything I could with an object, with a Datablock?
3) Are Datablocks instantiated from classes like objects?
4) Can multiple classes be defined in more than one file? (I am used to having individual .class files for every object in Java, with a driver file that runs them)
4)Can a plain old class be used to instantiate objects, like a ball, and get it on screen with physics and animation?
5) If I wanted a 100 balls on screen, would a datablock be better than a plain old object from a class for network preformance only, and not local preformance?
6) Any good referneces on objects and datablocks besides the link at the top?
I've been reading up on TorqueScript basics and I finished some basic TGB tutorials. I feel pretty comfortable now but I am still confused about how objects work in TorqueScript and how Datablocks fit in. I have a good Java object oriented background and I know a little C. I read that the purpose behind Datablocks is for good networking code and they are a special type of object, which is great! But I am confused on some things...
1) My main question is, are datablocks full blown objects themselves with fields and methods? (I think they are called commands instead of methods in TorqueScript)
2) Can I do anything I could with an object, with a Datablock?
3) Are Datablocks instantiated from classes like objects?
4) Can multiple classes be defined in more than one file? (I am used to having individual .class files for every object in Java, with a driver file that runs them)
4)Can a plain old class be used to instantiate objects, like a ball, and get it on screen with physics and animation?
5) If I wanted a 100 balls on screen, would a datablock be better than a plain old object from a class for network preformance only, and not local preformance?
6) Any good referneces on objects and datablocks besides the link at the top?
About the author
Recent Threads
#2
Are all on screen things made with instantiated objects from classes?
02/15/2007 (6:55 pm)
David Higgins, thank you for the explanation... but now I think I've realized there are three types of objects. There are datablocks which are a special type of object, an object which is on the screen like a character or background art piece, and a object in the object-oriented programming sense. My question was was refering to objects of the "object-oriented programming sense", like from classes. objects Do you see what I mean? Are all on screen things made with instantiated objects from classes?
#3
The only things in TGB that are not objects are strings and numbers, although, some strings can represent objects ... such as "1212" could represent a reference to the t2dStaticSprite that contains the "id" of "1212" ... but thats nothing to really worry about until you get a complete grasp on things ...
Anything created with "new" is an object, such as:
And if you dig deep enough into your projects files, you'll see "new" all over the place ... your "t2d" level files are actually Torque Script, and you can look at them and see how your "objects" are created in the level -- or how they were saved out to the file, rather ... as well as alter the file (if you didn't know about that already -- nice for making bulk updates to things, like changing the default size of everything with 2 key strokes)
And, just to clarify, in case this is where your question was going ... all objects in TorqueScript are also objects in C++ ... and all objects in TorqueScript are derived from the SimObject class, and all "scene" objects derive from SceneObject ... etc, etc, etc ... nice little object-tree ...
02/15/2007 (7:46 pm)
@Daniel -- everything you see on the screen is an object, yes -- including the 'screen' itself (it's a "Scene Window" object, which contains a "Scene Graph" object, which contains your "Onscreen Objects")The only things in TGB that are not objects are strings and numbers, although, some strings can represent objects ... such as "1212" could represent a reference to the t2dStaticSprite that contains the "id" of "1212" ... but thats nothing to really worry about until you get a complete grasp on things ...
Anything created with "new" is an object, such as:
%obj = new t2dStaticSprite() { };
new t2dSceneObjectDataBlock() {};And if you dig deep enough into your projects files, you'll see "new" all over the place ... your "t2d" level files are actually Torque Script, and you can look at them and see how your "objects" are created in the level -- or how they were saved out to the file, rather ... as well as alter the file (if you didn't know about that already -- nice for making bulk updates to things, like changing the default size of everything with 2 key strokes)
And, just to clarify, in case this is where your question was going ... all objects in TorqueScript are also objects in C++ ... and all objects in TorqueScript are derived from the SimObject class, and all "scene" objects derive from SceneObject ... etc, etc, etc ... nice little object-tree ...
#4
example
%obj = new t2dStaticSprite() { };
new t2dSceneObjectDataBlock() {};
%obj.SceneObjectDataBlack(); //calling oject
i am asking this because i was thinking of calling diffrent meshes using datablocks to change how a character appears, mainly the clothing and stuff.
02/15/2007 (8:14 pm)
Ok so does that mean that you can call objectes like methods example
%obj = new t2dStaticSprite() { };
new t2dSceneObjectDataBlock() {};
%obj.SceneObjectDataBlack(); //calling oject
i am asking this because i was thinking of calling diffrent meshes using datablocks to change how a character appears, mainly the clothing and stuff.
#5
Objects and Methods are two different things
Methods are functions available through Objects
If you want to change the textures applied to meshes, then you have to either a) change the datablock, or b) assign different values to the datablock, or c) call the 'setSkin' method for the shape object ... though I'm not 100% sure how it's all done, since I've only really coded in TGB so far ...
Also, the term "meshes" is used to describe the stuff a 3d model is made of, not the model itself ... and afaik, TGE doesn't provide any access at the mesh level -- I believe you meant to say "texture" ... ?
02/15/2007 (8:38 pm)
@Saix, you might want to read up on what OOP is ... Objects and Methods are two different things
Methods are functions available through Objects
function someClass::doSomething() { }
%obj = new t2dStaticSprite() { class = "someClass"; };
%obj.doSomething();If you want to change the textures applied to meshes, then you have to either a) change the datablock, or b) assign different values to the datablock, or c) call the 'setSkin' method for the shape object ... though I'm not 100% sure how it's all done, since I've only really coded in TGB so far ...
Also, the term "meshes" is used to describe the stuff a 3d model is made of, not the model itself ... and afaik, TGE doesn't provide any access at the mesh level -- I believe you meant to say "texture" ... ?
#6
what i really meant is....is a datablock an object...and how can you use a datablock for textures (calling datablocks)
i found something called hiding meshes and nodes...could you elaborate to me on what it does and how it works since that resource was made during verision 1.1
also thanks for clearifying whats the difference between a mesh and a texture....i appreciate you answering back, normally expert programmer i know don't have patience with me :P.
02/16/2007 (1:48 pm)
Oops sorry for the confusion....you exaclty right objects are completely differnet between methodswhat i really meant is....is a datablock an object...and how can you use a datablock for textures (calling datablocks)
i found something called hiding meshes and nodes...could you elaborate to me on what it does and how it works since that resource was made during verision 1.1
also thanks for clearifying whats the difference between a mesh and a texture....i appreciate you answering back, normally expert programmer i know don't have patience with me :P.
#7
Write a functional version of the above pseudo-code, and try it ... heh ...
As for swapping out textures on a Shape, all shapes derive from the ShapeBase, and that class has a 'setSkin' method exposed to TorqueScript --
You'll have to read up on the setSkin method itself, and how it works, as I'm not all too familiar with it, but it more or less requires you to have a datablock setup that identifies the 'base' texture, and then a skin would be "base_skinName.jpg" or something like that -- so your base texture would be "player.png", and "player_blue.png" could be used by calling "setSkin("blue");" ...
This is roughly how it works, but there are more details to it ... experiment, and see what happens -- I'm pretty sure if you pass something like "blue" into setSkin, it will report back not being able to find the file "something_blue.png" or "blue_something.png" or whatever ... check your console ;)
02/16/2007 (4:46 pm)
I can't recall off-hand if datablocks can contain methods, but they are in fact objects:function mydataBlock::doSomething() { }
new objectDataBlock(mydataBlock)
{
};
%obj = new Object() { defaultDatablock = "mydataBlock"; }
%obj.doSomething();Write a functional version of the above pseudo-code, and try it ... heh ...
As for swapping out textures on a Shape, all shapes derive from the ShapeBase, and that class has a 'setSkin' method exposed to TorqueScript --
%shape.setSkin("blue");You'll have to read up on the setSkin method itself, and how it works, as I'm not all too familiar with it, but it more or less requires you to have a datablock setup that identifies the 'base' texture, and then a skin would be "base_skinName.jpg" or something like that -- so your base texture would be "player.png", and "player_blue.png" could be used by calling "setSkin("blue");" ...
This is roughly how it works, but there are more details to it ... experiment, and see what happens -- I'm pretty sure if you pass something like "blue" into setSkin, it will report back not being able to find the file "something_blue.png" or "blue_something.png" or whatever ... check your console ;)
#8
i was planning on using the funtion Texture as a gui button to see what will happen
02/16/2007 (6:59 pm)
Before i try what you suggested would this syntax work?datablock ShapeBase(texturedatablock)
{
Filename = "player.png" //player.png is the file name for the 'base' texture
};
%shape = new Texture(){
datablock = "texturedatablock"
};
function Texture(){
%shape.setSkin("blue");
return;
}i was planning on using the funtion Texture as a gui button to see what will happen
#9
And, as I stated already, I'm not really a TGE developer -- so I'm not sure about the use of the Filename field, etc, etc ... but the TorqueScript is definately incorrect.
Texture() is a function, so it must return something ... the use of "%shape = new Texture();" is incorrect, since "new" is for instantiating new instances of objects ... and as Texture is not an object (objects must be defined in the C++ engine) it would fail to compile.
Also, I might add ... it's quite easy to 'test' the code, rather then ask if the syntax is 'correct'
02/16/2007 (9:10 pm)
Texture() is a function in the context of your code, so "new" is incorrectAnd, as I stated already, I'm not really a TGE developer -- so I'm not sure about the use of the Filename field, etc, etc ... but the TorqueScript is definately incorrect.
Texture() is a function, so it must return something ... the use of "%shape = new Texture();" is incorrect, since "new" is for instantiating new instances of objects ... and as Texture is not an object (objects must be defined in the C++ engine) it would fail to compile.
Also, I might add ... it's quite easy to 'test' the code, rather then ask if the syntax is 'correct'
#10
I say this because in C++ you can't declare objects outside of a class, it must be in the same file for instance
Player Orc would be define in the file where the class Player is located..i hope i am making sense...
02/18/2007 (1:39 pm)
@David: Sry for bothering you again...i know you not really a TGE developer (more of a server developer) but you said that "%shape = new Texture();" is incorrect becuase i did not declare it within a class (in this case ShapeBase)I say this because in C++ you can't declare objects outside of a class, it must be in the same file for instance
Player Orc would be define in the file where the class Player is located..i hope i am making sense...
#11
For example, it's like trying to do the following in C++
See how "Texture" is a function, and TextureClass is the actual class ... calling "new Texture()" would cause a compilation error -- in TorqueScript, theres no compile error, because it doesn't know that Texture isn't an available class-object ... HOWEVER, it does generate a run-time error, and would state the following "failed to instantiate instance of a non-conobject" (or something like that)
02/18/2007 (1:52 pm)
You misread what I said ... I said %shape = new Texture(); was incorrect because "Texture" is not a class, it's a function in your code -- For example, it's like trying to do the following in C++
TextureClass Texture()
{
return new TextureClass();
}
TextureClass *texture = new Texture();See how "Texture" is a function, and TextureClass is the actual class ... calling "new Texture()" would cause a compilation error -- in TorqueScript, theres no compile error, because it doesn't know that Texture isn't an available class-object ... HOWEVER, it does generate a run-time error, and would state the following "failed to instantiate instance of a non-conobject" (or something like that)
#12
02/18/2007 (5:47 pm)
@David: I get it now...i am using the same name for the function and the object....well now all i need to find out is how to mount textures onto models via script
Associate David Higgins
DPHCoders.com
For example, creating a 'playerSprite' datablock with some default fields defined, then dropping a t2dStaticSprite on the level and assigning it the 'playerSprite' datablock will then assign all that dataBlocks fields to the sprite -- in TGB, datablocks are used slightly different then they are in TGE ... from my understanding ... in TGB, they have no side-effect on the current networking, as TGB does not send objects across the network like TGE does ...
In TGB, datablocks are used to simply define defaults ...
Say you have 100 enemies in your scroller game ... each of these enemies has a common set of fields you want to define ... rather then copy/pasting one enemy ... you can just create an 'enemy' datablock, then assign it to ANY object in the level (given that the datablock has a 'global' object scope -- if it's an imageMapDatablock ... then you can only apply it to imagemap objects, etc) ... this 'enemy' datablock may define some 'behind the scenes' AI fields ... this ensures the fields exist when the object is created, and you don't have to type the 10 or so fields into the level builder every time you make a new enemy ... but you can change the values on any individual object ...
Datablocks have other uses, but in TGB, thats about the most useful thing they do ...