4.3
I’m currently following Bitlytic’s components tutorial to separate my objects into components, however i’m having trouble understanding how to assign values to the variables per object, in the tutorial he assigns the health value in the component’s script, yet this assigns the same value to every object with the component, to accomodate for that he modifies the value from the inspector, but that means he would have to change it for every object in the scene which would not work with dynamic instantiation, therefore i was wondering what is the best practice to accomodate for a case like this.
My first instinct is to assign the values within the object’s script and have the components retrieve it but this could cause some issues (the value is in the object script and components are supposed to unclutter the main object script, using get_parent() is a bad practice in general, all objects need the same variable name or the component’s code breaks) so i was wondering if there’s a better way to accomodate the values for each case.
This is my current code for a health component:
extends Node2D
class_name HealthComponent
@onready var parent = $"../.."
var maxHealth = parent.maxHealth
var health :float
func _ready():
health = maxHealth
func SubstractHealth(attack : Attack):
health = health - attack.attack_damage
if(health <= 0):
parent.queue_free()