Unity's OnValidate() equivalent ?

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

I know that in Unity, when something is changed in the editor, OnValidate() is called. Is there something similar in godot ? I have a Planet node containing a script with a generatePlanet() function. this function uses resources given to the node to create a mesh for the planet and I’d like to be able to see this mesh in the editor and not only when the game runs.

:bust_in_silhouette: Reply From: johnygames

Yes, you can do that. In fact, any bahaviour that works in-game can also work in the editor. However, this is not the default behaviour. You need to write a script to enable it. Those scripts inherit from a class called EditorScript and they allow you to make changes to your game from the editor without running them. Here are the docs, here an here are a two videos.

Basically what you want to do is create a script that runs from within the editor whenever you input a resource file in the corresponding field.

Follow GDQuest’s tutorial

  1. Create a new script and write the keyword tool on the first line.
  2. Inherit from EditorScript
  3. Create export variables which are going to hold the resources for your generatePlanet() function. use the setget keyword followed by the name of the generatePlanet() function in order to have the script update in the editor every time a resource file changes.

If this helps you, please mark this answer as best so that others can find it useful too.

I don’t quite get how to use that getset keyword, could you give an example please ?

Teln0 | 2020-01-04 18:54