Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically create trait dependencies (trait dependency injection) #5

Merged
merged 7 commits into from
Jan 20, 2024

Conversation

Earewien
Copy link
Owner

This PR brings automatic trait dependencies injection with cyclic dependency detection

Using this Healthable trait : it depends on a Killable and a Loggable. When assigning this trait to an object, if it is already killable or loggable, those traits are automatically injected to the Healthable instance. Else, they are automatically instantiated (recursively!) and saved into the receiver for futur usage.

This simplify trait usage, and guarantee that an object owns one and only one instance of each trait.

# @trait
class_name Healthable
extends RefCounted

var max_health:int = 100
var health:int = max_health:
    set(value):
        var old_health:int = health
        health = clamp(value, 0, max_health)
        if old_health != health:
            _loggable.log("HP : %s/%s" % [health, max_health])
            if value <= 0:
                _killable.kill()

# Reference to the Loggable, needed for this trait to print messages
var _loggable:Loggable
# Reference to the Killable, needed for this trait to handle death !
var _killable:Killable


# Automatically requires that the trait receiver is also a Killable and a Loggable object
# If the receiver does not have those traits yet, they will be automatically instantiated and
# registered into the receiver for future usages.
func _init(killable:Killable, loggable:Loggable) -> void:
    _killable = killable
    _loggable = loggable

Cyclic dependency error/detection:

image

@Earewien Earewien self-assigned this Jan 20, 2024
@Earewien Earewien added the enhancement New feature or request label Jan 20, 2024
@Earewien Earewien merged commit 693d539 into main Jan 20, 2024
@Earewien Earewien deleted the feature/dep-injection branch November 27, 2024 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant