# How do I reduce variable usage for health bars

0 favourites
• 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
• hi guys,

now there's a problem i'm dealing with. i have a health bar of size 100x20 (example)

and i have 3-4 types of enemies with different max HPs. (let's say 100,200,300).

if i want to show their health thorugh that health bar (with keeping the health bar size same) i have to calculate it's scale.

example - 100 = 100 so no need for calcs, 200!=100 so i have to do (example, enemy has 150 hp out of 200 max):

150*100/200 which is 75 and then i show 75/100.

now the problem is here - i need to have a variable MAXHP (200) and variable currentHP (150) to calculate how much is that out of 100pixels. since i know the size of hp bar i don't need that var (100).

this is the question - can i get rid of the 2nd variable (maxhp) and how can i calculate scale then?

(i want to get rid of that var, because every instance then has that var, and i gotta put it by instance because many types of enemy)

• put all enemies in a family and make those variables instance variables of the family, then everyone will inherit the default MaxHP. You can override individual enemies if you want to make them stronger or weaker.

But you do need both variables to calculate what percentage dead or alive they are.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• yet again if you put them in family - each instance has that variable.

i thought so. are you sure there isn't a way around this? (except putting each enemy to 100 and calculating differently for every enemy on hit XD (that's it XDDD))

• Yeah, either you have to assume what the max is (100), or you need to store what the max is.

You could have a table (array) that holds the max for each type of unit / enemy rather than store the max in each instance, but then you have to store the unit type in each instance so you can look it up in the table - so you aren't really saving anything, unless there is other data you want to define for each type as well - size, weight, weapons, abilities, vulnerabilities, etc. Then it would save on variables if you store just the type, and then look up all those other attributes (including MaxHP) based on the type, rather than having every unit duplicate all info that is the same.

• exactly. that's what i was thinking because of lot of types. thnx

• 5 posts