Class TrainUtil
From TrainzOnline
(Difference between revisions)
m |
m (→CreateTrainFromSoup: Note existence of World.CreateTrain) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | *[[TrainzScript | + | *[[TrainzScript Library Reference|API Hierarchy]] |
**TrainUtil | **TrainUtil | ||
<br> | <br> | ||
*TrainUtil is a static utility class containing a miscellany of methods. | *TrainUtil is a static utility class containing a miscellany of methods. | ||
+ | *TrainUtil is located in '''common.gs'''. | ||
<br> | <br> | ||
Line 21: | Line 22: | ||
==AreInTrain== | ==AreInTrain== | ||
− | {{MethodHeader|public bool AreInTrain(Train train, Vehicle[ ] vehicles)}} | + | {{MethodHeader|public bool AreInTrain([[Class Train|Train]] train, [[Class Vehicle|Vehicle]][ ] vehicles)}} |
;Parameters | ;Parameters | ||
*'''train''' = Train to search. | *'''train''' = Train to search. | ||
Line 45: | Line 46: | ||
==CreateTrainFromSoup== | ==CreateTrainFromSoup== | ||
− | {{MethodHeader|public Train CreateTrainFromSoup(Soup trainSoup, string trackmarkName, bool direction)}} | + | {{MethodHeader|public [[Class Train|Train]] CreateTrainFromSoup([[Class Soup|Soup]] trainSoup, string trackmarkName, bool direction)}} |
;Parameters | ;Parameters | ||
*'''trainSoup''' = Consist definition in the form returned by [[Class World#GetSurveyorSavedConsist|World.GetSurveyorSavedConsist()]]. | *'''trainSoup''' = Consist definition in the form returned by [[Class World#GetSurveyorSavedConsist|World.GetSurveyorSavedConsist()]]. | ||
Line 56: | Line 57: | ||
;Notes | ;Notes | ||
*It is the script programmer's responibility to ensure that sufficient track exists. | *It is the script programmer's responibility to ensure that sufficient track exists. | ||
+ | *See also [[Class World#CreateTrain|World.CreateTrain()]]. | ||
<br> | <br> | ||
==DoCoupleUp== | ==DoCoupleUp== | ||
− | {{MethodHeader|public Train DoCoupleUp(Train train, Train coupleTo)}} | + | {{MethodHeader|public [[Class Train|Train]] DoCoupleUp([[Class Train|Train]] train, [[Class Train|Train]] coupleTo)}} |
*'''train''' = First train. | *'''train''' = First train. | ||
*'''coupleTo''' = Train to couple with. | *'''coupleTo''' = Train to couple with. | ||
Line 70: | Line 72: | ||
==DoDecouple== | ==DoDecouple== | ||
− | {{MethodHeader|public Train DoDecouple(Train train, int count)}} | + | {{MethodHeader|public [[Class Train|Train]] DoDecouple([[Class Train|Train]] train, int count)}} |
;Parameters | ;Parameters | ||
*'''train''' = Train reference. | *'''train''' = Train reference. | ||
Line 139: | Line 141: | ||
==IsInTrain== | ==IsInTrain== | ||
− | {{MethodHeader|public bool IsInTrain(Train train, Vehicle vehicle)}} | + | {{MethodHeader|public bool IsInTrain([[Class Train|Train]] train, [[Class Vehicle|Vehicle]] vehicle)}} |
;Parameters | ;Parameters | ||
*'''train''' = Train reference. | *'''train''' = Train reference. | ||
Line 152: | Line 154: | ||
==IsPartTrainInTrigger== | ==IsPartTrainInTrigger== | ||
− | {{MethodHeader|public bool IsPartTrainInTrigger(GameObject runningThread, Train train, Trigger trigger)}} | + | {{MethodHeader|public bool IsPartTrainInTrigger([[Class GameObject|GameObject]] runningThread, [[Class Train|Train]] train, [[Class Trigger|Trigger]] trigger)}} |
;Parameters | ;Parameters | ||
*'''runningThread''' = Thread that called this method. | *'''runningThread''' = Thread that called this method. | ||
Line 165: | Line 167: | ||
==IsTrainInTrigger== | ==IsTrainInTrigger== | ||
− | {{MethodHeader|public bool IsTrainInTrigger(GameObject runningThread, Train train, Trigger trigger)}} | + | {{MethodHeader|public bool IsTrainInTrigger([[Class GameObject|GameObject]] runningThread, [[Class Train|Train]] train, [[Class Trigger|Trigger]] trigger)}} |
;Parameters | ;Parameters | ||
*'''runningThread''' = Thread that called this method. | *'''runningThread''' = Thread that called this method. | ||
Line 190: | Line 192: | ||
==RunTurnTable== | ==RunTurnTable== | ||
<font color=red>Is this still valid?</font> | <font color=red>Is this still valid?</font> | ||
− | {{MethodHeader|public void RunTurnTable(GameObject runningThread, Train train, string turntable)}} | + | {{MethodHeader|public void RunTurnTable([[Class GameObject|GameObject]] runningThread, [[Class Train|Train]] train, string turntable)}} |
*'''runningThread''' = Thread that called this method. | *'''runningThread''' = Thread that called this method. | ||
*'''train''' = Reference to a train. | *'''train''' = Reference to a train. | ||
Line 221: | Line 223: | ||
==WaitUntilHas== | ==WaitUntilHas== | ||
<font color=red>Is this still valid?</font> | <font color=red>Is this still valid?</font> | ||
− | {{MethodHeader|public void WaitUntilHas(Vehicle loco, ShuntTrack tracking)}} | + | {{MethodHeader|public void WaitUntilHas([[Class Vehicle|Vehicle]] loco, ShuntTrack tracking)}} |
;Parameters | ;Parameters | ||
*'''loco''' = Consist locomotive. | *'''loco''' = Consist locomotive. |
Latest revision as of 03:20, 29 December 2020
- API Hierarchy
- TrainUtil
- TrainUtil is a static utility class containing a miscellany of methods.
- TrainUtil is located in common.gs.
Contents |
[edit] 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.
[edit] AreInTrain
- 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
- See also IsInTrain()
[edit] 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
[edit] CreateTrainFromSoup
- 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().
[edit] DoCoupleUp
- 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
[edit] DoDecouple
- 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.
[edit] 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.
[edit] 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.
[edit] 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
[edit] 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!
[edit] IsInTrain
- 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
- See also AreInTrain()
[edit] IsPartTrainInTrigger
- 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
[edit] IsTrainInTrigger
- 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
[edit] RandBool
public bool RandBool(void)
- Parameters
- None
- Returned Value
- True or false at random.
- Syntax
if(TrainUtil.RandBool()) DoItAtRandom();
- Notes
[edit] 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
- ???????
[edit] 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.
[edit] 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.