How can I toggle visible collision shapes/polygons from a script?

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

The most promising thing I’ve found so far is the debug_collisions_hint property of SceneTree. However, it seems to be a bit finicky: If I set the field to true inside of _ready(), then collision shapes will be visible. But if I try to change its value later (in _unhandled_key_input() specifically), nothing happens.

I just need this for debugging purposes, but it’d be nice not to have to close the game, manually toggle the option, and restart the game every time.

:bust_in_silhouette: Reply From: uriel

generally, visible is a base property, you can easily setup using signals or user actions (keys, mouse, etc…) and then, toggle hide/show with:

visible = !visible

Thanks, that seems to work! It’s not an ideal solution since I still need to iterate over every CollisionPolygon2D and CollisionShape2D object, or place each of them in a group. But it will do for now.

andrej88 | 2019-10-01 17:53