Is it currently possible to set up multi-threading using blueprints alone? Long story short, I would like to shift my auto-saving system on to its own thread to prevent the hitch that it currently causes. I know the amazing Rama wrote a guide explaining how to do it via code, but I’m curious if there is a blueprint alternative at the moment, or if there are plans for one in the near future.
Interesting. So simply adding a Delay node but with a time of “0” causes what follows it to run async? I’ll have to give it a try! Thanks for the help, much appreciated
Interesting - thanks for the reply I’m not quite sure how or why this would make any difference, but you’ve definitely given me another avenue to research! And at the perfect time, too - I’m planning to have another stab at tackling this problem again soon.
At the moment, I’m not seeing how this would be any different from using a Sequence to fire a series of custom events, but I assume it has something to do with timelines having some in-built feature that automatically handles multithreading?
With this method, I’m guessing I’d just need to chop all the processes into smaller batches, so they individually don’t cause a hitch, then stagger them as necessary? Meh, I still don’t understand how that’s any different from firing staggered custom events, but I’ll do some more research!
Yes it’s not multi-threading but it’s parallel. After the first branch starts, the sequence branch proceeds to execute the next sequence pin (doesn’t matter if the first one completed execution or not). That’s to say it’s a multitasking trick (context switching) takes place to give the illusion that they’re operating at the same time. For cascaded nodes, it’s a different story, you can’t start the next node until the current one finishes. So there’s no way two nodes connected in cascade structure are the same as what a sequence node does. TLDR: it’s not one after another
also you can have a blueprint function library with a function that calls Async or AsyncTask and receives 2 delegates. executes 1 for inside the async task, and another one when it finishes (probably wrapped in another AysncTask for the game thread).
you can then pass some events to those delegates on bp.
Multithreading is not a goal, it’s a process.
Not what it is for does not apply.
I will definitely have a look at making a node that does this once I get comfy with the libraries.
i just replied to that question. why dont they make it do something else? because that’s not what that node is for. if you want that functionality you should implement something else.
for example:
this code is lgpl3-only copyright me
// Copyright (C) 2023 - Jeronimo Barraco-Marmol. All rights reserved.
// SPDX-License-Identifier: LGPL-3.0-only
#pragma once
// excerpt of my own library
#include "CoreMinimal.h"
class UWorld;
class UWidget;
#include "JMiscUtils.generated.h"
DECLARE_DYNAMIC_DELEGATE(FOnJAsync);
DECLARE_DYNAMIC_DELEGATE(FOnJAsyncDone);
// Type of graph to use
UENUM(BlueprintType)
enum class EAsyncExec: uint8
// Engine\\Source\\Runtime\Core\\Public\Async\Async.h:27
// the type used on AsyncTask is much more granular. but unfortunately it´s not BlueprintType and is int32 so it cant be exposed
{
/** Execute in Task Graph (for short running tasks). */
TaskGraph,
/** Execute in Task Graph on the main thread (for short running tasks). */
TaskGraphMainThread,
/** Execute in separate thread if supported (for long running tasks). */
Thread,
/** Execute in separate thread if supported or supported post fork (see FForkProcessHelper::CreateThreadIfForkSafe) (for long running tasks). */
ThreadIfForkSafe,
/** Execute in global queued thread pool. */
ThreadPool,
};
UCLASS(Blueprintable)
class JUTILS_API UJMiscUtils: public UBlueprintFunctionLibrary {
GENERATED_BODY()
public:
// Calls a Task (a Delegate) on another thread, when finishes calls OnDone on the game thread (if bound)
// What Not to Do:
// * Do not try to modify, create, or delete UObjects from other threads!
// * You can prepare all the data / do all the calculations, but only the game thread should be actually spawning / modifying / deleting UObjects / AActors.
// * Dont try to use TimerManager outside of the game thread :)
// * Don´t try to draw debug lines/points etc, as it will likely crash, ie DrawDebugLine(etc...)
UFUNCTION(BlueprintCallable)
static void BPASync(const FOnJAsync& Task, const FOnJAsyncDone& Done, EAsyncExec Exec = EAsyncExec::ThreadPool);
}
/// on the cpp
void UJMiscUtils::BPASync(const FOnJAsync& Task, const FOnJAsyncDone& Done, EAsyncExec Exec) {
// using [Task, Done] instead of & since it's much safer. re-entrant calls could crash otherwise.
Async((EAsyncExecution) Exec, [Task, Done]
{
Task.ExecuteIfBound();
if (Done.IsBound()) {
AsyncTask(ENamedThreads::GameThread, [Done]
{
Done.ExecuteIfBound();
});
}
});
}