Property Setters
Lesson 26 of 48 in Coddy's GDScript for Lua Developers course.
Lua can intercept absent-field access through __index and __newindex. A write to an existing raw field bypasses __newindex, so property-like behavior needs separate backing storage or explicit setter methods. The example keeps the public text field absent and stores _text instead. GDScript declares get and set blocks on a variable. In Godot 4, assigning that property from inside its own setter updates its backing value without recursing. Ordinary assignment elsewhere invokes the setter. A leading underscore is only a naming convention. Here text normalization illustrates access rules without solving the task's numeric interval clamp.
Complete Lua 5.1 comparison. Run main.lua from the directory containing all shown files:
Lua file badge.lua:
local Badge = {}
Badge.__index = function(self, key)
if key == "text" then return rawget(self, "_text") end
return Badge[key]
end
Badge.__newindex = function(self, key, value)
if key == "text" then rawset(self, "_text", string.upper(value))
else rawset(self, key, value) end
end
function Badge.new() return setmetatable({_text = ""}, Badge) end
return Badge
Lua file main.lua:
local Badge = require("badge")
local b = Badge.new()
b.text = "go"
print(b.text)
Lua output:
GOGDScript file demo_badge.gd:
class_name DemoBadge
extends RefCounted
var text: String = "":
set(value):
text = value.to_upper()
GDScript file main.gd:
extends Node
func _ready() -> void:
var badge: DemoBadge = DemoBadge.new()
badge.text = "go"
print(badge.text)
GDScript output:
GOChallenge
EasyImplement LimitGauge.level:int with a setter clamping every assignment to 0..100. Initial value is zero. The solve function assigns the input then returns level.
Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.
Your code goes in limit_gauge.gd, exercise.gd. The supplied main.gd cannot be edited. Keep each class_name type in its named file.
Try it yourself
extends Node
func contract_ok(data: Array) -> bool:
var gauge: LimitGauge = LimitGauge.new()
gauge.level = -1
if gauge.level != 0: return false
gauge.level = 101
if gauge.level != 100: return false
gauge.level = 7
if gauge.level != 7: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in GDScript for Lua Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command Count5Objects and State
Scripts as ClassesInitializationInstance MethodsRecap: Independent BasketsProperty SettersPractice on your own: Online GDScript compiler