C# Raycasting, and Cannot Open File Dictionary.cs

Godot Version

4.2.2

Question

Coming from Unity, I’m used to being able to raycast easily from scripts, rather than attaching a node to a game object. I’ve been attempting to do a raycast in Godot programmatically, using C#, to reduce the clutter on my node tree. The intended use is for the player character to interact with an object directly in front of him, if there’s a valid object within reach.

private void HandleInteraction() {

		if (Input.IsActionJustPressed("interact")) {
			// Get the direction the player is facing
			var direction = new Vector2(Mathf.Sign(Velocity.X), 0);

			// Adjust the raycast origin to the center of the player's CollisionShape2D
			var raycastOrigin = Position + collisionShape2D.Position;

			// Calculate the raycast length, slightly wider than the player's CollisionShape2D
			var raycastLength = collisionShape2D.Scale.X * 1.2f; // Adjust the factor '1.2f' as needed

			PhysicsRayQueryParameters2D rayParameters = PhysicsRayQueryParameters2D.Create(raycastOrigin, new Vector2(raycastOrigin.X + raycastLength, raycastOrigin.Y));

			// Cast the ray towards the direction the player is facing
			var raycastResult = GetWorld2D().DirectSpaceState.IntersectRay(rayParameters);

			if (raycastResult.Count > 0) {

				// Check if the collided object is an InteractableItem on the correct collision layer

            	Object collider = raycastResult[0];
				
				if (collider != null && collider is InteractableItem interactableItem) {

					GD.Print("Collider is interactable.");
					// If the raycast hits an InteractableItem, call the appropriate method
					if (currentlyCarriedItem == null) {
						interactableItem.PickUp(this);
						currentlyCarriedItem = interactableItem;
					}
					else {
						interactableItem.Drop();
						currentlyCarriedItem = null;
					}
				}
			}
		}
	}

I get a series of strange errors that occur on the line with Object collider = raycastResult[0];

Debugger

E 0:00:02:0976 Dictionary.cs:204 @ Godot.Variant Godot.Collections.Dictionary.get_Item(Godot.Variant): System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Output

Cannot open file ‘/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs’.
Failed to read file: ‘/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs’.
Cannot load C# script file ‘/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs’.
Failed loading resource: /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs. Make sure resources have been imported by opening the project in the editor at least once.

I’m trying to get the collider of the first object that gets hit by the ray, but instead I’m getting these errors. I don’t know how to resolve them.

Does anyone else know how to solve these problems? Alternatively, is there a better way to go about casting this ray without referencing a pre-existing node?

Additionally, how can I visualize the raycast on screen for debugging purposes?

The key exception you’re finding is because IntersectRay returns a dictionary, not an array - you can see the result structure in the docs.

1 Like