Class AsyncObjectSearchResult

From TrainzOnline
Revision as of 05:43, 29 December 2020 by Danny252 (Talk | contribs)

Jump to: navigation, search



Contents

Related Messages

  • Messages sent to and from TrackMark objects are listed below:


Major Minor Source Destination Description
ObjectSearch AsyncResult AsyncObjectSearchResult Broadcast Search is complete, and GetResults may be called.
ObjectSearch AsyncLoadComplete AsyncObjectSearchResult Broadcast Loading of search result has been completed. See World.GetGameObjectByID().
ObjectSearch Failure AsyncObjectSearchResult Broadcast Search has failed, call GetSearchErrorCode for more information.
ObjectSearch Expired AsyncObjectSearchResult Broadcast New search results are available, and this result has expired. Not posted by default - see World.GetNamedObjectList().

Methods


GetResults

public native NamedObjectInfo[] GetResults(void)
Parameters
  • None
Returned Value
  • Array of search results
Syntax
NamedObjectInfo[] results = searchObj.GetResults();
for (int i = 0; i < results.size(); ++i) {
    NamedObjectInfo result = results[i];
    Interface.Log("Result " + i + ": '" + result.localisedUsername + "'");
    // Process each result in some useful way...
}
Notes
  • The caller must wait for the search to complete, indicated by the ObjectSearch/AsyncResult message.
  • If the search is not complete, calling this method will result in an exception.


GetQueryErrorCode

public native int GetQueryErrorCode(void)

See GetSearchErrorCode

GetSearchErrorCode

public native int GetSearchErrorCode(void)
Parameters
  • None
Returned Value
  • Error code for the query
Syntax
wait() {
  on "ObjectSearch", "Failure", msg:
    if (msg.src == searchObj) {
      Interface.Log("Search error " + searchObj.GetSearchErrorCode();
    }
}
Notes
  • When a ObjectSearch/Failure message is posted, this method will return an error code.
  • Error codes are defined in the ERROR_ constants for AsyncQueryHelper.


Code Examples


thread void SearchForTrains()
{
  // Start a search for any traincars within the world
  AsyncObjectSearchResult searchObj = World.GetNamedObjectList(AssetCategory.TrainCar, "");
  
  // Sniff for search related messages, and then wait for either completion or failure
  Sniff(searchObj, "ObjectSearch", "", true);
  Message msg;
  wait()
  {
    on "ObjectSearch", "Failure", msg:
      if (msg.src == searchObj)
        break;
      continue;
    
    on "ObjectSearch", "AsyncLoadComplete", msg:
      if (msg.src == searchObj)
        break;
      continue;
  };
  
  // Check the results
  int errCode = searchObj.GetSearchErrorCode();
  if (errCode != AsyncObjectSearchResult.ERROR_NONE)
  {
    // TODO: Add any error handling here, such as waiting and reattempting the
    // search later, showing an error message, throwing script exceptions, etc.
    return;
  }
  
  // Get the search results
  NamedObjectInfo[] results = searchObj.GetResults();
  
  // TODO: Add any processing of the results here
}

Related Methods



Categories

Personal tools