Returning a reference from a Thread Task
I'm multi-threading our AI to choose the closest actor. To do this I'm trying to use a task on the task graph since there can be hundreds of units doing this at once.
I created a class to house all the information for the thread. It looks like this.
My testing shows that I can't use references at all because if the unit dies and the thread continues, I get null references. All I need to do is set 2 blackboard values.
But because the AIController owning the thread is destroyed, The thread crashes the game. Where should I be storing variables that makes them unique to the querying unit but owned by the querying unit.
asked Oct 18 '16 at 07:27 PM in C++ Programming
In UE4 all the gameplay code (including AI) is expected to live on the GameThread. You'll get yourself into a lot of trouble if you try calling functions on gamethread-bound objects outside of gamethread (unless you really, relly know what you're doing).
Ideally all the computation you'd like to do outside of game-thread should be abstracted from any UObject code. You should gather all the information you'd might want to ask off-gamethread, pack it into your tasks, and have tasks operate on that data, without any need to ask for additional information. This has lots of benefits (apart from being thread-safe) - it's a lot more efficient to ask actor X for a location, store it and reuse it than have every separate task ask X for its location (even if it was possible).
Alternatively you can create a gamethread-side model of the world and use thread shared pointers (`TSharedPtr< FYourType, ESPMode::ThreadSafe>`) or references (`TSharedRef< FYourType, ESPMode::ThreadSafe>`) to access it from threads - again, note that
answered Oct 19 '16 at 12:44 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here