Skip to content
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

Intersect ray hit RID is different each time? #36824

Closed
Tracked by #45334 ...
gitcatrat opened this issue Mar 5, 2020 · 8 comments
Closed
Tracked by #45334 ...

Intersect ray hit RID is different each time? #36824

gitcatrat opened this issue Mar 5, 2020 · 8 comments

Comments

@gitcatrat
Copy link

gitcatrat commented Mar 5, 2020

Godot version:
3.2.stable.mono

OS/device including version:
macOS Catalina 10.15.3/MacBook Pro

Issue description:
DirectSpaceState.IntersectRay hit object RID is different each time, even if same hit body.

I didn't even notice this before because I used nodes and collider (and its Name) to identify the hit target but now that I've moved some of my stuff to use Servers API directly, I have no way to identify the hit target if RID is constantly changing.

Maybe it's supposed to change? I have no idea, couldn't find any information about it. I was going to use RID as a dictionary key to get my data instance but this condition fails:

public static Dictionary<RID, Enemy> enemyRids;
// ...
if (enemyRids.ContainsKey(hitRID)) { /* ... */ }

Steps to reproduce:
Sorry for C#, I've used GDScript only few times.

public override void _PhysicsProcess(float delta) {
  if (doRayCast) {
    var spaceState = GetWorld().DirectSpaceState;
    var hit = spaceState.IntersectRay(from, to);
    Debug.Log(hit["rid"]); // different each time, even if same hit body
    doRayCast = false;
  }
}

Reproduction project:
issue.zip

Screenshot of RIDs (Newtonsoft JSON serializer) when ray-casting same body:

wtf

@KoBeWi
Copy link
Member

KoBeWi commented Mar 5, 2020

I tried with GDScript and the RID is constant.

Any reproduction project for this?

@gitcatrat
Copy link
Author

Was about to make one but then I stumbled on this #35897..

@gitcatrat
Copy link
Author

@KoBeWi Managed to solve previously mentioned issue by running NuGet restore.

Project: issue.zip
Blue (left) cube is node-based, red (right) cube is added with Servers. Both have the issue.

@KoBeWi
Copy link
Member

KoBeWi commented Mar 7, 2020

Sooo, when you change the debug line to
Debug.Log((hit["rid"] as RID).GetId());
you get consistent IDs. So the RID is indeed not changing, but when you print it directly it might be printing some instance data (each RID might be unique object even if they have the same ID).

No idea what is the "NativeInstance" stuff, but otherwise seems like it's working correctly.

@gitcatrat
Copy link
Author

Okay, so the bottom line is that RID comparison is not reliable and it can't be used as a key (in dictionary for example). But at least there's a way to get the ID that can be used in these situations.

Thanks, I'll make a quick test in my project to see if it solves my issue.

@KoBeWi
Copy link
Member

KoBeWi commented Mar 7, 2020

Okay, so the bottom line is that RID comparison is not reliable and it can't be used as a key (in dictionary for example).

This is weird actually, because RID is a built-in type, so they should compare properly. It is possible that it's only C# issue, this probably requires some more testing.

@gitcatrat
Copy link
Author

@KoBeWi Fixed in Godot 4.0 beta 17, I have no idea which version fixed it.
I'm not going to close this myself in case you have some other plans.

@akien-mga akien-mga added this to the 4.0 milestone Feb 7, 2023
@akien-mga
Copy link
Member

Fixed in 4.0.

@akien-mga akien-mga added the bug label Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants