Set properties via script or inspector?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Taylor Hansen
:warning: Old Version Published before Godot 3 was released.

While working on a mini-project using Godot I hit this realization: some of the inspector properties can be directly set via script in the _ready() function. I could easily just modify these properties in the inspector, but it may not be obvious to people looking at my project, like say a crucial property that must be set or the script will fail. On the other hand, I can explicitly declare this property to be set to something other than the default by including it in _ready(). Are there any drawbacks, efficiency or readability-wise, that could affect the choice between using the inspector or the script to edit properties?

:bust_in_silhouette: Reply From: DodoIta

In my opinion writing code yourself is always better and safer, especially if you’re changing a crucial property. Most importantly, writing code gives you a better understanding of what you’re doing (and also more control).
You can use the inspector if you prefer, I use it sometimes, but I tend to write code if I can, I don’t see any drawbacks.

In the end, it’s all about trying and trying :slight_smile:

:bust_in_silhouette: Reply From: eons

Non programmers (could be game designers, artists, level designers) using the project, may feel better looking at the inspector IF the property can be changed to alter a mechanic or needed for scene edition (export all the variables they may need, but no more that the needed).

For programmers, looking at the code they will look for flow control, math, algorithms in general, the values should not matter much but what is done with the values; where you initialize the values matters but not for readability, different places mean different things (export/var/init on instance, onready/ready/enter_tree when added to the tree), setting values on ready will change all values setted after instancing.

If a specific value can make the game explode in tiny pieces, you need a value check somewhere in the code (the common ones, == null and has_node), one day you may forget it and assign that value from another place and boom!.


About _ready:

Once a class is instanced, it uses _init, you can override that method on your script and set there all the values it can have that does not depend on the instance being on the scene tree, like the ones you use as simple var (or export var).
You can set more complex variables that require special initialization (or override parent class initialization).

Node and subclasses have _ready, this is called after _enter_tree (and called only once per instance on Godot 3), some variables may be better to (re)set on _enter_tree than on _ready, both work fine for variables that depend on the tree (like for checking the parent).

I have read that onready vars are supposed to be faster than vars set on _ready but I don’t know why, and don’t know if the new ready style will affect them.


If the value of a property can affect another or stored in a different way, use setters and getters, these can be used in interesting ways.

Nice answer, especially the part about the interface being easier on the game designers/artists. Your last sentence is pretty helpful too, but I don’t think you really needed that explanation about _ready. While Dodolta’s answer was also really helpful, this one was a little more concrete.

Taylor Hansen | 2017-04-15 00:41

eons is the pro :stuck_out_tongue:

DodoIta | 2017-04-15 08:21