HowTo/Your first Trainz Script

From TrainzOnline
Jump to: navigation, search

Scripting is one of most powerful tools when creating trainz assets. More and more content creators are using scripts to extend the funciontallity of their contents, so scripters, like me, get a lot of request for script work. Finally, I thought, even for small functionallity, it'll be a good idea, if more people know how to script.

I'm from Germany and this is my first article here, so feel free to send corrections or if you have the permission, feel free to correct this article on your own!

First, let us take a theoretical approach:

Trainz Script is a small programming language and has many similar aspects to Java or C++. I'll say it's a kind of a Framework, that allows us to implement new features based on built-in classes and their members. That means we have to assign our assets to a script class. Without a custom script, we do it by selecting the kind of our asset in config.txt. A kind of "scenery" (not "scenery-trackside"!) will assign the class "MapObject" to our asset. So, if we want to extend our asset of Class MapObject, we'll have to extend the class itself. Extending a class means to create a custom script file with a custom class declaration inside of it that inherits from the Class MapObject. "MapObject" then becomes our parent class and we have all functionallity of this class within our custom implemented class.

A basic script may look like this:


myscript.gs
include "MapObject.gs"

class CMyClass isclass MapObject
{
    public void Init(Asset pAsset)
    {
        inherited(pAsset);
    }
};

As you see in the little script above, our class already has a member function. This member function (Init) is the most important member we have to implement. This little function initializes our class and inherits all the properties of our parent class (see: inherited(pAsset);). Without that function with its call to inherited, functionality of our asset is not granted, because in this case our parent class won't be initialized.


Notice:

1. Never forget the "Init"-Method (method = function within class)!

2. Never forget to call "inherited"!


In the config.txt of our asset, we need to set two new tags! The first tag is the "script"-tag and the second one is the "class"-tag. We have to fill these tags with our script information.

script: The filename of our script

class: The class we created for our


In our case the config.txt may look like:

trainz-build                            3.7
kuid                                    [...]
category-class                          "[...]"
category-region                         "[...]"
category-era                            "[...]"
kind                                    "scenery"
description                             "[...]"
username                                "[...]"
script                                  "myscript.gs"
class                                   "CMyClass"

thumbnails
{
    [...]
}

mesh-table
{
    [...]
}

kuid-table
{
    [...]
}


What we've learned today

- If we want to extend the functionallity of an asset, we have to extend the Trainz-Script-Class that belongs to our asset (here Class MapObject)

- We extend our class by implementing a custom class that inherits from our parent class (CMyClass inherits from Class MapObject)

- All Trainz-Classes that assets can inherit from have the Init-Method and custom classes have to implement the Init-Method, too!


If you want to contact me, you may use this e-mail: callavsg@gmx.de

Until next time!

callavsg

Return to Index

<< How To Guides

Personal tools