C# - errors when asynchronously loading resources

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By _bjork

I’ve been following the tutorial on changing scenes asynchronally (as seen here) and translating it to C#, however, the following errors occur:

  1. E 0:00:01.799 get_tree: Condition “!data.tree” is true. Returned: nullptr
  2. E 0:00:01.802 connect_signal_awaiter: Parameter “p_source” is null.

When yielding, I run

 await (ToSignal(GetTree(), "idle_frame"));

as seen here
however the errors above happen right at that moment. What am I missing to properly load resources/scenes asynchronously?

Code:

   private async void ChangeSceneAsync(string path)
{
    ResourceInteractiveLoader ril = ResourceLoader.LoadInteractive(path);

    while (true)
    {
        Error err = ril.Poll();
        if (err == Error.FileEof)
        {
            Resource res = ril.GetResource();
            break;
        }
        if (err == Error.Ok)
        {
            float progress = (float)(ril.GetStage() / ril.GetStageCount());
            GD.Print(progress);
        }

        await (ToSignal(GetTree(), "idle_frame")); // Errors occur here
    }
}
:bust_in_silhouette: Reply From: kelaia

I got this error once and the reason was that the node (where the script is running the GetTree()) was not inside the tree. You need to make this node a child of another. Another reason might be that you are using the constructor of this class instead of using the _Ready method.

This was it. I had this function in an autoloaded Global.cs script thats why it couldnt find the tree.

I moved it to a node in a simple scene to test it quickly and now it loaded my resource properly.

_bjork | 2022-02-02 23:57