[SOLVED]Trying to code a Life Leech Mechanic.

0 favourites
  • 5 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • I'd like to incorporate a method for my player to leech life from enemies based on the damage of each attack.

    The image below shows how damage is calculated in my game. It's a bit complicated, since there are many possible debuffs the monsters could have to increase the damage taken.

    I want to take 5% of the damage dealt and add it to player health.

    What I have done so far is add another line below it "Add to Health -> Damage calculation*0.05", but this is very cumbersome. I have 40 different attack types in this game, and I am looking for a simpler way to do this.

    Is there a way to store the damage value each time a monster takes damage and use that in a calculation to add player health?

    Thanks.

  • I mean you could first store the calculated damage in a variable and then subtract it from the health.

    By 40 do you mean you have the events you’ve shown duplicated 40 times with individual tweaks for different attacks?

    That just means there will be busywork for any change you go with. Personally I’d try to utilize a function to use less duplicated code.

    I guess a two event hack would be to add a variable to the monsters for their old health and do something like this:

    Every tick

    — monsters: set oldHealth to self.health

    … all existing events …

    For each monster

    — player: add 0.05*(monster.health-monster.old health) to health

  • Yes, code similar to this would be replicated 40 time (80 with crits) I am not a strong coder.... more of a game designer. I know its' inefficient.

    Would this solution not cause the 5% life leech to be added constantly?

    Is there a way to trigger an event "when monster health changes"? or "when health is subtracted from monster"? Then I can have one single event which would apply to any damage they receive.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Not it would only add health when enemy health changes. Guess you probably would want a check to make sure the old health is higher otherwise the difference would be negative.

    Other than that, no, there are no such conditions other than saving the old health and checking if it’s different than the current health.

  • Awesome. It works.

    Thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)