Hello, I have a custom PCG node in c++ that I want to run async, I overrode the “CanExecuteOnlyOnMainThread” and made it return false, the problem is that sometimes the node executes in the game thread instead of another thread, I think if that the pcg ms is still under the budget(we set it to 2ms), it will run on the gamethread, and it will only run on another thread if it is over the budget, is there some way to force the node to always run in another thread?
My custom pcg node sometimes can take a little bit of time to finish, which would cause it to spike, also even if it didnt spike, I dont think it makes sense to use the gamethread and make the game run slower in a node that can run in the background
[Attachment Removed]
Steps to Reproduce
Create a new PCG node in c++ and override the function “virtual bool CanExecuteOnlyOnMainThread(FPCGContext* Context) const override;”
Make it return false so it can execute in other threads
[Attachment Removed]
Hi Victor,
We don’t currently have support for your use case, namely for force something not to run on the game thread - mainly because we had not run into this yet, and there are a bit more edge cases there (namely if you’re on a system with a low number of threads, etc.)
We’ll talk about it and how easily feasible this is in a future release, but in the mean time, there might be a few workarounds for you:
(1) - make sure to implement your custom C++ node with time-slicing support.
Basically, if you return ‘false’ in the ExecuteInternal function, it means that the execute isn’t done, and will be rescheduled again. There are a lot of examples in the code base so I’m sure you’ll be able to pick it up quickly.
(2) - you could spin a task if and only if you’re called from the game thread
Basically, you can spin your processing task through a normal task call (outside of PCG), and make your current node context ‘paused’ until that task completes. That would guarantee you’re not running it on the game thread, since you’d have started the task on the game thread.
Not perfect solutions, but hopefully that’s useful to you.
Cheers,
Julien
[Attachment Removed]
I see, the second thing that you mentioned is what I’ve been doing, but sometimes I have a crash because I need to access the context in the async task, but the context is getting destroyed when the pcg executing gets cancelled(its a runtime pcg), I added a wait in the destructor of the context and seemed to mostly fix it, but in some very rare cases it still crashes, is there some way to make the context stay alive while the pcg node is paused?
[Attachment Removed]
Hi again Victor,
In this case, the solution is most likely for you to implement/override the ‘Abort’ function on the IPCGElement, which will allow you to properly close your task (with a state variable + wait or event or else, depending on your implementation). Basically, this method will be called on every task that’s currently running prior to being cancelled, so at that point in time, the context will always be in a viable state.
Let me know if that works or not!
Cheers,
Julien
[Attachment Removed]