Skip to content

Allows to use standard C# async/await to load resources for Godot engine.

License

Notifications You must be signed in to change notification settings

ProFiLeR4100/AsyncResourceLoaderForCS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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<AudioStream>("res://sound.mp3");

If you have to override Engine.MainLoop, then you must provide your SceneTree.

await AsyncResourceLoader.LoadResource<AudioStream>(
    "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<AudioStream>(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();
    }
}

About

Allows to use standard C# async/await to load resources for Godot engine.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages