Getting Information From the Game, Writing Your Own Methods.

From TrainzOnline
Jump to: navigation, search

Although we can't control when scripts are called by the game, we can control what happens when the various standard methods are actually executed. We could add pages of code to our Init() method to do all sorts of things, but it makes life more manageable if we write our own methods to carry out a specific task and have them called by Init().

Reopen Tutorial.gs and edit it, or copy and paste, to look like this:

include "MapObject.gs"

class Tutorial isclass MapObject {

   string GetTime(float GameTime) {
      string result = "Good Afternoon";
      if (GameTime >= 0.25) result = "Good Evening";
      if (GameTime >= 0.5) result = "Good Morning";
      return result;
   }

   public void Init(void) {
      inherited();
      string Now = GetTime(World.GetGameTime());
      Interface.ShowPopupHelp("Hello World, " + Now,GetAsset(),Interface.GetTimeStamp())
      //Exception("Hello World, " + Now + "\n\n"); // use this instead of ShowPopupHelp in old builds
   }

};

You will have created a new method called GetTime() and added a call to that method in Init().
Looking at the code as before:

string GetTime(float GameTime) {

  • This is the method prototype which defines the information required and the data which will be returned to the caller.
  • This method will not be accessible to external scripts so the public qualifier is not required
  • string indicates that the method will return data as a character string.
  • GetTime is the name of the method.
  • (float GameTime) means that the method will need one float (real number) as a parameter.

string result = "Good Afternoon"
declares a string variable and assigns a value to it.

if (GameTime >= 0.25) result = "Good Evening";
looks at the value of the float parameter and alters the value of result if it is 0.25 or more.

if (GameTime >= 0.5) result = "Good Morning";
does the same thing for a GameTime value of 0.5 or more.

return result;
sends the value back to the caller as a string.

}
indicates that the method code is complete.


You will notice that Init() has also has been amended.

string Now = GetTime(World.GetGameTime());

  • declares a string variable and assigns the value to be returned by our new method.
  • World.GetGameTime() is a built-in function that provides a value between 0 and 1, representing the time in Trainz.
  • This value is sent directly to the GetTime function to provide the parameter which it needs to decide whether it is morning, afternoon or evening.
  • The character string produced by GetTime is assigned to Now and then added into our message.


Save Tutorial.gs and recommit the asset. When you open Surveyor and place a new copy of your asset (or reopen a map with a copy included) this should be the result:


GoodMorning.jpg

  • Your message might of course be different if you have Surveyor set to a different time.
  • It would have been perfectly possible for GetTime to have returned a properly formatted time such as 23:15:34.

Navigation

Personal tools