Check out our latest project ✨ OpenChapter.io: free ebooks the way its meant to be 📖

Async Resource Loader (C#)

An asset by ProFiLeR4100
The page banner background of a mountain and forest
Async Resource Loader (C#) hero image

Quick Information

0 ratings
Async Resource Loader (C#) icon image
ProFiLeR4100
Async Resource Loader (C#)

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

Supported Engine Version
4.2
Version String
1.0
License Version
MIT
Support Level
community
Modified Date
1 year ago
Git URL
Issue URL

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();
    }
}

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

Reviews

0 ratings

Your Rating

Headline must be at least 3 characters but not more than 50
Review must be at least 5 characters but not more than 500
Please sign in to add a review

Quick Information

0 ratings
Async Resource Loader (C#) icon image
ProFiLeR4100
Async Resource Loader (C#)

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

Supported Engine Version
4.2
Version String
1.0
License Version
MIT
Support Level
community
Modified Date
1 year ago
Git URL
Issue URL

Open Source

Released under the AGPLv3 license

Plug and Play

Browse assets directly from Godot

Community Driven

Created by developers for developers