HowTo/Upgrade obsolete script functions - simple examples

From TrainzOnline
< HowTo(Difference between revisions)
Jump to: navigation, search
Line 5: Line 5:
  
 
   
 
   
=class Crossing=
+
=class Crossing/Signal/JunctionBase=
  
==SetCrossingAutomatic()==
+
==SecurityToken==
==SetCrossingState()==
+
  
 +
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".<br>
 
Error: VE197: Syntax error in script 'somescript.gs' for asset <kuid:0000:0000> "Asset Name".<br>
 
Error: VE267: somescript.gs(48) : function SetCrossingAutomatic is obsolete in object Crossing.<br>
 
Error: VE267: somescript.gs(48) : function SetCrossingAutomatic is obsolete in object Crossing.<br>
 
Error: VE267: somescript.gs(48) : function SetCrossingState is obsolete in object Crossing.
 
Error: VE267: somescript.gs(48) : function SetCrossingState is obsolete in object Crossing.
 
This method now requires a SecurityToken as a parameter to the function call
 
  
 
'''Repair'''
 
'''Repair'''
  
You need to initialize a SecurityToken, then add it as a parameter to your function calls.
+
You need to check whether the crossing is owned, and call the new function variant instead.
  
 
'''Usage example:'''
 
'''Usage example:'''
  
In your header add
+
<code>
 
+
  if (!GetCrossingOwner())
SecurityToken stoken;
+
  {
 
+
    SetCrossingAutomatic(null, false);
Then in your function calls add your token.
+
    SetCrossingState(null, 1);
 
+
  }
SetCrossingAutomatic(stoken, false);
+
  else
SetCrossingState(stoken, 1);
+
  {
 
+
    // The crossing is under the control of some other script, and calling the
More information on [[Class Crossing]]
+
    // above functions (without a valid SecurityToken) will cause exceptions.
 +
  }
 +
</code>
  
  
Line 40: Line 42:
 
VE267: somescript.gs(linenumber) : function GetCompleteIndustryViewDetailsHTMLCode is obsolete in object HTMLWindow.<br>
 
VE267: somescript.gs(linenumber) : function GetCompleteIndustryViewDetailsHTMLCode is obsolete in object HTMLWindow.<br>
  
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.
+
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'''
 
'''Repair'''
Line 92: Line 94:
 
''float GetSmoothedVelocity(void);''
 
''float GetSmoothedVelocity(void);''
  
This function return the train's velocity in metres per second and is suitable for human viewing such as a cab display.
+
This function return the trains velocity in metres per second and is suitable for human viewing such as a cab display.
  
 
    
 
    
 
''float GetTrainVelocity(void);''  
 
''float GetTrainVelocity(void);''  
 
    
 
    
This function provides the instantaneous velocity for vehicles in this train, which is good for physics calculations but not good for human-readable display.<br><br>
+
This function provides the instantaneous velocity for the train, which is good for physics calculations but not good for human-readable display.<br><br>
 
   
 
   
 
'''Usage example:'''  
 
'''Usage example:'''  
Line 122: Line 124:
 
''bool GetDirectionRelativeToTrain(void);''
 
''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.
+
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:'''  
 
'''Usage example1:'''  
 
Vehicle myVehicle;
 
bool sameDirectionAsTrain;
 
 
   
 
   
  sameDirectionAsTrain = GetDirectionRelativeToTrain();   
+
  bool sameDirectionAsTrain = GetDirectionRelativeToTrain();   
 
   
 
   
 
'''Usage example2:'''  
 
'''Usage example2:'''  
 +
 +
Vehicle[] trainVehicles = GetMyTrain().GetVehicles();
  
Vehicle[] trainVehicles;
 
Vehicle myVehicle;
 
bool sameDirectionAsTrain;
 
 
  int i;
 
  int i;
   
+
  for (i = 0; i < trainVehicles.size(); ++i)
trainVehicles = GetMyTrain().GetVehicles();
+
{
+
  bool sameDirectionAsTrain = trainVehicles[i].GetDirectionRelativeToTrain();
if (trainVehicles) {  //unlikely to be null but ...
+
  if (sameDirectionAsTrain)
+
    Interface.Print("Vehicle " + i + " is facing forward");
  for (i = 0; i < trainVehicles.size(); ++i) {
+
  else
    //get direction of this vehicle in the train
+
    Interface.Print("Vehicle " + i + " is facing backwards");  
    sameDirectionAsTrain = trainVehicles[i].GetDirectionRelativeToTrain();
+
    if (sameDirectionAsTrain) {
+
      // do what is necessary here
+
      Interface.Print("Vehicle "+ (i+1) + " is facing forward"); // add 1 so that the first vehicle is number 1 and not 0 (zero)
+
    } else {
+
      Interface.Print("Vehicle "+ (i+1) + " is facing backwards");  
+
    }
+
  }
+
+
 
  }
 
  }

Revision as of 09:16, 26 February 2024

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