Saturday, July 26, 2025

godot – Methods to pre-process the @export variable in Godot4?

That is what you do:

  1. Export a variable to the inspector:
@export var arena_size:int = 80

Observe: That is the property you’ll set within the inspector. It is very important retailer it and cargo it as a result of that is the worth you may be enhancing, and you don’t want to lose what you have been doing.

  1. Export one other variable, however just for storage, so it doesn’t present up it within the inspector:
@export_storage var actual_arena_size:int

Observe: That is the property that may maintain the pre-processed worth. It is very important retailer it and cargo it as a result of that is the already processed worth, and you don’t want it to course of it once more each time.

  1. Make the previous set the later, however solely on the editor:
@export var arena_size:int = 80:
    set(mod_value):
        if arena_size == mod_value:
            return

        arena_size = mod_value
        if Engine.is_editor_hint():
            # pre-process right here
            actual_arena_size = arena_size*2

Observe: This can solely pre-process the opposite property once more whether it is operating within the editor and the worth modified. If we let it do the pre-process code on runtime, then when Godot hundreds this property it is going to execute it, so you don’t get the advantage of skipping the preliminary pre-process… We would remedy that by introducing a 3rd property to examine, however I am leaving that out in advantage of maintaining this easy.

  1. Use the processed property all over the place else:
func _ready():
    print("SIZE:",actual_arena_size)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles