property_setter_getter.gd 743 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # 4.0+ replacement for `setget`:
  2. var _backing: int = 0
  3. var property:
  4. get:
  5. return _backing + 1000
  6. set(value):
  7. _backing = value - 1000
  8. func test():
  9. print("Not using self:")
  10. print(property)
  11. print(_backing)
  12. property = 5000
  13. print(property)
  14. print(_backing)
  15. _backing = -50
  16. print(property)
  17. print(_backing)
  18. property = 5000
  19. print(property)
  20. print(_backing)
  21. # In Godot 4.0 and later, using `self` no longer makes a difference for
  22. # getter/setter execution in GDScript.
  23. print("Using self:")
  24. print(self.property)
  25. print(self._backing)
  26. self.property = 5000
  27. print(self.property)
  28. print(self._backing)
  29. self._backing = -50
  30. print(self.property)
  31. print(self._backing)
  32. self.property = 5000
  33. print(self.property)
  34. print(self._backing)