Godot Version
4.3 i think
Question
so basically at the moment i am using this
func _on_body_entered(body : Node2D) -> void:
for child : Node in body.get_children():
if child is HealthComponent:
child.hit(kusarigama_damage)
so i think this creates an array of all the components of the characterand this is probably quite expensive, is there a way i can access this component that i can go directly to it to skip the other steps, also this is a Health component that i will attatch to everything damageable so it has to be able to access it no matter what it is attatched to…
Help would be appreciated geniuses of this forum
I think the easiest way is that the body holds a variable for each component so you can access it directly
1 Like
thats not a bad idea…so your saying in the character body script your saying create a variable that directly accesses the component and then instead of scanning through components use this variable to adjust the component?
Thing is when you have lots of components you will have hundreds of variables
because of all the enemies in the game. Would this become expensive in itself because you are theoretically creating variables about things that already exist… is there not a way to access it directly? sorry im very new to this and just trying to gain as much knowledge as possible
well if you dont want to search for a component you need an existing “pointer” to the component and a variable is one way of doing it. You can also create a dictionary or array with fixed positions for each component.
The question is how many components do you plan to make?
1 Like
I am not sure yet i am redesigning a lot of my game progress because before i was just accessing variables in the enemies CharacterBody2D but have noticed it gets so repetitve when doing that a lot and learned about making components so thought this is an easier way of doing things.
But i am pretty sure i will probably need things for all the basic attributes i would need to have on any Character Body such as
Health, Attack, Hitbox,
So maybe not too many, but this is off top of my head and feel like it will build up. Just wondering if there is a way for my hitbox component to point directly at the health component.
i appreciate your help am just trying to learn as much as i can about different ways of doing things and best practices.
Well normally you dont have too many components and even if you have too many you can still use a dictionary. This will always be faster then looping through all children.
In the end its a question about performance or memory usage and normally you dont need to think about that unless it becomes a problem. Most of the times i go for performance
1 Like
Why not just use
if body.get_node_or_null("HealthComponent"):
body.get_node("HealthComponent").hit()
I think internally it will do the same as looping through the children yourself
1 Like
okay so a dictionary is better for performance than creating single variables that point to each component?
No dictionary and variables should be the same but dictionary is more pleasent to work with when you have a lot of values
1 Like
amazing thankyou very much
1 Like