new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Godot 4 Patterns · all subjects

typing

12 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Type everything in GDScript 4.6 strict mode

Always explicitly type variables, function parameters, and return types. Bad: var data = {}; func process(items): for x in items:. Good: var data: Dictionary = {}; func process(items: Array) -> void: for x: String in items:.

StringName vs String auto-conversion

StringName and String usually auto-convert, but can fail in strict comparisons. Be aware of the type when comparing identifiers.

Variant from Dictionary index breaks := inference

The := operator fails when the right side is a Dictionary or Array index access because these return Variant, not a concrete type. Replace with explicit type annotation: var first: String = tokens[0] or var first: String = str(tokens[0]).

Variant from dict.get() with default

Using := with dict.get() breaks type inference because it returns Variant. Fix by using explicit type cast: var value: String = str(dict.get("key", "")) or var value: int = int(dict.get("count", 0)).

Variant from JSON.parse breaks type inference

json.data returns Variant, breaking := inference. First check the type: if typeof(json.data) != TYPE_DICTIONARY: return {}. Then assign with explicit type: var data: Dictionary = json.data.

Untyped for iterator triggers UNTYPED_DECLARATION warning

Writing 'for x in arr:' without a type annotation triggers UNTYPED_DECLARATION which becomes an error in strict mode. Fix by typing the iterator: for x: String in arr: or cast inside loop: var s: String = str(x).

const := value syntax fails in Godot 4.6

Using const FOO := 100 or const BAR := "hello" breaks strict typing. Write const FOO: int = 100 or const BAR: String = "hello" instead.

Function returning Dictionary pattern with JSON parsing

func _read_json(path: String) -> Dictionary: var f: FileAccess = FileAccess.open(path, FileAccess.READ) if f == null: return {} var text: String = f.get_as_text() f.close() var json: JSON = JSON.new() if json.parse(text) != OK: return {} if typeof(json.data) != TYPE_DICTIONARY: return {} return json.data

Typing dict.keys() iteration pattern

When iterating dict.keys(), type the iterator explicitly: for key: String in dict.keys(): and then cast the value: var val: Dictionary = dict[key].

Safe pattern for is check before property access

Do not access properties on Variant or uncast types. Use type guard first: if event is InputEventKey: var ke: InputEventKey = event; if ke.pressed and ke.keycode == KEY_ENTER: ...

Project-level strict typing warnings configuration

In project.godot [debug] section, control strict typing warnings with: gdscript/warnings/untyped_declaration=0, gdscript/warnings/unsafe_call_argument=0, gdscript/warnings/unsafe_property_access=0, gdscript/warnings/unsafe_method_access=0, gdscript/warnings/unsafe_cast=0, gdscript/warnings/untyped_iterator=0, gdscript/warnings/inferred_iterator=0, gdscript/warnings/unused_signal=0, gdscript/warnings/treat_warnings_as_errors=false. Prefer fixing types over silencing warnings.

Strict typing for InputEventKey: check type first, then access

In Godot 4.6 with strict typing, do not access .pressed directly on a Variant. First check 'if event is InputEventKey', then cast to InputEventKey and access .pressed. This prevents type warnings.

Give your agent this brain