Class AsyncObjectSearchResult
From TrainzOnline
(Difference between revisions)
m (→Code Examples: Correct example to wait for AsyncResult, not AsyncLoadComplete) |
(→GetResults) |
||
| (One intermediate revision by one user not shown) | |||
| Line 36: | Line 36: | ||
;Syntax | ;Syntax | ||
NamedObjectInfo[] results = searchObj.GetResults(); | NamedObjectInfo[] results = searchObj.GetResults(); | ||
| − | for (int i = 0; i < results.size(); ++i) { | + | 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 | ;Notes | ||
| Line 58: | Line 59: | ||
*Error code for the query | *Error code for the query | ||
;Syntax | ;Syntax | ||
| − | wait() { | + | wait() |
| + | { | ||
on "ObjectSearch", "Failure", msg: | on "ObjectSearch", "Failure", msg: | ||
| − | if (msg.src | + | if (msg.src != searchObj) |
| − | Interface.Log("Search error " + searchObj.GetSearchErrorCode(); | + | continue; |
| − | + | Interface.Log("Search error " + searchObj.GetSearchErrorCode(); | |
| + | break; | ||
} | } | ||
;Notes | ;Notes | ||
Latest revision as of 13:09, 3 March 2022
- 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 |
[edit] 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(). |
[edit] Methods
[edit] 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.
[edit] GetQueryErrorCode
public native int GetQueryErrorCode(void)
[edit] 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)
continue;
Interface.Log("Search error " + searchObj.GetSearchErrorCode();
break;
}
- 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.
[edit] 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", "AsyncResult", 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
}
[edit] Related Methods