YesNoOk
avatar

Expressions using Life/LifeMax not calculating correctly (Read 993 times)

Started by Minakata Moriya, August 31, 2020, 10:13:01 PM
Share this topic:
Expressions using Life/LifeMax not calculating correctly
#1  August 31, 2020, 10:13:01 PM
  • *
    • USA
I'm attempting to set a variable as a timer, determined by how full the player's life is at the time of triggering.

The max value is 1280, assuming the player has almost no health at the time.

[State 780, Activate]
type = VarSet
trigger1 = animelem = 4
v = 10
value = 1280 * floor(life / lifemax)

This works just fine if the player has taken no damage. I did some Displaytoclipboard fiddling to see what the expression is returning, and it's always int 1 if life = 1000, and 0 if it's anything else. I tried using VarSet to just pull the values to do the calculation elsewhere, and the behavior is the same. Even if I just hard-code 1000 instead of lifemax, it still returns a boolean value. Am I missing something, or is this a bug? Presumably the former, but one never knows.
Last Edit: September 01, 2020, 01:02:02 AM by Minakata Moriya
Re: Expressions using Life/LifeMax not calculating correctly
#2  August 31, 2020, 10:54:16 PM
  • *
    • Mexico
    • Reptorebanador@gmail.com
well this is because you're using floor when calculating life/lifemax, so basically any value lower than 1 will always become 0 so your math will also become 0

your code for value basically says "1280 * round down(1000/1000)" when at lifemax, but if that life valñue goes down by even 1, 999/1000 = 0.999, rounded down would be 0.

What you want to do is change it so the floor (or ceil) is applied after the miltiplication, so, tl;dr:

change this

Code:
value = 1280 * floor(life / lifemax)

to this:

Code:
value = floor(1280 *(life / lifemax))

hope that helps
Re: Expressions using Life/LifeMax not calculating correctly
#3  August 31, 2020, 11:35:25 PM
  • *
    • USA
Same result, I'm afraid. :/

I put "floor(1280 *(life / lifemax))" as a param in DisplayToClipboard, and it shows 1280 as long as life is full, and it immediately drops to zero as soon as a single hit is taken.

Similarly, the timer variable gets set to 0 if not at full life. I tried using ceil() instead of floor() with the same result, and just "1280 *(life / lifemax" didn't make a difference. It's all or nothing.
Re: Expressions using Life/LifeMax not calculating correctly
#4  September 01, 2020, 12:38:30 AM
  • *
    • Mexico
    • Reptorebanador@gmail.com
Well that would have worked in theory, but I did test it and apparently mugen likes doing things in a weird way so it truncates the value when doing a division so you only get an int, fortunatly there's more than one way to do stuff in math, this may notbe the most elegant solution but it should acomplish what you need:

Code:
(life%lifemax)*0.001*1280

that should do what you were trying to do with that original code right there, may need a few tweaks but a quick testing shows it doing the job, so hopefully it can help you
Re: Expressions using Life/LifeMax not calculating correctly
#5  September 01, 2020, 01:01:44 AM
  • *
    • USA
That pushed me much further along the way. I did find that it was due to MUGEN trying to force an integer when I tried dividing by 1000.0 for the hell of it.

Since the value is meant to be inversely proportional to the life, this is what worked.

value = ceil(1280 * abs(((life%lifemax)* 0.001)-1))

It's ugly, but it does the trick. Thanks for your help!
Re: Expressions using Life/LifeMax not calculating correctly
#6  September 01, 2020, 01:26:50 AM
  • *
    • Mexico
    • Reptorebanador@gmail.com
yeah I was just trying to get the original code working :p, anyway, glad it helped and you got it working
Re: Expressions using Life/LifeMax not calculating correctly
#7  September 02, 2020, 11:47:55 PM
  • ****
    • USA
    • https://twitter.com/inktrebuchet
floor(life / lifemax) would always be 0, unless life = lifemax.

maybe you meant (lifemax/life)
Re: Expressions using Life/LifeMax not calculating correctly
#8  September 19, 2020, 09:47:11 AM
  • avatar
  • *
    • Vietnam
convert life and lifemax to floating-point data type In your case it should like this:
[State 780, Activate]
type = VarSet
trigger1 = animelem = 4
v = 10
value = floor(1280*(life*1.0/lifemax*1.0))

Since there was a same problem while u use 'enemy,life' trigger so u need to convert ;Life' or anything related with 'life' trigger(lifemax/enemy,life/enemy,lifemax) to floating-point data type before u used it. and also if u want to store it in integer variable like var(10) of your so u need to round down it, otherwise u dont have to.
------Tremble Mortal and Despair. Doom has come to this world------