Godot Version
Godot 4.4.1 (stable)
Question
I’ve just come across some information about ‘exporting variables to the editor’ etc. but the information doesn’t really explain why you need to! - it also goes on about ‘getters’ and ‘setters’ ?Well, I can honestly say at this moment, I don’t quite follow what it’s about. I find it puzzling as to why you would need to export variables to see them in the inspector section of the UI; you’re able to change the variable value ‘in real time’ ? but that can cause differences between the inspector value and script code values? Mmm?
Would someone please be kind enough to explain to me what the whole idea is behind the exportation of variables, getters, and setters… or possibly point me in the right direction to explanatory videos etc ?
Any help would be really appreciated
getter and setters are a way to run code when accessing or editing a variable, it’s for ease of use, comes up rarely, and is never strictly needed.
@export
is extremely useful, not only can you edit the variables in the inspector but it assigns unique values to each object with the script.
For example @export
ing a speed variable allows you to control the speed of each character individually without making a new script for every instance of the character. A simple spawner script can export what to spawn and it can work with any scene without modifying the code.
extends Node3D
# Could be *any* scene!
@export var spawnable_object: PackedScene
func _on_timer_timeout() -> void:
var clone = spawnable_object.instantiate()
add_sibling(clone)
Hi there. thank you for your reply. Please forgive me but that is too detailed for me at this time to fully appreciate/understand (newbie status, lol). I’m struggling with WHY and the very need for them to be exported. I would love it if I can find some very basic examples (scripts, projects etc) showing exactly their applications.
As already mentioned, I don’t fully understand what you’ve stated here but I value your time and input, so thank you for that.
Try out the script I posted, when given a timer it will spawn what ever scene is provided to it at regular intervals. Because it uses @export
the script doesn’t have to know what it’s spawning as that information is given through the editor to the node.
Hi. I have found the following that demonstrates a use for @export etc.
I can clearly what the person is doing. The export variable is being modified for each object, creating varying speeds that the objects move across the screen. The modifications are obviously being made in the inspector for each, which would create variances between the adjusted inspector values and the original script values, relevant for ‘speed’. …but WHY would you need to perform these modifications in this ‘live’ way? surely, the values in the scripts would suffice based upon the original idea and design?
Thanks
You could also ask why Godot has visual scripting, when writing code is an option.
I suspect the answer is the same in both cases - that the feature makes it easier for people who are trying out Godot to create games.
But if I have two characters whom I want to operate the same but at different speeds, I don’t want to copy the scirpt and only change one variable, so @export
allows me to use the same script while specifying different values arbitrarily.
Getters and setters can be extremely useful, and I wouldn’t say they come up rarely. It all depends on how you’re designing your stuff. Consider this example akin to the built-in Node2D:
It has both a global_position and position, global_rotation and rotation etc.
Now, in GDScript, there is no private scope. Anyone can, at any moment, change either the global_position or the position of the node.
If you don’t have a setter, how do you keep them in sync? You can calculate one from the other, but how do you know which is calculated from which? You cannot control the access to them.
But if you have a setter for them both, you can calculate global_position whenever position changes and vice versa.
You can also protect your variables from invalid state with setters.