0 votes

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?

in Engine by (19 points)

2 Answers

+2 votes
Best answer

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.

by (7,922 points)
selected by

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.

eons is the pro :p

+1 vote

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 :)

by (567 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.