#Better access to components

1 messages · Page 1 of 1 (latest)

merry otter
#

I have units with properties that are given to them via components.
Stats are given via a resource called "stats" so to access for example defense this needs to be done:
unit.stat_component.stats.defense

This works but feels a bit roundabout. Is there a better way to do this or should I leave it as is?

#

The stat resource itself is dropped on the unit node and then handed to the component by the script

eager kestrel
#

I think the current setup you describe is fine. You could maybe shortcut some of it by including a function on the unit class, like "increase_stat(STAT, amount)" that will check if that unit has:

  • A stat component
  • The specified stat
  • If so, increase that stat by amount.

Then your code would look more like "unit.increase_stat(STAT.DEFENSE, 3)" or something, but it is still readable, and management of stat changes can be delegated to functions that can be called instead of updating stats directly.

distant linden
#

It is also a good idea because each node may use the component differently.
Leaving the implementation to its owner means that it can be more easily re-used somewhere else. And that other nodes don't have to worry about the owner AND the component.

kindred flame
distant linden
#

That is what local_to_scene is for. But ye.

kindred flame
#

Not yucking anyone's yum. There are a million ways to do anything in game dev

eager kestrel
# kindred flame Careful with this. If the stats are indeed coming from a resource, each unit usi...

I overlooked the fact that they were applying the change directly to the stat resource...

@merry otter personally I feel better practice would be to assign stats to the units directly. The stats resource informs the Unit what its base stats should be, but then you can modify the Unit stats directly without fussing with the Stat resource itself (which , as Mysersguy suggests, could cause issues, i.e. accidentally updating ALL unit stats).

kindred flame
merry otter
#

The resource set function has been modified in order to save and move value.duplicate() instead of the actual resource