HowTo/Your first Trainz Script

From TrainzOnline
< HowTo
Revision as of 06:28, 19 April 2016 by Callavsg (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 "screnery-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:

include "MapObject.gs"

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