-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add async behavior to functions of Godot classes in C# #3527
Comments
TL;DR: You can already add the This code is not valid, it does not compile because you are using public override void _GuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseButton)
{
unitToMove.MoveUnitToPosition(mouseButton.Position);
await ToSignal(unitToMove, "MovementStopedSignal"); //Here an await of some sort happens ! Hurray!
Debug.WriteLine($"Movement completed!");
}
} You can already add the public override async void _GuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseButton)
{
Task unitMovementTaskResult = unitToMove.MoveUnitToPosition(mouseButton.Position);
await unitMovementTaskResult;
}
} This means the method will be awaited before the next line of code is executed. But Godot won't await the I'd like to know what is it that you really want to do, if your intention is to ignore all input while the units are moving you can just do something like this: bool inputDisabled = false;
public override async void _GuiInput(InputEvent @event)
{
if (inputDisabled) return;
if (@event is InputEventMouseButton mouseButton)
{
inputDisabled = true;
Task unitMovementTaskResult = unitToMove.MoveUnitToPosition(mouseButton.Position);
await unitMovementTaskResult;
inputDisabled = false;
}
} |
I thought the
You are right again, I wrote this out of my memory and of course thats invalid code. In reality I dont wait in Thanks a lot for help again, I owe you a beer ! |
Closing, as this is already feasible as demonstrated above. |
Describe the project you are working on
Strategy game
Describe the problem or limitation you are having in your project
I have a an
Area2D
acting as one unit.Unit moves towards targetPosition like so (this is from godot best practices):
I have a Control as my UI control. There, in
public override void _GuiInput(InputEvent @event)
a mouse click can detect where do we want to move the unit to.I made a simple async Task so I can call
Unit.MoveTo(targetPos);
So in my UI Control I basically have:
The problem is, right now we CANNOT await until an async Task is complete. This is because
**_GuiInput**
is NOT an async function. So the task execution WILL happen but the code cannot await for it.I could write:
but this hangs and waits indefinately.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
It would be great if godot functions could be marked/changed to async.
So instead of
public override void _GuiInput(InputEvent @event)
we would have:
public override async void _GuiInput(InputEvent @event)
Async helps integrate async C# Tasks and it would be usefull for tens od thousands of C# programmers using them.
And people who want to continue to use the functions in an (existing) sync dont have to do anything and can continue to use it like so.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
It would be great if godot functions could be marked/changed to async.
So instead of
public override void _GuiInput(InputEvent @event)
public override void _Input(InputEvent @event)
we would have:
public override async void _GuiInput(InputEvent @event)
public override async void _Input(InputEvent @event)
If this enhancement will not be used often, can it be worked around with a few lines of script?
The nearest work around right now is to use signals
So right now I am using the following.
Disadvantage of this approach is that I am forced to work with Signals and the same thing could be done much more elegantly using C# tasks.
So this would be ideal instead:
Is there a reason why this should be core and not an add-on in the asset library?
This is cannot be an addon since it changes the core godot functions.
The text was updated successfully, but these errors were encountered: