godot: Export variable triggers setter

Operating system or device - Godot version: Ubuntu 16.10, Godot v2.1.official.stable

export var a = 10 setget set_a triggers setter, but var a = 10 setget doesn’t. This might be logical from the implementation point of view but from a developer point of view it doesn’t. An example why I don’t like this. I’m trying to keep track of the player health in some singleton or with Globals when changing scenes so I do:


export var hp = 100 setget set_hp

func _ready():
    if Globals.has('player_hp'):
        self.hp = Globals.get('player_hp')

func set_hp(x):
    Globals.set('player_hp', x)
    hp = x

This doesn’t work when using export like in the above example because the setter gets triggered when the node enters the scene so the if in _ready will always get triggered and resets the player hp to the default value. This is unwanted and if I’m not using export it works as expected.

So there’s an inconsistency on how the setter is triggered between using and not using export.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 24 (19 by maintainers)

Most upvoted comments

I like the fact the inspector is able to call setters. It’s incredibly handy. Why would we remove that feature?

Hmm it looks like two issues were discussed about here:

  1. Getter not being used by the inspector
  2. Setter being called by the scene loader (nothing to do with inspector)