property_setter_getter.gd 821 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. var property_2:
  9. get(): # Allow parentheses.
  10. return 123
  11. func test():
  12. print("Not using self:")
  13. print(property)
  14. print(_backing)
  15. property = 5000
  16. print(property)
  17. print(_backing)
  18. _backing = -50
  19. print(property)
  20. print(_backing)
  21. property = 5000
  22. print(property)
  23. print(_backing)
  24. # In Godot 4.0 and later, using `self` no longer makes a difference for
  25. # getter/setter execution in GDScript.
  26. print("Using self:")
  27. print(self.property)
  28. print(self._backing)
  29. self.property = 5000
  30. print(self.property)
  31. print(self._backing)
  32. self._backing = -50
  33. print(self.property)
  34. print(self._backing)
  35. self.property = 5000
  36. print(self.property)
  37. print(self._backing)
  38. print(property_2)