Game Development Community

Defining new datablocks

by Tim Doty · in Torque Game Builder · 04/10/2005 (10:04 am) · 18 replies

I thought I'd seen something about this in the forums, but a search came up with a lot of other stuff, but not this.

How do I define a new datablock? When I do:
datablock myNewDatablock(datablockName) {
  attrib = 5;
  };
I get an error about not finding the class rep for dynamic class myNewDatablock. This datablock isn't based on anything, just a convenient way to have some parameters grouped and defined.

#1
04/10/2005 (10:57 am)
I don't think you can do that but I'm not a script-guru or anything. As far as I understand, "myNewDatablock" has to be a class within the engine.

- Melv.
#2
04/10/2005 (11:09 am)
I'd thought maybe that was the case. No problem, I'll just implement thinks as an OO class ;^)

Thanks, Melv!

:: Tim Doty
#3
04/10/2005 (11:40 am)
Yes the datablock has to be an actual datablock class from the engine side.

A ScriptObject works well for what you want to do.
#4
04/11/2005 (3:45 am)
John is absolutely correct and I should've pointed that out.

%object = new ScriptObject()
{
    class = myNewDatablockClass;

    attrib = 5;
};

... but don't forget to delete it when you've finished with it.

- Melv.
#5
04/11/2005 (5:53 am)
I'd thought about ScriptObject but didn't realize the correct syntax. What I ended up doing was extending the base T2D datablock. It didn't have much extra so it worked. But this lets me know how to do what I wanted.

Thanks to both of you!
#6
04/11/2005 (6:02 am)
Don't forget as well Tim that you can do so much more with Script-Objects like defining the following from the above object...
%object = new ScriptObject()
{
    class = myNewDatablockClass;

    attrib = 5;
};

function myNewDatablockClass::setAttribute(%this, %value)
{
    %this.attrib = %value;
}

%object.setAttribute( 100 );

... and much more besides.

- Melv.
#7
04/11/2005 (8:12 am)
@Melv: Thanks! I was starting to suspect that -- but I hadn't quite worked out being able to define new functions for them (I forget about what can be done without the OO resource). That will help with a different project. The support you provide is amazing.
#8
04/11/2005 (8:30 am)
Script Objects are great... they naturally are OO... lol too bad I didn't know how viable they were when doing my TorqueScript DB... might have to go in and make it completely inheritable
#9
04/11/2005 (8:53 am)
You can also do it like this, just for reference.

%object = new ScriptObject(myClass);
%object.attrib = 5;

function myClass::setAttribute(%this, %value)
{    
     %this.attrib = %value;
}

%object.setAttribute( 100 );
#10
04/11/2005 (12:32 pm)
@Matt: the problem with T2D is that you can't subclass in the torquescript which is why Bryan Edds did the extremely useful OO resource. My background is C/C++ programming so I find it much more natural to do things with the OO resource. I thought I'd figured datablocks out, but I'm still learning.

@John: Thanks for the reference.
#11
04/11/2005 (12:43 pm)
I could be mistaken but I 'think' you can s ubclass in ScriptObjects... but not SimObjects (which it and many things inherit from)... I think he move the Script Object subclassing up to SimObject so everything that inherits from SimObject would get the subclass... think I spotted this in the source also... again I might be mistaken
#12
04/11/2005 (1:09 pm)
@Matt: You're probably right. But if I couldn't subclass fxStaticSprite2D (and related) it would make the programming more complex. The ScriptObject is great for when you don't want to carry extra baggage, but sometimes you need the extra baggage.
#13
04/11/2005 (2:56 pm)
Very very true... in fact instead of implementing script OO I plan on creating sub classes of the C++ counterparts and exposing them to script
#14
04/11/2005 (2:58 pm)
Which is really the way its intended.
#15
04/11/2005 (9:20 pm)
Intended perhaps... but the best approach in all cases?

I prefer keeping all my own code in script - it's a great layer between the T2D platform in the engine. T2D subclass code in script can be recompiled and reloaded while the game is running, and I find it sooo much easier and safer (minus type and const-safety) to code in script than in C++, as well as more consistant and conveniant.

In other words, I find extending T2D classes in script the best approach for me because I prefer the features of subclasses defined in script to doing it all in C++. I'm sure other people prefer to extend T2D in C++ for reasons I disagree with, developer's original intent possibly being among them.
#16
04/11/2005 (9:25 pm)
Of course, if the extension of a T2D class was very low level (like make a whole new fxCrazyAnimation2D class from the fxSceneObject2D class), then I would totally recommend using C++. But if you're just taking an fXAnimatedSprite and subclassing it to a high level PlayerCharacterSprite class, then I'd recommend keeping the inheritance stuff in script (assuming you're using my OOInT2DScript resource).
#17
04/11/2005 (9:35 pm)
I am a very big supporter of using TorqueScript :) lol made a database in it and it works quite efficiently (a bit surprised how efficiently)... though like Bryan is saying theres a point where C++ objects really have an advantage...
#18
01/03/2010 (8:36 pm)
Matt, you mentioned you made a database utilizing TorqueScript. I am coming from a .NET/MSSQL background where data is almost always stored in databases. If I wanted to make an MMO with T3D, would a server-side MSSQL database model work? Or perhaps I should ask my question this way: How are objects like player inventory and other items stored and persisted in the T3D engine? Obviously they are stored on the server. I don't see folks trying to store items in databases, and if they are stored in flat files on the server, is this an improvement on speed over a transactional database like MSSQL? I heard that some MMO's are made using SQL, and it might be debatable, but Torque doesn't use that model, or does it? I invite input from anyone. Thanks!