in godot 4.3 , c# .
i want to update gui or node3d object position inside Task.run.
end goal is to animate object. in circle path.
it also needs to be in the code.
using gui is not an option for me..
it just complicates...
expected animation duration:
3.6 seconds => 50ms * 360degree/5step ;
actual behavior:
When I run this code, UI doesn't do anything. it freezes for a while...
code
public partial class Scene1 : Node3D
{
public override void _Ready(){};
public override void _Process(double delta){};
void Button_Move_in_Circle_onClick() => MoveCube_inCirclularPath();
private void MoveCube_inCirclularPath()
{
Task.Run( () =>
{
//cube y is Height, move it to 2meter. above ground;
InvokeHelper.CallDeferred(() =>{
cube.position.x = 2;
});
for (int i = 0; i < 360; i = i +5)
{
Thread.Sleep(50);
var pt = GetCircleCoordinates_FromAngle(i, 4);
InvokeHelper.CallDeferred(() =>{
cube.position.x = pt.x;
cube.position.z = pt.y;
});
}
});
}
public static (double x, double y) GetCircleCoordinates_FromAngle(double angleDegrees, double distance)
{
double angleRadians = angleDegrees * Math.PI / 180.0;
double x = distance * Math.Cos(angleRadians);
double y = distance * Math.Sin(angleRadians);
return (x, y);
}
}
InvokeHelper.cs
using Godot;
using System;
public static class InvokeHelper
{
/// <summary>
/// in winforms equivalent is *this.Invoke( delegate { } )*
/// </summary>
public static void CallDeferred(this Action action)
{
Callable.From(action).CallDeferred();
}
}