Class AsyncObjectSearchResult
From TrainzOnline
(Difference between revisions)
m (Add GetQueryErrorCode) |
m (→Related Messages: Fix table formatting) |
||
| Line 15: | Line 15: | ||
{{TableHeader|width=50%|margin=15px}} | {{TableHeader|width=50%|margin=15px}} | ||
|- | |- | ||
| − | |bgcolor="#EEEEEE"|'''Major'''||bgcolor="#EEEEEE"|'''Minor'''||bgcolor="#EEEEEE"|'''Source'''||bgcolor="#EEEEEE" |'''Destination'''|bgcolor="#EEEEEE"|'''Description''' | + | |bgcolor="#EEEEEE"|'''Major'''||bgcolor="#EEEEEE"|'''Minor'''||bgcolor="#EEEEEE"|'''Source'''||bgcolor="#EEEEEE" |'''Destination'''||bgcolor="#EEEEEE"|'''Description''' |
|- | |- | ||
|ObjectSearch||AsyncResult||AsyncObjectSearchResult||Broadcast||Search is complete, and [[#GetResults|GetResults]] may be called. | |ObjectSearch||AsyncResult||AsyncObjectSearchResult||Broadcast||Search is complete, and [[#GetResults|GetResults]] may be called. | ||
Revision as of 05:43, 29 December 2020
- API Hierarchy
- GSObject *
- GameObject
- AsyncQueryHelper
- AsyncObjectSearchResult
- AsyncQueryHelper
- GameObject
- GSObject *
- An AsyncObjectSearchResult represents an asynchronous search of objects in the game world.
- HowTo/Search for objects in the world provides a detailed tutorial on asynchronous searches.
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)
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