Invalid get index 'collider' (on base: 'Dictionary'). While Raycasting

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FreddieBees

I am trying to raycast from the ememy(pos) to the player(targetPos) and have gotten: Invalid get index ‘collider’ (on base: ‘Dictionary’).


var space = get_world().direct_space_state
var collision = space.intersect_ray(pos, targetPos, [self])
if collision != null and collision.collider.is_in_group("Player"):
    runActive = true

Edited to fix code formatting. Use the {} button in future posts to format code blocks.

jgodfrey | 2023-01-15 20:34

:bust_in_silhouette: Reply From: jgodfrey

I think your problem is here:

if collision != null and collision.collider.is_in_group("Player"):

As documented intersect_ray() will return an EMPTY dictionary if no ray intersection is found. So, I assume that’s what’s happening here. However, an EMPTY dictionary IS NOT null, so it’ll get through that first != null check without issue. Then, the very next check attempts to access the collider key, which won’t exist and triggers the error you report.

Change the above code to:

if collision and collision.collider.is_in_group("Player"):

Thanks! I had tried this in many different ways but not if it was colliding and was hitting the player.

FreddieBees | 2023-01-15 21:53