HowTo/Read string table or Config text
(→Reading the string table or configtext of another object) |
|||
| (9 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | This page is the continuation of the | + | This page is the continuation of the [[HowTo/Upgrade obsolete script functions]] |
| − | More precisely, it's about the new method to read the | + | More precisely, it's about the new method to read the config file of an object, and thus its string table or another element of its configtext. Two cases are presented to us : the reading concerns the own string table or configtext of an object (in Trainz, an asset), or we want to read the same thing from an object into another object. |
| Line 11: | Line 11: | ||
{ | { | ||
inherited(asset); | inherited(asset); | ||
| − | } | + | } |
| − | + | ||
This method gets the asset subject of this script as a parameter. This guarantees that the config file is already available for reading. | This method gets the asset subject of this script as a parameter. This guarantees that the config file is already available for reading. | ||
| Line 26: | Line 25: | ||
Here is a basic exemple. | Here is a basic exemple. | ||
| − | Assume we have an object whith this string-table: | + | Assume we have an object whith this string-table : |
String-table | String-table | ||
| Line 57: | Line 56: | ||
== Reading the string table or configtext of another object == | == Reading the string table or configtext of another object == | ||
| − | This time, the | + | This time, the string-table or the configtext of the second object is not directly accessible. We have to introduce an additional step. |
After the mandatory 'Init (Asset asset)....', we have to launch an asynchronous query to cache the config file of the second object. But don't worry, it's finally quite simple. | After the mandatory 'Init (Asset asset)....', we have to launch an asynchronous query to cache the config file of the second object. But don't worry, it's finally quite simple. | ||
| Line 63: | Line 62: | ||
Here are the codes to implement : | Here are the codes to implement : | ||
| − | + | AsyncQueryHelper asyncqueryhelper = SecondObject.GetAsset().CacheConfigSoup(); //Starts a query to cache the config file of the second object | |
asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the query to be completed | asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the query to be completed | ||
string ExtractFromStringTable=SecondObject.GetAsset().GetStringTableCached().GetString("string0"); //Reads a string 'string0' into the string table of the second object | string ExtractFromStringTable=SecondObject.GetAsset().GetStringTableCached().GetString("string0"); //Reads a string 'string0' into the string table of the second object | ||
| + | Soup SecondObjectconfigSoup = SecondObject.GetAsset().GetConfigSoupCached().GetNamedTag("xxx"); //Reads the tag 'xxx' into the configtext of the second object | ||
To note : | To note : | ||
| Line 74: | Line 74: | ||
| − | Here an exemple of implementation, with a loco that needs to read the | + | Here an exemple of implementation, with a loco that needs to read the string-table or the configtext of the cars of its train. |
include "locomotive.gs" | include "locomotive.gs" | ||
| Line 82: | Line 82: | ||
thread void ReadConfigFile(void); | thread void ReadConfigFile(void); | ||
| − | public void Init ( | + | public void Init (Asset asset) |
| − | { | + | { |
| − | inherited(); | + | inherited(asset); |
ReadConfigFile(); | ReadConfigFile(); | ||
} | } | ||
| Line 91: | Line 91: | ||
{ | { | ||
Train mytrain = me.GetMyTrain(); //Gets the train to which the loco belongs | Train mytrain = me.GetMyTrain(); //Gets the train to which the loco belongs | ||
| − | Vehicle[]vehicles = mytrain.GetVehicles(); //Makes up a list of the cars in this train, including the loco | + | Vehicle[]vehicles = mytrain.GetVehicles(); //Makes up a list of the cars in this train, including the loco<br> |
| + | //Read the string table : | ||
int i; | int i; | ||
for(i=1; i<vehicles.size();i++) //Inspects each car of the train vehicle[0], vehicle[1],....vehicle[i] | for(i=1; i<vehicles.size();i++) //Inspects each car of the train vehicle[0], vehicle[1],....vehicle[i] | ||
{ //We assume that the loco is in front of train (i=0) ; so the first car should correspond to the i=1 vehicle. | { //We assume that the loco is in front of train (i=0) ; so the first car should correspond to the i=1 vehicle. | ||
| − | + | AsyncQueryHelper asyncqueryhelper = vehicles[i].GetAsset().CacheConfigSoup(); //Starts a query to cache the config file of the car[i] | |
asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the result (infinitesimal on a human scale) | asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the result (infinitesimal on a human scale) | ||
StringTable stringTable = vehicles[i].GetAsset().GetStringTableCached(); //Gets the cached string-table of the car[i] | StringTable stringTable = vehicles[i].GetAsset().GetStringTableCached(); //Gets the cached string-table of the car[i] | ||
| − | string Coupling = stringTable.GetString("coupler"); //In that string-table, read the line 'coupler' to get the associated string (e.g. 'animated' or 'not animated') | + | string Coupling = stringTable.GetString("coupler"); //In that string-table, read the line 'coupler' to get the associated string (e.g. 'animated' or 'not animated')<br> |
} | } | ||
| + | //Read the configtext : | ||
| + | int i; | ||
| + | for(i=1; i<vehicles.size();i++) //Inspects each car of the train vehicle[0], vehicle[1],....vehicle[i] | ||
| + | { //We assume that the loco is in front of train (i=0) ; so the first car should correspond to the i=1 vehicle. | ||
| + | AsyncQueryHelper asyncqueryhelper = vehicles[i].GetAsset().CacheConfigSoup(); //Starts a query to cache the config file of the car[i] | ||
| + | asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the result (infinitesimal on a human scale) | ||
| + | Soup VehicleconfigSoup = vehicles[i].GetAsset().GetConfigSoupCached(); //Gets the cached configtext as a Soup of the car[i] | ||
| + | string Script = VehicleconfigSoup.GetNamedTag("script"); //Read the tag 'script' in the configtext and return the name of this script | ||
| + | } | ||
} | } | ||
}; | }; | ||
Latest revision as of 19:48, 3 November 2025
This page is the continuation of the HowTo/Upgrade obsolete script functions
More precisely, it's about the new method to read the config file of an object, and thus its string table or another element of its configtext. Two cases are presented to us : the reading concerns the own string table or configtext of an object (in Trainz, an asset), or we want to read the same thing from an object into another object.
[edit] Reading its own string table or configtext.
Begin your script by the mandatory :
public void Init(Asset asset)
{
inherited(asset);
}
This method gets the asset subject of this script as a parameter. This guarantees that the config file is already available for reading.
Thus, and further on in the script, you only have to introduce :
StringTable MyStringTable = me.GetAsset().GetStringTableCached(); or Soup MyConfigSoup = me.GetAsset().GetConfigSoupCached()
Note that the function GetStringTableCached replaces the obsolete GetStringTable. The same with GetConfigSoupCached.
Here is a basic exemple.
Assume we have an object whith this string-table :
String-table
{
string0 "Hello"
}
The following script will result in throwing a exception message : 'Extract = Hello'.
include "MapObject.gs"
class MyObject isclass MapObject
{
void ReadStringTable (void);
public void Init(Asset asset)
{
inherited(asset);
ReadStringTable();
}
void ReadStringTable (void)
{
StringTable MyStringTable = me.GetAsset().GetStringTableCached();
string ExtractFromStringTable = MyStringTable.GetString("string0");
Exception("Extract = "+ExtractFromStringTable);
}
};
[edit] Reading the string table or configtext of another object
This time, the string-table or the configtext of the second object is not directly accessible. We have to introduce an additional step.
After the mandatory 'Init (Asset asset)....', we have to launch an asynchronous query to cache the config file of the second object. But don't worry, it's finally quite simple.
Here are the codes to implement :
AsyncQueryHelper asyncqueryhelper = SecondObject.GetAsset().CacheConfigSoup(); //Starts a query to cache the config file of the second object
asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the query to be completed
string ExtractFromStringTable=SecondObject.GetAsset().GetStringTableCached().GetString("string0"); //Reads a string 'string0' into the string table of the second object
Soup SecondObjectconfigSoup = SecondObject.GetAsset().GetConfigSoupCached().GetNamedTag("xxx"); //Reads the tag 'xxx' into the configtext of the second object
To note :
- 'SynchronouslyWaitForResults' function is usable only by script 'thread' functions
- the first object has to 'know' the second object as an asset (refer to HowTo/Search_for_objects_in_the_world in this wiki)
Here an exemple of implementation, with a loco that needs to read the string-table or the configtext of the cars of its train.
include "locomotive.gs"
class MyScript isclass Locomotive
{
thread void ReadConfigFile(void);
public void Init (Asset asset)
{
inherited(asset);
ReadConfigFile();
}
thread void ReadConfigFile(void)
{
Train mytrain = me.GetMyTrain(); //Gets the train to which the loco belongs
Vehicle[]vehicles = mytrain.GetVehicles(); //Makes up a list of the cars in this train, including the loco
//Read the string table :
int i;
for(i=1; i<vehicles.size();i++) //Inspects each car of the train vehicle[0], vehicle[1],....vehicle[i]
{ //We assume that the loco is in front of train (i=0) ; so the first car should correspond to the i=1 vehicle.
AsyncQueryHelper asyncqueryhelper = vehicles[i].GetAsset().CacheConfigSoup(); //Starts a query to cache the config file of the car[i]
asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the result (infinitesimal on a human scale)
StringTable stringTable = vehicles[i].GetAsset().GetStringTableCached(); //Gets the cached string-table of the car[i]
string Coupling = stringTable.GetString("coupler"); //In that string-table, read the line 'coupler' to get the associated string (e.g. 'animated' or 'not animated')
}
//Read the configtext :
int i;
for(i=1; i<vehicles.size();i++) //Inspects each car of the train vehicle[0], vehicle[1],....vehicle[i]
{ //We assume that the loco is in front of train (i=0) ; so the first car should correspond to the i=1 vehicle.
AsyncQueryHelper asyncqueryhelper = vehicles[i].GetAsset().CacheConfigSoup(); //Starts a query to cache the config file of the car[i]
asyncqueryhelper.SynchronouslyWaitForResults("AsyncResult"); //Waits for the result (infinitesimal on a human scale)
Soup VehicleconfigSoup = vehicles[i].GetAsset().GetConfigSoupCached(); //Gets the cached configtext as a Soup of the car[i]
string Script = VehicleconfigSoup.GetNamedTag("script"); //Read the tag 'script' in the configtext and return the name of this script
}
}
};