Home > Net >  How to Properly Check Dictionary Key c#
How to Properly Check Dictionary Key c#

Time:08-13

So I am doing some Ray-Casting in the _PhysicsProcess method and using a for loop to instance a new ray-cast every time the previous ray-cast hits something. This allows the user to "see" through objects. To check that this is all working, I am printing the position value, but I don't seem to be getting anything.

    //declare list of new raycast spawn points
    List<Vector3> rSkullSpawnPoints = new List<Vector3>() { soundSource.GlobalTransform.origin };

    var rRayCastSkull = GetWorld().DirectSpaceState;        

    for (int i = 0; i < rSkullSpawnPoints.Count()   1; i  )
    {
        var rRaycastSkullResult = rRayCastSkull.IntersectRay(rSkullSpawnPoints.Last(), earDrumRight.GlobalTransform.origin, rSkullHitObjects, 4);

        if (rRaycastSkullResult.Contains("collider") && rRaycastSkullResult["collider"] != null)
        {
            rSkullSpawnPoints.Add((Vector3)rRaycastSkullResult["position"]);

            GD.Print("rSkullHitPoint:  "   (Vector3)rRaycastSkullResult["position"]);
        }

    }

    rSkullSpawnPoints.Clear();

I'm pretty sure my problem is with my ray-cast dictionary check. I would like to have used ContainsKey() instead of Contains(), but ContainsKey() was not recognized. Any thoughts?

CodePudding user response:

I am not familiar with godot so I apologize if I misunderstand. If rRaycastSkullResult is of type Dictionary and you would like to parse it to see if a key exists then you could use rRaycastSkullResult.Keys.Contains("collider"). the Keys property returns a generic collection of just the keys. Cheers!

CodePudding user response:

I'm pretty sure my problem is with my ray-cast dictionary check.

If you have a Godot Dictionary, you can print it whole as a way to confirm.

would like to have used ContainsKey() instead of Contains(), but ContainsKey() was not recognized. Any thoughts?

The method Contains in Godot Dictionary checks for a key. It is what you want.


The Dictionary returned by IntersectRay is either empty or has all the documented keys. So you can check with rRaycastSkullResult.Count > 0 instead of checking the key.


Instead of making a ray cast at the result of the last one, exclude the the collider you have found (I see you have rSkullHitObjects, which would mean add the colliders there, if you don't want to modify it, you can work with a duplicate).

Why not change the origin of the raycast? Because the point the raycast finds would be in the surface of a physics body, and you know what is not good at boundary conditions? floating point numbers. In occasion you will find the new raycast finds the same point.

Which reminds me, pay attention to the condition to exit the loop. Should you not exit when you hit nothing? Should you not keep looping if you do? Will you have a predefined maximum number of iterations to avoid an infinite loop?


So... You are getting nothing? Enable visible collision objects from the debug menu. Perhaps the colliders aren't where you expect them to. Also are you sure that is the correct collision layer? (4 is the third layer. Yes, 1 is the first, and 2 is the second. But if you wanted the fourth layer you would have to put 8).

  • Related