Godot scenes and scripts are classes

    As a result, many best practices in Godot boil down to applying Object-Oriented design principles to the scenes, nodes, or script that make up your game.

    This guide explains how scripts and scenes work in the engine’s core, to help you get a sense of how Godot works under the hood, and to help you better understand where some of this series’ best practices come from.

    Godot Engine provides built-in classes like . User-created types are not technically classes. Instead, they are resources that tell the engine a sequence of initializations to perform on one of the engine’s built-in classes.

    Godot’s internal classes have methods that register a class’s data with a ClassDB. This database provides runtime access to class information. contains information about classes like:

    • properties
    • methods
    • constants
    • signals

    This ClassDB is what Objects check against when performing an operation like accessing a property or calling a method. ClassDB checks the database’s records and the records of the Object’s base types to see if the Object supports the operation.

    On the engine’s side, every class defines a static _bind_methods() function that describes what C++ content it registers to the database and how. When you use the engine, you can extend the methods, properties, and signals available from the ClassDB by attaching a to your node.

    Even scripts that don’t inherit from a built-in type, i.e. scripts that don’t start with the extends keyword, implicitly inherit from the engine’s base Reference class. This allows the Object to defer to the script’s content where the engine logic deems appropriate.

    Note

    As a result, you can instance scripts without the extends keyword from code, but you cannot attach them to a

    As the size of Objects increases, the scripts’ necessary size to create them grows much, much larger. Creating node hierarchies demonstrates this. Each individual Node’s logic could be several hundred lines of code in length.

    Let’s see a simple example of creating a single Node as a child. The code below creates a new Node, changes its name, assigns a script to it, sets its future parent as its owner so it gets saved to disk along with it, and finally adds it as a child of the node:

    GDScript

    1. using System;
    2. using Godot;
    3. namespace ExampleProject
    4. {
    5. public class Main : Resource
    6. {
    7. public Node Child { get; set; }
    8. {
    9. Child = new Node();
    10. Child.Script = (Script)ResourceLoader.Load("child.gd");
    11. Child.Owner = this;
    12. AddChild(Child);
    13. }
    14. }
    15. }

    Script code like this is much slower than engine-side C++ code. Each change makes a separate call to the scripting API which leads to many “look-ups” on the back-end to find the logic to execute.

    Scenes help to avoid this performance issue. PackedScene, the base type that scenes inherit from, are resources that use serialized data to create objects. The engine can process scenes in batches on the back-end and provide much better performance than scripts.

    Why is any of this important to scene organization? Because scenes are objects. One often pairs a scene with a scripted root node that makes use of the sub-nodes. This means that the scene is often an extension of the script’s declarative code.

    The content of a scene helps to define:

    • What nodes are available to the script
    • How they are organized
    • How are they initialized

    Many Object-Oriented principles which apply to written code also apply to scenes.

    The scene is always an extension of the script attached to its root node. You can see all the nodes it contains as part of a single class.