Author Topic: Functions, Datablocks, And ScriptObject | Question...  (Read 1250 times)

What is the difference between a function alone, Datablock, and a ScriptObject? I'm not fully grasping the understanding as to why I need a ScriptObject and why is it different from a Datablock...

1.Like is a Datablock just a virtually physical thing with a bunch of script inside of it happening in the game?
2.What makes functions and SO different?
3.Define your own definition of a SO please.

Function is like a bookmark of code you can call whenever including but not limited to creating datablocks, scriptobjects etc.

A scriptobject is Torquescript's version of object-oriented programming. It gets very in depth with namespaces, but steer clear of them until you get a clear understanding of what the purpose of scriptobjects can be or you might get discouraged.
You can create a scriptobject with it's own values like this:
Code: [Select]
$name = new scriptObject(name)
{
    int = 8;
    string = "foo";
};

You can later call these default values with either name.int and name.string or $name.int and $name.string. However setting properties to scriptObjects is not limited to default values. You can just set dynamic fields like so: name.foo = "bar" name.bool = true;. The basic usefulness of object-orientated programming is that you can assign your functions and variables to a object.

A datablock defines what some physical object looks like, but isn't itself a physical object.
A ScriptObject isn't a physical object, and is primarily used as a base for non-physical classes.
A function is something that can be called to do some repetitive or complicated task, without having to rewrite the whole of its code each time.

Datablocks are objects (clusters of information) that are downloaded by clients when they connect. Once the game starts, they cannot (or should not) be changed. For example, the wand item has two primary datablocks, one which is the item when its on the ground or spawned from a brick ('item'), and one which is when its in a players hand ('image'). The image datablock has all the data about how the weapon fires, like if you click and hold if it keeps firing automatic, or if you've got to click and release each time.

The main advantage of them is that clients can do some 'client sided prediction' to predict what will happen when doing things, to significantly reduce the effects of lag. The client doesn't need to be told that when they fire the weapon, it plays a set animation, then produces particles, then fires a projectile, because the client already knows that it should do that.


Script objects are more complicated, but very useful to the intermediate coder. If you're at the point where you are coding full add-ons (with the exception of mainly copy-paste add-ons, weapons, particles), and have not learnt about script objects, I seriously recommend that you learn them. They completely changed how I coded things, and allowed for a whole ton of new opportunities.


A function is a collection of code that you can run from anywhere. They are a basic programming concept, so you can just use google to find your answer.

Once the game starts, they cannot (or should not) be changed.

You can change and create new datablocks easily as long as the clients already have all required resource files

Datablocks define information that is going to be used by many different objects to prevent redundancy. For example, a player object. There's a lot of variables that are specific to that player, so they can't be defined on a datablock: colors, items, health, etc. However, there's a lot of data that's the same for every single player: maximum health, the model they use, if they can jet, how high they jump, how fast they move (by default, this can now be changed per player), information about animations, and so much more. That data is relevant to every single player object of that type, so you set the player's datablock to that datablock and they inherit all of that information.

Functions are a list of instructions for the game to perform when that function is run. Functions are like buttons, you 'press' them and something happens. In programming lingo, we call that 'calling' them.

Script Objects are neither datablocks nor functions, and share almost no similar traits with either. Script Objects are generic objects that have no physical representation in the game. They're used to store information in a categorized way. They can't perform actions on their own like functions, and they don't tell other objects essential information about themselves like datablocks.