How do I reduce variable usage for health bars

Get help using Construct 2

Post » Fri Aug 07, 2015 3:31 pm

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)
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
43
S
14
G
12
Posts: 626
Reputation: 9,455

Post » Fri Aug 07, 2015 4:09 pm

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.
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Fri Aug 07, 2015 4:13 pm

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))
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
43
S
14
G
12
Posts: 626
Reputation: 9,455

Post » Fri Aug 07, 2015 4:59 pm

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.
B
99
S
35
G
20
Posts: 438
Reputation: 17,910

Post » Fri Aug 07, 2015 5:18 pm

exactly. that's what i was thinking because of lot of types. thnx :)
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
43
S
14
G
12
Posts: 626
Reputation: 9,455


Return to How do I....?

Who is online

Users browsing this forum: WesleyDeveloper9 and 6 guests