0 votes

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

in Engine by (75 points)

1 Answer

+1 vote
Best answer

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.

by (1,114 points)
selected by

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

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

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

Good to hear it! Good luck!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.