All Questions

Community

A
Nathaniel Adams
posted to: The Power System

Questions on type hints in PowerSystem.gd

In PowerSystem.gd, the method

func _on_entity_placed(entity, cellv: Vector2) -> void:

doesn't specify the type of entity. Is this intentional? Also, in

func _get_power_source_from(entity: Node) -> PowerSource:
entity is typed as a Node. Could entity be typed as the Entity class instead of Node, and is it intentional that it's not?

Thanks!
  • Razoric replied

    1. It's intentional - when there is no type hint, the type is Variant, which means it can be anything. While all entities that can be placed will be entity, it does not need to be and will still work with the signal without crashing. However, if you adhere to a very strict regimen with type hints, then yes, absolutely, you can specify it as : Entity.

    But there may be cases where this is not going to be the case. For example, if you have a very special and unique entity that acts entirely differently from all other entities. If that's not a case you ever plan, then it's fine, but the option is there.

    2. Also intentional. It allows us to pass in nodes that may not be entities but still have a power source. The function itself does not require anything from the Entity class - just Node (get_children()). It's the only class that it needs the entity to be to not crash (so you can't pass in a Reference type.)


    So they are intentional on my part, but they are benign examples and you are not going to break the code by being more strict with your type hints.