#How to properly compose node?

8 messages · Page 1 of 1 (latest)

fallen fox
#

Hello! Fairly new to Godot but experienced gamedev/programmer here. I have a design question about a problem I stumbled upon. I want to have buildings and units that can be damaged. I want to be able to damage things without knowing if it's a building or a unit, and I don't want to duplicate the damageable logic. So I came up with the straightforward class diagram attached as a picture.

My problem is that Building is a StaticBody3D and Unit is a CharacterBody3D (or KinematicBody3D for Godot 3.x) hence I can't have a single hierarchy. So far I for-see the following solutions:

  1. Make Damageable extends CharacterBody3D, the buildings will be dynamic bodies. Simple and should work.
  2. Make Damageable extends Node and add the 'logic' node as a child of the root body node. Kind of like a composition I guess? Seems more 'correct' but requires more work and might introduce coupling.

Any though or other ways/patterns to solve this in an elegant manner? Would love to ear how experienced Godot dev solve this :)

tacit vapor
#

You could split damageable into staticDamageable and liveDamageable (naming up to you) and static will hold stuff that doesn't move, live stuff that does move

#

I don't think GDScript supports multiple inheritance, but that could also be a way

#

Or, alternatively, have a node Building, which has sub-nodes of Damageable, and other nodes if you so choose

#

So you could have building composed of damageable node, a staticTarget node for example, and other stuff

dawn rivet
#

I'm not an experience godot dev, but as far as I understood, the engine wants you to compose behavior via nodes, so I would have your scenes hold a damageable node that they can expose then through a field (or exposing only the required methods if you so choose)

#

You could look at it as if the scene was your object graph in dependency injection, and the held nodes are the injected fields

vague cloud
#

You could create a "DamageHandler" node and add it as a child to anything that takes damage - Then the parent can subscribe to it's "On_Damage_Taken" signal or you could handle all the health/armor/healing in the DamageHandler and just emit a "Died"/"HealthZero" signal

Consider an object "damageable" if it has a "DamageHandler" child