Setting WorldEnvironment properties in gdscript?

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

Hi!

Does anyone know how to use GDscript to enable/disable WorldEnvironment properties?

In particular, I was hoping to use set_dof_blur_far_enabled to switch DOF on and off. Basically, I’ve set all parameters up in the inspector but I want to be able to toggle DoF on and off based on certain events. I thought it would be as simple as attaching a script to my WorldEnvironment node and using the set_dof_blur_far_enabled(bool) inside it, but it clearly doesn’t work like this. I can’t find any tuts. Can someone help please…

:bust_in_silhouette: Reply From: njamster
get_node("<RelativePathToNode>").environment.dof_blur_far_enabled = not environment.dof_blur_far_enabled

The get_node-part in front is only needed when the script is not attached directly to the WorldEnvironment-node. If you hover over a property in the Inspector, it will display the property name in a tooltip - use this to find out the right path for setting it from GDScript.

The documentation needs some examples to avoid some mistakes.

My mistake, for example, was the case below:

My example:

Wrong: $WorldEnvironment.glow_intensity = value
Correct$WorldEnvironment.enviroment.glow_intensity = value

I didn’t think I would need to use “enviroment” after “WorldEnvironment”.

I couldn’t understand this from the documentation either.

As always, thanks to the Godot community for this amazing tool.

trickstercode | 2021-11-25 20:21

:bust_in_silhouette: Reply From: trickstercode

I also had difficulty understanding how to change WorldEnvironment values in GDScript.
Seeing the answer you got, I understood (but it took me a while to realize) that in the case of WorldEnvironment, it’s not enough to write in GDScript the name of the WorldEnvironment + “.attribute”. To work, you need to write WorldEnviroment + enviroment + “.attribute”.

My example:

Wrong:
$WorldEnvironment.glow_intensity = value

Correct:
$WorldEnvironment.enviroment.glow_intensity = value

I think the documentation could have some example of using WorldEnvironment to facilitate the learning of those who are learning GDScript (as is my case).

As always, thanks to the Godot community for this amazing tool.