Make text object dynamically change color based on Hp

Hi :slightly_smiling_face:
I have 2 scene variables for hp Head and “Variable(Enemy.HP.HeadMax)” and “Variable(Enemy.HP.Head)” .
Damage is subtracted from Variable(Enemy.HP.Head) and I use Variable(Enemy.HP.HeadMax) to know when to stop regenerating .
I want text object Head to dynamically change color from green to orange to red and when Variable(Enemy.HP.Head) = 0 to be black.

The way it’s currently set-up:
If scene variable Enemy.HP.Head is less then 60% of max hp, the text object “EnemyHead” color changes to orange.
if scene variable Enemy.HP.Head is less then 30% of max hp, the text object “EnemyHead” color changes to Red.
If scene variable Enemy.HP.Head = 0, the text object “EnemyHead” color changes to Black.

So what happens at the moment? You describe what you’d like it to do, but not what symptoms it currently exhibits.

Also, your events in the screen snippet don’t show any of the variables you mention in your post. I suggest you change your events to match your post, or screenshot the events that do.

If scene variable Enemy.HP.Head is less then 60% of max hp, the text object “EnemyHead” color changes to orange.
if scene variable Enemy.HP.Head is less then 30% of max hp, the text object “EnemyHead” color changes to Red.
If scene variable Enemy.HP.Head = 0, the text object “EnemyHead” color changes to Black.

I have no idea what you’re talking about.

Is this what you want to happen, or what is happening?

Because if this is what is happening, then what is the problem?

I’m just trying to confirm I understand the issue.

This is what’s happening.
I want the colors to gradually change from green to orange, orange to red, red to black

Ok. Let see if I got this right - you want the colour to initially be green. As the Head value decreases, you want the colour to slowly change too.

At the moment, you’ve got thresholds (60%, 30% and 0%), at which the colour changes. Abruptly. You’ve not told it to gradually merge from one to the other.

There are a few ways to achieve this. I’ll give you two:

The first one will change from green to dark red (sorry, no black, it’s an existing routine I’ve already got). the events are as follows:

The second one is to use lerp, and lerp from one colour to the next based which threshold it falls under. Something along the lines of:

Create a scene variable to hold the % Enemy.HP.Head is of Max HP (I’ll refer to is as headPercent)

If scene variable headPercent is greater than or equal to 60%, the text object “EnemyHead” color changes to

ToString(lerp(0, 255, (Variable(headPercent) - 60) * 2.5)) + ";" + ToString(lerp(255, 160, (Variable(headPercent) - 60) * 2.5)) + ";0".


If scene variable headPercent is greater then 30% and less than 60%, the text object “EnemyHead” color changes to

"255;" + ToString(lerp(160, 0, (Variable(headPercent) - 30) * 3.3)) + ";0"


If scene variable Enemy.HP.Head is less then or equal to 30% , the text object “EnemyHead” color changes to

ToString(lerp(255, 0, Variable(headPercent) * 3.3)) + ";0;0"

I think those are the right numbers.

I don’t understand this part.
What do I put as the value for Variable(headPercent) ?

Do you mean something like
Variable(headPercent) set to Enemy.HP.Head/(Max HP/100)

Ex.
Max HP=200
Enemy.HP.Head=130
130/(200/100)=130/2=65
Variable(headPercent)=65

?

I have no idea what numbers I’m supposed to add. :cold_sweat:
I don’t know what does numbers refer to.

I tried setting them to 0, but it didn’t work.

Try Variable(headPercent) = Enemy.HP.Head/Max HP

And change the ‘- 60’ and ‘- 30’ to ‘- 0.6’ and ‘-0.3’. The 3rd lerp parameter should be between 0 & 1.0

In the last screenshot, you’ve got more closing parenthesis than opening ones. You need an opening bracket before the Variable keyword.

Colour is set as a string, made up of 3 components red, green & blue, which are values that range from 0 to 255. We’re trying to slide from one colour to the next by changing these components using lerp. I suggest you look up lerp and how it works to understand what we’re doing to each component.

The calculation involving headPercent is to get the range (60%-100%, 30%-60% and 0%-30%) into a value from 0 to 1. So, for example, a headPercent of 80% is halfway in the first range (60%-100%), so should end up as 0.5 (halfway) as the 3rd lerp parameter. A headPercent of 50% is 2/3 of the way from 30% TO 60%, so it should end up around 0.66.

Is this correct?
ToString(lerp(0,255,(Variable(Enemy.HP.HeadPercen)-0.6)*2.5))+";"+ToString(lerp(255,160,(Variable(Enemy.HP.HeadPercen)-0.6)*2.5))+";0;0"

Not quite - you have one too many zeros at the end. And check the variable name (it’s missing a ‘t’) :slight_smile:

Another (and less complex/convoluted) method you could try is to break the ranges into lots of 10%, and set the colours accordingly. It won’t give a super smooth transition, but will provide a reasonable change.

What I mean by that is calculate headPercent, and have 10 events with the conditions:

if (headPercent >= 0.9), set colour green

if (headPercent >= 0.8 and headPercent < 0.9), set colour slightly orange green

if (headPercent >= 0.7 and headPercent < 0.8), set colour slightly green orange

if (headPercent >= 0.6 and headPercent < 0.7), set colour orange

if (headPercent >= 0.5 and headPercent < 0.6), set colour slightly red orange

And so on. This may be easier to code for now.

Its missing in the original variable name l’ll change it

Pleas tell me I got it right. :sweat_smile:
The first one is between 100% and 60%
The second one is between 60% and 30%
The third is between 30% and 0%