GdTasks 1.0.0-beta0071

Suggested Alternatives

GDTask

This is a prerelease version of GdTasks.
There is a newer version of this package available.
See the version list below for details.
dotnet add package GdTasks --version 1.0.0-beta0071
                    
NuGet\Install-Package GdTasks -Version 1.0.0-beta0071
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GdTasks" Version="1.0.0-beta0071" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GdTasks" Version="1.0.0-beta0071" />
                    
Directory.Packages.props
<PackageReference Include="GdTasks" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GdTasks --version 1.0.0-beta0071
                    
#r "nuget: GdTasks, 1.0.0-beta0071"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GdTasks@1.0.0-beta0071
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GdTasks&version=1.0.0-beta0071&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=GdTasks&version=1.0.0-beta0071&prerelease
                    
Install as a Cake Tool

GdTasks

Disclaimer

This is a fork of Fractural's original GDTask, created because I wasn't comfortable keeping a third-party project together all the time, so I wanted to wrap it in a NuGet package, but unfortunately I made a lot of weird changes

Breaking changes

  1. Namespaces:
    • Fractural.GDTask ⇒ GdTasks
  2. File Structure:
    • Splitting multi-class files into single-class files,
    • Organizing files into folders according to their logical meaning
  3. Changed the style and project
    • Primary constructors
    • Replace throw Exception("name") to ThrowIfNull(nameof(name))
    • Rename
  4. Added a test project to the repository

Description

Adds async/await features in Godot for easier async coding. Based on code from Cysharp's UniTask library for Unity.

Installation



Examples

using GdTasks;

public Test : Node 
{
	[Signal]
	public delegate void MySignalHandler(int number, bool boolean);
	
	public override _Ready() 
	{
		// Running a task from a non-async method.
		Run().Forget();
	}

	public async GdTaskVoid Run() 
	{
		await GdTask.DelayFrame(100);

		// Waiting some amount of time
		// Note that these delays are paused when the game is paused
		await GdTask.Delay(TimeSpan.FromSeconds(10));
		await GdTask.Delay(TimeSpan.FromSeconds(10), PlayerLoopTiming.Process);
		await GdTask.Delay(TimeSpan.FromSeconds(10), PlayerLoopTiming.PhysicsProcess);
		// Waiting some amount of milliseconds
		await GdTask.Delay(1000);
		// Waiting some amount of milliseconds, regardless of whether the game is paused
		await GdTask.Delay(TimeSpan.FromSeconds(10), PlayerLoopTiming.PauseProcess);
		await GdTask.Delay(TimeSpan.FromSeconds(10), PlayerLoopTiming.PausePhysicsProcess);

		// Awaiting for a signal
		WaitAndEmitMySignal(TimeSpan.FromSeconds(2)).Forget();
		var signalResults = await GdTask.ToSignal(this, nameof(MySignal));
		// signalResults = [10, true]

		// Cancellable awaiting a signal
		var cts = new CancellationTokenSource();
		WaitAndEmitMySignal(TimeSpan.FromSeconds(2)).Forget();
		WaitAndCancelToken(TimeSpan.FromSeconds(1), cts).Forget();
		try 
		{
			var signalResults = await GdTask.ToSignal(this, nameof(MySignal), cts.Token);
		}
		catch (OperationCanceledException _)
		{
			GD.Print("Awaiting MySignal cancelled!");
		}

		// Waiting a single frame
		await GdTask.Yield();
		await GdTask.NextFrame();
		await GdTask.WaitForEndOfFrame();

		// Waiting for specific lifetime call
		await GdTask.WaitForPhysicsProcess();

		// Cancellation of a GdTask
		var cts = new CancellationTokenSource();
		CancellableReallyLongTask(cts.Token).Forget();
		await GdTask.Delay(TimeSpan.FromSeconds(3));
		cts.Cancel();

		// Returning a value from a GdTask
		string result = await RunWithResult();
		return result + " with additional text";
	}

	public async GdTask<string> RunWithResult()
	{
		await GdTask.Delay(TimeSpan.FromSeconds(3));
		return "A result string";
	}

	public async GdTaskVoid ReallyLongTask(CancellationToken cancellationToken)
	{
		GD.Print("Starting long task.");
		await GdTask.Delay(TimeSpan.FromSeconds(1000000), cancellationToken: cancellationToken);
		GD.Print("Finished long task.");
	}
	
	public async GdTaskVoid WaitAndEmitMySignal(TimeSpan delay)
	{
		await GdTask.Delay(delay);
		EmitSignal(nameof(MySignal), 10, true);
	}

	public async GdTaskVoid WaitAndCancelToken(TimeSpan delay, CancellationTokenSource cts)
	{
		await GdTask.Delay(delay);
		cts.Cancel();
	}
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.