Can you connect a signal to async function in c#?

Godot Version

4.5

Question

Is it possible to connect a signal in Godot to an async function in C#?
I’m new to Godot, but from what i have tried, my connected method is only called if it is a synchronus void function.

The code below is what I’m currently doing. OnAttackButtonPressed() is connected to a signal and OnAttackButtonPressedAsync() contains the async code i actually want to execute, when recieving ths signal.

public void OnAttackButtonPressed()
{
  OnAttackButtonPressedAsync();
}

public async Task OnAttackButtonPressedAsync()
{
  await AttackEnemy();
  await EnemyTurn();
}

Functionally, this works fine, I just find it a bit silly that i need two functions for this. Any recommendation for handling this differently is greatly appreciated :smile:

I’m new to Godot as well, so it might not be correct. It’s not the async keyword that is causing the problem, it’s the return type Task not being a variant. So you can use private async void DoSomething(), but you probably do not want to do that.

1 Like

Thanks for the reply!
I tested out what you wrote and it’s correct that it is the return type Task that causes an error. Not the function being async. But you are also correct that I want to avoid using async void DoSomething(), so I’m gonna stick to my current solution for now :smile:

Hey ! In C#, always avoid to call a ‘async void’. because this is going to run in an another thread and if your logic fails, the error will be silent. You’ll never know what happened.

1 Like