To extend node or not to extend node, that is the question

Godot Version

Godot 4.2

Question

I’m writing some custom classes for use in a project, and most of the examples of custom classes that I’ve seen extend from another class, most commonly extending from node. The classes that I’m making right now are mainly for storing and updating data pertaining to the behavior of characters in my game, and since each character will already be implemented as a subtree of nodes, I’m wondering if there is a significant difference between extending this class as a node and adding it as a child to each character or instead just using the class in a script attached to the character node. Are there any notable performance/capability differences that I should take into consideration when deciding this?

For collisions it can be quite helpful to extend CharacterBody2D, this will allow you to filter and directly access members of that class like so

func _on_body_enter(body: Node2D) -> void:
    if body is Player:
        body.add_coin(1)
        queue_free()

If you are just storing data it may be more helpful to extend from Resource instead of Node. Adding and removing nodes from the scene tree is quite taxing for the engine.

Based on this reply I looked more into custom Resources and that was exactly what I was looking for, thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.