Issue when calling super method

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Roshi

Hello,
Like the tittle says, I don’t understand how to properly call super methods…
For example :

Class A :
     export (Array) var transitions
     func _process(_delta):
        check_transitions()

Class B extends of Class A
     func _process(_delta):
        ._process(_delta)

Class C extends of Class B
     func _process(_delta):
        ._process(_delta)
       

My issue is that in class C only checks it own transitions but not those of class A and B.
How can I get base classes to also check their own transitions?

Thanks in advance

:bust_in_silhouette: Reply From: Bernard Cloutier

Your example is incomplete. It makes it difficult to understand your question.

Am I correct to assume that Class A, B and C all have a check_transition method? If so, you should have added it to your example:

Class A :
     export (Array) var transitions
     func _process(_delta):
        check_transitions()

    func check_transitions():
        # do stuff for A

Class B extends of Class A
     func _process(_delta):
        ._process(_delta)

    func check_transitions():
        # do stuff for B

Class C extends of Class B
     func _process(_delta):
        ._process(_delta)

    func check_transitions():
        # do stuff for C

If you have an object C and call _process on it, it will call B’s _process, which in turn calls A’s _process. A’s _process function will call check_transitions. Since there are 3 check_transitions methods defined for an object of type C, it will choose the most derived, which is C’s check_transitions method.

I’m not sure what you expected, since you seem to already know how overriding methods and calling the parent’s method works. I assume you meant to do something like this:

Class A :
     export (Array) var transitions
     func _process(_delta):
        check_transitions()

    func check_transitions():
        # do stuff for A

Class B extends of Class A
     func _process(_delta):
        ._process(_delta)

    func check_transitions():
        .check_transitions()
        # do stuff for B

Class C extends of Class B
     func _process(_delta):
        ._process(_delta)

    func check_transitions():
        .check_transitions()
        # do stuff for C

Am I missing something? If so, please add more detail and a more complete script.

Hello and thank you for your awnser.

Am I correct to assume that Class A, B and C all have a check_transition method? If so, you should have added it to your example:

No, only Class A had the “check_transition method”. My classes are really like I wrote them in my first post.

I’m not sure what you expected

I’m looking for a way that Class C checks A’s transitions then B’s transtions and C’s transtions.

I assume you meant to do something like this

I’m gonna try this solution.
Thank you

Roshi | 2020-10-05 08:41

No, only Class A had the “check_transition method”. My classes are really like I wrote them in my first post.

Alright, sorry then. I was a bit confused.

How is Class A meant to check class B and C’s transitions? You could have many more classes inheriting from A elsewhere in your game, and you wouldn’t want the code in A to check the transitions on some D object if all you have is a C object. The trick with inheritance is that you do not know about the derived classes.

I don’t know what the check_transition method does, but if you don’t need to change its logic in the derived classes, you could do this:

class A:
    func get_transitions(): # override this
        return [A_transition1, A_transition2]  # array containing transitions to check

    func check_transitions():
        for transition in get_transitions():
            # check

class B extends A:
    func get_transitions():
        return .get_transitions() + [B_transition1, B_transition2, B_transition3]

class C extends B:
    func get_transitions():
        return .get_transitions() + [C_transition1]

So if check_transitions was called on a C object, it would check the following transitions: [A_transition1, A_transition2, B_transition1, B_transition2, B_transition3, C_transition1]

edit: removed some backslashes

Bernard Cloutier | 2020-10-05 14:36

Hello.

I tried your way with the “get_transitions” method but it didn’t work as I want…
But I found another way to do what I want.
As my script is attached to nodes, I use the hierarchy between the nodes to check parent transitions :

func get_transitions():
if "transitions" in get_parent():
	return get_parent().transitions + transitions
else:
	return transitions

So far, it work perfectly.
Thank you for your help and time

Roshi | 2020-10-06 13:44

Good to hear it! Good luck!

Bernard Cloutier | 2020-10-06 17:00

1 Like