T
Tazerrtot

Strange behavior with export var

So I noticed something happening a couple times when using export var and assigning their values to other variables at the top of the script. The variables don't receive the updated values from whatever value the export var is given in the editor. As an example this can be seen in the Robot script where "max_health" is an export var and below "health" is a var. When max_health is set to 10 in the editor, the setter function works as intended, but health is set to the default 5 that max_health has in the script.

I managed to get it to work by setting health to max_health in the _ready function, but I wanted to know if this behavior is intended.

  • Xananax replied

    The var values get assigned when the script loads, then changed values through export get assigned.

    1. script loads
    2. max_health is set
    3. health is set
    4. scene modifications get applied
    5. max_health is changed, setter runs

    By the time your modified max_health exported value is assigned, the health value already has.

    I agree that this is unintuitive, but it is intended. The workaround is to set health in the setter.

    Hope that helps!

    1 love