HowTo/Upgrade obsolete script functions - simple examples

From TrainzOnline
< HowTo
Revision as of 09:16, 26 February 2024 by Pw3r (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

These are examples of solutions for common and simple obsolete Trainzscript functions. This is not an exhaustive list. Solutions for more complex obsolete functions can be found (TBD).

Under Development The contents and the layout of this page is under development.


Contents

class Crossing/Signal/JunctionBase

SecurityToken

With the addition of Interlocking Towers these script classes had a multitude of functions declared obsolete, and new variants added which take a SecurityToken parameter. This new parameter is entirely optional, and may be passed as null. However, if the relevant object is part of an InterlockingTower 'path' then the tower will take ownership of the object, preventing other scripts from modifying state. In this instance, passing a null SecurityToken will cause a script exception. To avoid this, the caller should first check if the signal/junction/crossing is 'owned', but calling GetSignalOwner(), GetJunctionOwner() or GetCrossingOwner() as appropriate.

Error Examples

Error: VE197: Syntax error in script 'somescript.gs' for asset <kuid:0000:0000> "Asset Name".
Error: VE267: somescript.gs(48) : function SetCrossingAutomatic is obsolete in object Crossing.
Error: VE267: somescript.gs(48) : function SetCrossingState is obsolete in object Crossing.

Repair

You need to check whether the crossing is owned, and call the new function variant instead.

Usage example:

 if (!GetCrossingOwner())
 {
   SetCrossingAutomatic(null, false);
   SetCrossingState(null, 1);
 }
 else
 {
   // The crossing is under the control of some other script, and calling the
   // above functions (without a valid SecurityToken) will cause exceptions.
 }

class HTMLWindow

GetCompleteIndustryViewDetailsHTMLCode()

VE267: somescript.gs(linenumber) : function GetCompleteIndustryViewDetailsHTMLCode is obsolete in object HTMLWindow.

This error occurs when trying to use this method with an obsolete class. In this case, class GenericIndustry has been obsoleted and replaced with BaseIndustry.

Repair

Change all instances of 'GenericIndustry' in the code to 'BaseIndustry'.

Usage example:

include "BaseIndustry.gs"
class <myclass> isclass BaseIndustry>

class IndustryProductInfoCollection

GetProductIndexFromAsset()

<kuid2:00000:00000:1> : VE267: multipleindustryplus.gs(711) : function GetProductIndexFromAsset is obsolete in object IndustryProductInfoCollection.

Repair

Replace with GetProductIndex(Asset product) from the same class. The returned value (int) is the same.

GetProcessIndexFromName()

 <kuid2:00000:00000:1> : VE267: multipleindustryplus.gs(2024) : function GetProcessIndexFromName is obsolete in object IndustryProductInfoCollection.

Repair Replace with GetProcessIndex(int productIndex, string processName) from the same class. The returned value (int) is the same.

AddProduct()

 <kuid2:00000:00000:1> : VE267: somescript.gs(999) : function AddProduct(string, string) is obsolete in object IndustryProductInfoCollection.

Repair Replace with AddProduct(Asset product, Vehicle vehicle) from the same class. Instead of using strings for product and vehicle names, you will need to provide a product asset and a vehicle of type Vehicle.

Usage example: TBD

class Train

GetVelocity()

GetVelocity obsolete error or warning: <kuid:0000:0000> : VE267: somescript.gs(299) : function GetVelocity is obsolete in object Train.


Repair

You have two replacement options in the Train class (train.gs):


float GetSmoothedVelocity(void);

This function return the trains velocity in metres per second and is suitable for human viewing such as a cab display.


float GetTrainVelocity(void);

This function provides the instantaneous velocity for the train, which is good for physics calculations but not good for human-readable display.

Usage example:

float mySpeed;
mySpeed = GetSmoothedVelocity();  // this is the more likely option but depends on what the overall script is trying to do.

// or the more accurate version
mySpeed = GetTrainVelocity();

class Vehicle

GetFacingRelativeToTrain()

???GetFacingRelativeToTrain obsolete error or warning: <kuid:0000:0000> : VE267: somescript.gs(450) : function GetFacingRelativeToTrain is obsolete in object Vehicle.


Repair

You have one replacement option in the Vehicle class (vehicle.gs):


bool GetDirectionRelativeToTrain(void);

This function returns true if the vehicle faces the same way as the Train, false otherwise. This function is a bit tricky to understand and especially when a vehicle, such as a coach/carriage, is involved.

Usage example1:

bool sameDirectionAsTrain = GetDirectionRelativeToTrain();  

Usage example2:

Vehicle[] trainVehicles = GetMyTrain().GetVehicles();
int i;
for (i = 0; i < trainVehicles.size(); ++i)
{
  bool sameDirectionAsTrain = trainVehicles[i].GetDirectionRelativeToTrain();
  if (sameDirectionAsTrain)
    Interface.Print("Vehicle " + i + " is facing forward");
  else
    Interface.Print("Vehicle " + i + " is facing backwards"); 
}
Personal tools