Announcement

Collapse
No announcement yet.

Virtual function override

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Virtual function override

    Ok, so what I'm trying to do should be simple.
    This is a blueprint-only project.
    I have a Character class and two derived classes: Player and Enemy.
    In Character I have CheckDeath function (implemented) and a Die function (virtual). CheckDeath should call Die from Character blueprint, and then each derived class should handle the call with different implementation each. The problem is, when I call Die from Character class, it doesn't call Die from subclass, but from itself. I tried making it pure virtual, but then I can't seem to execute it from Character.
    Now I'm trying to use Dispatchers, but feels like I'm overcomplicating something that should be simple.

    Any thoughts?

    #2
    Sounds like you need 3 functions CheckDeath, CanDie and Die.

    CheckDeath calling CanDie and then if true Die. All in the Character Class

    Then in Player and Enemy you can override the CanDie function.

    Comment


      #3
      Every function in Blueprint is virtual and can be overridden btw.

      Comment


        #4
        I'll try to better explain the problem. This is the piece of code in question.

        Click image for larger version

Name:	CharBP.png
Views:	2
Size:	87.3 KB
ID:	1664472

        This event calls CheckDeath... CheckDeath just checks if Health is lower than zero and then calls Die, which should be implemented in derived classes.

        Click image for larger version

Name:	CheckDeath.jpg
Views:	1
Size:	56.4 KB
ID:	1664473

        At the moment, Die just print something.

        Click image for larger version

Name:	CharDie.jpg
Views:	2
Size:	32.2 KB
ID:	1664475
        Click image for larger version

Name:	PlayerDie.jpg
Views:	1
Size:	29.4 KB
ID:	1664468

        And the result...

        Click image for larger version

Name:	Debug.jpg
Views:	1
Size:	56.0 KB
ID:	1664470

        That means the function Die from Player is not being accessed, even though Player is derived from Character. If I try to make Die pure virtual, I cannot call it from Character blueprint for some reason.

        Click image for larger version

Name:	CharCheckDeathPure.jpg
Views:	2
Size:	44.8 KB
ID:	1664476


        And that's it. Am I doing something wrong? Any ideas?
        Attached Files

        Comment


          #5
          Pure function are functions that should return something since they are evaluated at every call. Pure functions should not have any side effects potentially changing the return value for every call unless if the input parameter is different.

          The purity of a function has nothing to do with inheritance, however it is a bit odd that "Char Die" is being called and not "Player Die". Are you sure that you don't have a CharacterBP instead of a PlayerBP placed in the Level?

          What other options are not default in the class and functions?

          Perhaps try and recreate the behavior in two new Blueprint classes and see if the problem persists.

          Comment


            #6
            Originally posted by GarnerP57 View Post
            Are you sure that you don't have a CharacterBP instead of a PlayerBP placed in the Level?
            omg, I feel SO dumb. You are absolutely right, everything is working as expected now.

            Originally posted by GarnerP57 View Post
            Pure function are functions that should return something since they are evaluated at every call. Pure functions should not have any side effects potentially changing the return value for every call unless if the input parameter is different.
            I thought "pure" meant C++ pure virtual functions. I'll read more about this.

            Thank you very much GarnerP57! You saved my day.

            Comment


              #7
              Glad you solved it, it is a classic mistake we all make making wrong assumptions as to where the issue is which results in tunnel vision. Sometimes you just need to take a step back and go through everything.
              Originally posted by Jord Hisu View Post
              I thought "pure" meant C++ pure virtual functions. I'll read more about this.
              The main difference between pure and impure functions is that impure functions have to have their execution pin called while pure functions don't have execution pins and is called once for every node it is connected to.
              Last edited by GarnerP57; 09-16-2019, 04:08 AM.

              Comment

              Working...
              X