Talk:HowTo/Search for objects in the world
From TrainzOnline
< Talk:HowTo(Difference between revisions)
Kcrowder13 (Talk | contribs) (→Correct Async Object Search Sample Code: new section) |
|||
Line 9: | Line 9: | ||
(2024-04-11 10-18 ek.skirl) | (2024-04-11 10-18 ek.skirl) | ||
+ | |||
+ | == Correct Async Object Search Sample Code == | ||
+ | |||
+ | thread void TrainSearch(void) | ||
+ | { | ||
+ | // prevent multiple threads | ||
+ | if(m_bIsThreadedFunctiionRunning) return; | ||
+ | |||
+ | // 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) | ||
+ | continue; | ||
+ | Interface.Log("Search error " + searchObj.GetSearchErrorCode()); | ||
+ | break; | ||
+ | on "ObjectSearch", "AsyncLoadComplete", msg: | ||
+ | if (msg.src == searchObj) | ||
+ | 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. | ||
+ | Interface.Log("WaybillTracker:: ERROR during async object search"); | ||
+ | m_bIsThreadedFunctionRunning = false; | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // Get the search results | ||
+ | NamedObjectInfo[] results = searchObj.GetResults(); | ||
+ | |||
+ | // TODO: Add any processing of the results here | ||
+ | string successmsg = "WaybillTracker:: Size of results of async object search" + results[0].localisedUsername; | ||
+ | |||
+ | Interface.Log(successmsg); | ||
+ | m_bIsThreadedFunctionRunning = false; | ||
+ | } | ||
+ | |||
+ | NamedObjectInfo[] contains 4 properties: | ||
+ | GameObject objectRef; Holds reference to object 'IF' loaded. Reference may go out of scope and be lost, for long term storge GameObjectId should be used. A call to Router.DoesGameObjectStillExist() should be called before accessing the GameObject reference. | ||
+ | GameObjectId objectId; Currently the safest to use for long term storage of objects. May be subject to change in the future. | ||
+ | string localisedUsername; Localized display name for the object. | ||
+ | string categoryString; The asset category of the current object. |
Latest revision as of 20:08, 21 May 2025
Script error in lines 12, 17, 23: The pointer variable asyncSearch has to be searchObj.
Or vice versa in lines 3, 6, 31: The pointer variable searchObj has to be asyncSearch
P.S. For some reason I don't know yet, I'm no more able to edit the main page? For other pages I#m able. Is there some reason I don't know why?
(2024-04-11 10-18 ek.skirl)
[edit] Correct Async Object Search Sample Code
thread void TrainSearch(void) { // prevent multiple threads if(m_bIsThreadedFunctiionRunning) return;
// 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) continue; Interface.Log("Search error " + searchObj.GetSearchErrorCode()); break; on "ObjectSearch", "AsyncLoadComplete", msg: if (msg.src == searchObj) 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. Interface.Log("WaybillTracker:: ERROR during async object search"); m_bIsThreadedFunctionRunning = false; return; } // Get the search results NamedObjectInfo[] results = searchObj.GetResults(); // TODO: Add any processing of the results here string successmsg = "WaybillTracker:: Size of results of async object search" + results[0].localisedUsername;
Interface.Log(successmsg); m_bIsThreadedFunctionRunning = false; }
NamedObjectInfo[] contains 4 properties: GameObject objectRef; Holds reference to object 'IF' loaded. Reference may go out of scope and be lost, for long term storge GameObjectId should be used. A call to Router.DoesGameObjectStillExist() should be called before accessing the GameObject reference. GameObjectId objectId; Currently the safest to use for long term storage of objects. May be subject to change in the future. string localisedUsername; Localized display name for the object. string categoryString; The asset category of the current object.