Title here
Summary here
Godot's `Array` type can be used in GDScript to hold typed collections of fields. They’re useful for grouping data together to form records.
This `person` class type has `name` and `age` fields.
```gdscript
class_name Person
var name: String
var age: int
The _init
function constructs a new person instance with the given name.
func _init(name: String):
self.name = name
self.age = 42
You can create a new instance of a Person
.
var p = Person.new("Alice")
print(p.name) # Output: Alice
print(p.age) # Output: 42
You can name the fields when initializing an instance.
p.name = "Bob"
p.age = 20
print(p) # Output: [Object:Object:1026]
GDScript does not support anonymous structs, but you can use Dictionaries for similar purposes.
var dog = {
"name": "Rex",
"is_good": true
}
print(dog) # Output: {name:Rex, is_good:true}
To run this script, attach it to a Node in your Godot project and run the scene.
# Set up a simple scene in Godot
extends Node
func _ready():
var p = Person.new("Alice")
print(p.name) # Alice
print(p.age) # 42
p.name = "Bob"
p.age = 20
print(p) # Person Object
var dog = {
"name": "Rex",
"is_good": true
}
print(dog) # {"name": "Rex", "is_good": true}
Next example: Methods