• Godot Help
  • Functions and Properties Sharing in Godot

Hi all!
I want to share some properties and functions with the player and enemy characters for not to write them many times.
For example HP, state machine, speed, etc.
I made the player CharacterBody2D and the enemy Area2D.
I couldn't inherit from more than one script (one Node in that case).
How do I share those properties and functions between them?
I'm pretty new to OOP so maybe it's a stupid question.

    Sonadrin I want to share some properties and functions with the player and enemy characters

    you mean BETWEEN the player and enemy characters

    Sonadrin for not to write them many times.

    scientists were so preoccupied with whether they could they didn't stop to think if they should've

    Sonadrin For example HP, state machine, speed, etc.

    these are all completely different

    Sonadrin I made the player CharacterBody2D and the enemy Area2D.

    yes

    Sonadrin I couldn't inherit from more than one script (one Node in that case).

    godot does not support multiple inheritance (godot, not gdscript. you can't do this with C# either due to the way godot works) (at least in theory).

    but you CAN call methods from completely different classes, you can check the method with has_method().
    this is why we encapsulate, you are NOT supposed to access HP etc directly, but through a function such as get_health()/set_health()/damage()

    godot does support inheritance which is useful for keeping a statically cast array of objects, in fact every single node inherits from another, all the way down to Node, and then to object.
    So both CharacterBody2D and Area2D inherit from Node2D. but you might need the methods present in CharacterBody2D that don't exist in Area2D and vice-versa.

    Sonadrin How do I share those properties and functions between them?

    the way to do something like this is to use Resources. a class inherited from Resource does not have the methods of Node like get_node, but can be used to store data such as HP, and have them interact in the respective scripts. this however adds more work so only do it if there's a billion variables like in an RPG.
    add the Resource as a variable in your script.

    another way is to use a dictionary and fill it with the same values. the get() method of dictionary can be used to retrieve a value even if it doesn't exist, returning a default value instead, or being used to check for errors and missing variables.
    var hp : int = dictionary.get("HP", 10)

    or just put the same variables in both scripts, but I don't recommend you do this.

    edit: there's also the much better option, use meta_data. the has_meta and get_meta functions can be used to obtain metadata from any node. then, at the bottom of the inspector you can add metadata. these are variables that are added to the Node, similar to BGE's properties.

    Sonadrin I'm pretty new to OOP so maybe it's a stupid question.

    keep learning

      Jesusemora
      Wow thanks for the detailed answer!
      I just saw a series of videos from this guy .
      I think I understood how to do it and I like the style of what he did.
      I will probably change the enemy to character 2D because I think I don't need the _on_area_entered anymore.
      So making them the same type of node will make things simpler.
      I will use your tips about the metadata and the usage of the methods (get_health(), etc..)
      Thanks again!