Class TrainUtil

From TrainzOnline
Jump to: navigation, search


  • TrainUtil is a static utility class containing a miscellany of methods.
  • TrainUtil is located in common.gs.


Contents

AlreadyThereStr

public bool AlreadyThereStr(string[ ] target, string search)
Parameters
  • target = Array of strings to search.
  • search = String to look for.
Returned Value
  • Boolean indicating whether or not the string search exists in the target array.
Syntax
string[] target = Str.Tokens("one two three");
bool found = TrainUtil.AlreadyThereStr(target,"four");    // the result will be false
Notes
  • As is usual in Trainzscript string methods the search is case sensitive.


AreInTrain

public bool AreInTrain(Train train, Vehicle[ ] vehicles)
Parameters
  • train = Train to search.
  • vehicles = Array of vehicle to look for.
Returned Value
  • True if all of the vehicles in the array are found in the train, false otherwise.
Syntax
bool allPresent = TrainUtil.AreInTrain(train,vehicles);
Notes


CompareStrLists

public bool CompareStrLists(string[ ] list1, string[ ] list2)
Parameters
  • list1,list2 = String arrays.
Returned Value
  • True if the lists are identical, same strings in each index position, same total count. False otherwise.
Syntax
bool identical = TrainUtil.CompareStrLists(address1,address2);
Notes


CreateTrainFromSoup

public Train CreateTrainFromSoup(Soup trainSoup, string trackmarkName, bool direction)
Parameters
  • trainSoup = Consist definition in the form returned by World.GetSurveyorSavedConsist().
  • trackmarkName = Name of the trackmark where the train is to be placed.
  • direction = True denotes that the train direction should match that of the trackmark.
Returned Value
  • A reference to the newly created train if successful, null otherwise.
Syntax
Train train = TrainUtil.CreateTrainFromSoup(BrightonBelle,"Victoria Station",true);
Notes
  • It is the script programmer's responibility to ensure that sufficient track exists.
  • See also World.CreateTrain().


DoCoupleUp

public Train DoCoupleUp(Train train, Train coupleTo)
  • train = First train.
  • coupleTo = Train to couple with.
Returned Value
  • A reference to the newly extended train.
Syntax
Train newTrain = TrainUtil.DoCoupleUp(train, newSection);
Notes


DoDecouple

public Train DoDecouple(Train train, int count)
Parameters
  • train = Train reference.
  • count = Number of wagons to detach from the rear of the train.
Returned Value
  • A reference to the remaining consist (front section) if successful, null otherwise.
Syntax
train = TrainUtil.DoDecouple(train,4);  // drop four cars from the rear of the train, train will be null if this fails.
Notes
  • It is assumed that the train is stopped and already in position.
  • The method will wait for the operation to be completed.


GetAfter

public string GetAfter(string target, string search)
Parameters
  • target = String to operate on.
  • search = String containing delimiter characters.
Returned Value
  • Substring of target to the right of the characters in search.
Syntax
string result = TrainUtil.GetAfter("Afternoon", "er");   // result is "noon"
Notes
  • The method starts at the end of the string and works backwards until any of the characters in search are encountered.
  • Once any character in search is found the substring after that character is returned.
  • If search is not found the result will be an empty string.


GetUpTo

public string GetUpTo(string target, string search)
Parameters
  • target = String to operate on.
  • search = String containing delimiter characters.
Returned Value
  • Substring of target to the left of the characters in search.
Syntax
string result = TrainUtil.GetUpTo("Before noon"," no");   // result is "Before"
Notes
  • The method starts at the beginning of the string and works forwards until any of the characters in search are encountered.
  • Once any character in search is found the substring before that character is returned.
  • If search is not found the result will be an empty string.


HasPrefix

public bool HasPrefix(string target, string prefix)
Parameters
  • target = String to operate on.
  • prefix = String containing prefix to search for.
Returned Value
  • True if target starts with prefix, false otherwise.
Syntax
bool IsPrefixed = TrainUtil.HasPrefix("ante","antecedent"); // returns true
Notes


HasSufix

public bool HasSufix(string target, string suffix)
Parameters
  • target = String to operate on.
  • suffix = String containing suffix to search for.
Returned Value
  • True if target ends with suffix, false otherwise.
Syntax
bool IsSuffixed = TrainUtil.HasSufix("dent","antecedent"); // returns true
Notes
  • This will not work if the method identifier is correctly spelled!


IsInTrain

public bool IsInTrain(Train train, Vehicle vehicle)
Parameters
  • train = Train reference.
  • vehicle = Vehicle reference.
Returned Value
  • True if vehicle is included in train, false otherwise.
Syntax
bool brakeVanPresent = TrainUtil.IsInTrain(train,brakeVan); 
Notes


IsPartTrainInTrigger

public bool IsPartTrainInTrigger(GameObject runningThread, Train train, Trigger trigger)
Parameters
  • runningThread = Thread that called this method.
  • train = Reference to a train.
  • trigger = Reference to the trigger to query.
Returned Value
  • True if any part of the train is in the trigger area, false otherwise.
Syntax
bool arrived = TrainUtil.IsPartTrainInTrigger(me,train,trigger);
Notes


IsTrainInTrigger

public bool IsTrainInTrigger(GameObject runningThread, Train train, Trigger trigger)
Parameters
  • runningThread = Thread that called this method.
  • train = Reference to a train.
  • trigger = Reference to the trigger to query.
Returned Value
  • True if the train is in the trigger area, false otherwise.
Syntax
bool arrived = TrainUtil.IsTrainInTrigger(me,train,trigger);
Notes


RandBool

public bool RandBool(void)
Parameters
  • None
Returned Value
  • True or false at random.
Syntax
if(TrainUtil.RandBool()) DoItAtRandom();
Notes


RunTurnTable

Is this still valid?

public void RunTurnTable(GameObject runningThread, Train train, string turntable)
  • runningThread = Thread that called this method.
  • train = Reference to a train.
  • turntable = Name of turntable.
Returned Value
  • None
Syntax
TrainUtil.RunTurnTable(me,train,"Turntabel 1");
Notes
  • ???????


StrSubst

public string StrSubst(string target, string remove, string replace)
Parameters
  • target = String to operate on.
  • remove = Substring to remove.
  • replace = Replacement substring.
Returned Value
  • Returns the string target with replace substituted for remove, if the method fails an empty string is returned.
Syntax
string s = "Gnomes are your friends";
s = TrainUtil.StrSubst(s,"friends","enemies");    // returns "Gnomes are your enemies"
Notes
  • remove must exist as a substring of target, otherwise and empty string will be returned.
  • remove and replace do not have to be the same length.
  • Only the first instance of remove will be substituted.


WaitUntilHas

Is this still valid?

public void WaitUntilHas(Vehicle loco, ShuntTrack tracking)
Parameters
  • loco = Consist locomotive.
  • tracking = Tracking array.
Returned Value
  • None
Syntax
Notes
  • Waits until all of the vehicles tracked by the tracking array are in the train of the given loco.


Related Methods

Categories

Personal tools