-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
C# Getting Node3D Position/GlobalPosition in Thread will always return zero #79164
Comments
Accessing node members outside of the main thread is not allowed. The Lines 508 to 511 in c3b0a92
You should be getting this error:
Take a look at the Thread-safe APIs documentation for more information. You can follow the error's suggestion and use using Godot;
using System;
using System.Threading.Tasks;
public partial class Demonstration : MeshInstance3D
{
public override void _Ready()
{
var currentPosition = this.Position;
GD.Print(currentPosition); // will print the correct position
Task.Run(async() =>
{
await Task.Delay(100);
Callable.From(() =>
{
var currentPositionInThread = this.Position;
GD.Print(currentPositionInThread); // will print the correct position
}).CallDeferred();
});
}
} |
Thanks for clarification. |
Yes, you should also be able to await the await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); The continuation will execute in the main thread. |
Also consider if the public override async void _Ready()
{
var currentPosition = this.Position;
GD.Print(currentPosition); // will print the correct position
await Task.Delay(100);
var currentPositionAfterWait = this.Position;
GD.Print(currentPositionAfterWait); // will print the correct position
} |
Would this not block the whole game? Considering you are awaiting on the main thread? |
That is the whole point of async, |
Alright thanks for your suggestions @raulsntos & @RedworkDE |
Godot version
Godot 4.1 stable
System information
Windows 10, RTX2080, i9-9900K
Issue description
Trying to access the position with node3D.Position or node3D.GlobalPosition inside a thread will always return Vector3(0,0,0) in 4.1
Worked without any issue in 4.0
Steps to reproduce
Add a Node to your scene and attach a script to it like this: (same code in mrp)
If step into this code you will see that the information of the position will be lost as soon as you enter the Task.Run() block
Minimal reproduction project
GetPositionIssue.zip
The text was updated successfully, but these errors were encountered: