
Publisher
profiler4100
Async Resource Loader (C#)
Allows to use standard C# async/await to load resources.
This plugin has been mirrored from the Godot Asset Library.
The plugin author is in no way affiliated with Gadget.
If you are the author of this plugin and would like this mirror removed, please contact support@gadgetgodot.com.
Async Resource Loader (C#)
Allows to use standard C# async/await to load resources for Godot engine.
Supports only Godot 4.0+
How to install
Download the project and copy the addon folder into your godot project.
Go to Project Settings > Plugins, and enable Async Resource Loader (C#).
How to use
Add import of namespace at the beginning of C# file.
using AsyncResourceLoaderRuntime;
Load resource that you want.
await AsyncResourceLoader.LoadResource("res://sound.mp3");
If you have to override Engine.MainLoop, then you must provide your SceneTree.
await AsyncResourceLoader.LoadResource(
"res://sound.mp3",
GetTree()
);
Other parameters are the same as on original ResourceLoader.LoadThreadedRequest(path, typeHint, useSubThreads, cacheMode)
.
Please check official Engine manual for these params explanation.
Example
using System.Threading.Tasks;
using Godot;
using AsyncResourceLoaderRuntime;
public partial class ExampleClass: Node {
// I've put a _Ready method just as a way to trigger async task.
public override void _Ready() {
base._Ready();
PlayAudio("res://sound.mp3");
}
private async Task PlayAudio(string pathToAudioFile) {
// Create new Audio Stream Player
AudioStreamPlayer streamPlayer = new();
// Asynchronously loading and assigning audio stream
streamPlayer.Stream = await AsyncResourceLoader.LoadResource(pathToAudioFile);
// Add Audio Stream Player to the tree
CallDeferred(Node.MethodName.AddChild, streamPlayer);
// Call "Play" method on player
streamPlayer.CallDeferred(AudioStreamPlayer.MethodName.Play);
// Awaits player to finish playing
await streamPlayer.ToSignal(
streamPlayer,
AudioStreamPlayer.SignalName.Finished
);
// Safely queues stream player to free mamory.
streamPlayer.QueueFree();
}
}