Property Setters
Lesson 26 of 48 in Coddy's GDScript for C Developers course.
C struct field assignment cannot automatically invoke a setter. Use explicit functions, and hide storage behind an opaque type when access must be enforced. GDScript attaches get/set blocks to a variable. Ordinary assignment invokes its setter even inside the class, but accessing that property from its own setter uses underlying storage to avoid recursion. Initialization has special rules, so start with valid data. A leading underscore is only a GDScript naming convention. The C example explicitly invokes an ASCII-uppercase setter function.
Complete C comparison. Save the listed files together and compile the .c files; main is the entry point.
C file badge.h:
#ifndef BADGE_H
#define BADGE_H
#include <stddef.h>
struct Badge { char text[16]; };
static inline void badge_set(struct Badge *b,const char *s) { size_t i=0; for(;s[i] && i<sizeof b->text-1;++i) b->text[i]=(s[i]>='a' && s[i]<='z')?(char)(s[i]-'a'+'A'):s[i]; b->text[i]=0; }
#endif
C file main.c:
#include <stdio.h>
#include "badge.h"
int main(void) { struct Badge b={{0}}; badge_set(&b,"go"); puts(b.text); return 0; }
C output:
GOGDScript example file demo_badge.gd:
class_name DemoBadge
extends RefCounted
var text: String = "":
set(value):
text = value.to_upper()GDScript example 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. Solve 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. main.gd holds the supplied input/output code and 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 C 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