I figured out how to create components without nodes (and it is much better!)

Create a resource with default values. Use that as the argument to _init(). That way you only have to change what needs to be changed.

It’s a good practice any time you have a large list of arguments.

As for your overall approach, it is similar to mine. I don’t use a node as a base class unless I specifically need the functionality of a node.

Something I think is worth mentioning - you can pass nodes as arguments. For example:

func _init(ref_speaker: AudioStreamPlayer2D) -> void:
	speaker = ref_speaker
	game = GameSounds.new(speaker)

Doing this lets you create mixin classes that do not need to be added to the scene tree.

Man I´m just starting a game for my subject and this is so amaizing thank you mate

Yes, resources are amazing! When you get the hang of them they are spectacular. For my stats I export a config file as a resource of resources.

class_name CreatureConfig
extends Resource

# Species
@export var core: CreatureCoreSettings

# Behaviours
@export var drifting: DriftingBehaviourSettings
@export var chasing: ChasingBehaviourSettings
@export var evading: EvadingBehaviourSettings
@export var bonding: BondingBehaviourSettings
...etc

Then each of those individual classes I create resources from for each creature.

So Drifting has this class:

class_name DriftingBehaviourSettings
extends Resource

@export var max_speed: float = 30
@export var change_max_angle: float = 3

@export var preferred_highest_drift: float = -200
@export var preferred_lowest_drift: float = 200

@export var energy_recovery_rate: float = 1

@export var interval_min: float = 0.5
@export var interval_max: float = 1.5

@export var direction_smoothing: float = 0.5
@export var speed_smoothing: float = 0.5

...etc

And I add it in the inspector and save it as the specific creatures class. If I need to add a stat to this behaviour, I add it to the behaviour class and every creature has it automatically (at least the default class value).

Resources are like literal game engine magic!

1 Like