mercredi 4 novembre 2020

Overriding a critical base method while preserving its functionality without making my code smelly

In Godot, it is possible to define a signal and emit it in a method, which will call all the methods connected to it. In this example, on_met_customer() is connected to signal met_customer in the class Merchant, which is responsible for detecting customers.

# Parent class
extends Merchant
class_name Salesman

func greet():
    print("Hello!")

func on_met_customer():
    greet
# Child class
extends Salesman
class_name CarSalesman

func shake_hands():
    print("*handshake*")
    
func on_met_customer():
    # .on_met_customer() I don't want to have to remember to call super
    shake_hands()
# Desired console output when calling on_met_customer() from the child class
Hello!
*handshake*

What I want to do is either:

  1. "Extend" instead of overriding on_met_customer() to preserve the parent functionality without calling super, or:
  2. Warn myself somehow when overriding on_met_customer() to not forget to call super

Aucun commentaire:

Enregistrer un commentaire