View Full Version : TrainzAssetSearch Obsoletions

October 26th, 2015, 08:55 AM
I noticed from compilation warnings that you have obsoleted all of the methods in TrainzAssetSearch and replaced them with asynchronous alternatives. The alternatives are welcome since using the functions can sometimes introduce short delays, but I'm having trouble with one particular application of SearchAssetsSorted().

This is the code involved, its part of a library function which tries to find a list of texture libraries. (remember that in your infinite wisdom you no longer provide a straightforward mechanism to search for 'kind texture-group' :))

// a click on the texture link will call this code
if (function == "GetPropertyElementList") {
string pID = Strings[0];
int i, j;
Strings[0,Strings.size()] = null;

if (pID[0,8] == "texture-") {
kuidlist[0,kuidlist.size()] = null;
int[] keys = new int[4];
string[] values = new string[4];
keys[0] = TrainzAssetSearch.FILTER_KEYWORD; values[0] = "texture;library";
keys[1] = TrainzAssetSearch.FILTER_LOCATION; values[1] = "local";
keys[2] = TrainzAssetSearch.FILTER_AND;
keys[3] = TrainzAssetSearch.FILTER_VALID; values[3] = "true";
SearchResult = TrainzAssetSearch.SearchAssetsSorted(keys,values,T rainzAssetSearch.SORT_NAME,true);
for (i = 0; i < SearchResult.size(); i++) {
Strings[Strings.size()] = SearchResult[i].GetLocalisedName();
kuidlist[kuidlist.size()] = SearchResult[i].GetKUID();
return "";
// immediately after this returns the game will expect the Strings array to be populated and will show the list in an edit box for user selection
// I have no control over this mechanism.

Since this operates in response to a property object call from native code I don't see how the asynchronous mechanism can be made to work without the risk of exceptions if it doesn't return immediately - the code works well enough without this risk at present. How can this be made to work with the corresponding asynchronous method?

October 26th, 2015, 09:46 AM
You need to ensure that any results you want are available by the time the user triggers that function call. There are two reasonable places you could start the query to aim for this result:

* You could start the query when the properties editor is opened. If the query doesn't finish by the time the results are needed, you'll have to display an empty list or similar.

* You could start the query from a library when the library is loaded. If the query doesn't finish by the time the results are needed, you'll either have to display an empty list, or else show a message in your property editing UI instead of the link in question.

Neither one will trigger a script exception; that only happens if you make a mistake in your code.

The search functions will typically take no more than a second or so, but it's feasible that they could take minutes or longer to complete under some circumstances. Even if it isn't really necessary in your own testing, you will need to ensure that you correctly handle the case where they have not completed.


October 26th, 2015, 03:28 PM
OK that all makes sense.