Skip to reading
GDScript Programming & Godot

47. Your own classes and helper objects

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
class RewardData:
    var item_id: String
    var points: int
Code example
    func _init(new_item: String, new_points: int) -> void:
        item_id = new_item
        points = new_points
Code example
func run() -> Array:
    var reward := RewardData.new("key", 5)
    return [reward.item_id, reward.points]

An inner class is useful when a small helper belongs with one script record. A separate .gd file can extend RefCounted for a data/service helper, Node for a scene component, or Resource for serialized editor data.

class_name registers a file-backed Godot class in the Godot project. PCA compiles authored records dynamically; do not depend on a record’s class_name turning it into a universally registered file-backed class. Use CallScript/IncludeScript for record-to-record libraries.

In a Godot source project you can create a file such as res://custom/reward_data.gd and preload it. That file must actually be present in the project/export. Writing its path in a string does not create or package it.

Page 56 of 378Permalink
Go to page