How do I manage multiple health bars of different enemies within a family?

0 favourites
  • 7 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • So its been hours of fruitless research but heres a quick rundown. I have Enemy1 and Enemy2 in a Family with each having different MaxHP & currentHP Instant Variables attached. I have a EnemyHP progress bar. I enemies spawn and the yellow points seen in the preview and the enemies themselves spawn a HP progress bar. Only the progress bar for Enemy2 shows. Attached is my event sheet and a screenshot of when i preview.

    https://imgur.com/gallery/LErIyjM

    https://imgur.com/gallery/RUW3l84

    I have attempted to use containers but it seems i can place the EnemyHP progress bar into a container more than once. UIDs are giving problems for me as well. I have a mix and match of all past post solutions so thing might have gotten a little messy.

    The end goal is to have a HP progress bar hover over each enemy that spawns that track the currentHP variable of the enemy that spawned it.

    THanks for the help in advance,

    -Pino

  • Well one issue I can see is that you haven't made use of the family. The whole point of putting enemies in a family is that you don't have to duplicate code, which you have done. Your code should apply to all enemies so there is definitely some redundant code in there. The health bars at least should be in code like on Enemies created, spawn healthbar. Enemies set healthbar to enemies.hp.

    Anyway the actual bug, it's because your set position for the health bar is in its own event and it does the last one on the event sheet, so every tick it is setting the health bar to the position of enemy 2. The set position events for the health bar do not relate to anything and are independent events. The one above for enemy 1 is ignored, you won't see anything happen for that, and if you look in debug mode you will probably see several health bars in that enemy 2 location overlapping each other.

    As for resolving the issue, if you reduce the code as stated above to on Enemies created, spawn healthbar. You can then pin it to the Enemies position at that point and not every tick.

  • Well one issue I can see is that you haven't made use of the family. The whole point of putting enemies in a family is that you don't have to duplicate code, which you have done. Your code should apply to all enemies so there is definitely some redundant code in there. The health bars at least should be in code like on Enemies created, spawn healthbar. Enemies set healthbar to enemies.hp.

    Anyway the actual bug, it's because your set position for the health bar is in its own event and it does the last one on the event sheet, so every tick it is setting the health bar to the position of enemy 2. The set position events for the health bar do not relate to anything and are independent events. The one above for enemy 1 is ignored, you won't see anything happen for that, and if you look in debug mode you will probably see several health bars in that enemy 2 location overlapping each other.

    As for resolving the issue, if you reduce the code as stated above to on Enemies created, spawn healthbar. You can then pin it to the Enemies position at that point and not every tick.

    Hey thanks for the awesome help. Alright i managed to get the family variables down and came up with rough logic. Here is the event sheet excrept and and shoot of what the preview shows. The 'white' enemy has a higher HP cap but seems to be using the HP of the 'red' enemy and the HP progress bar of the 'red' enemies dosnt go down when a bullet collides with it. Once one 'red' enemy dies, all HP progress bars are destroyed.

    https://imgur.com/gallery/qKMxR6L

    https://imgur.com/gallery/FHB9Bgq

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Assuming playerprojectiles is the bullet hitting the enemy, the event should be Playerprojectiles on collision with enemy, so you can pick the projectile, then destroy it. That should also fix the hp problems as projectile on collision with enemy will pick that enemy and subtract damage.

    Although family logic can be used to reduce some code such as 'create healthbar for red' 'create healthbar for white' etc, you can still split out some code to define things for different types. To do this, create a family instance variable 'type' and then you can assign all stats to red type and all stats to white type on created. So if type=white it will have the appropriate health.

  • Assuming playerprojectiles is the bullet hitting the enemy, the event should be Playerprojectiles on collision with enemy, so you can pick the projectile, then destroy it. That should also fix the hp problems as projectile on collision with enemy will pick that enemy and subtract damage.

    Although family logic can be used to reduce some code such as 'create healthbar for red' 'create healthbar for white' etc, you can still split out some code to define things for different types. To do this, create a family instance variable 'type' and then you can assign all stats to red type and all stats to white type on created. So if type=white it will have the appropriate health.

    Alright each enemy is spawning with their own status but the HP bars are still destroyed after one enemy dies. I have it set up as On created, Spawn HPbar, Set position of HPbar to were i want it above the enemy and pin it. I think i need to mess with a 'For Each' or UIDs. I will definitely need help as its just trial and error at this point.

  • For health bars you can put it in a container with the enemy. It gets rid of the create health bar logic, instead it will be automatically created although you will still need to set position, and when you destroy the enemy on hp 0, health bar will go with it automatically as well.

  • I found a good solution. I made a separate Progress Bar for each type of enemy sprite and put them in a container. Such as red_enemy had a container with red_enemy_HPbar (progress bar) inside. then i just remade a few lines of code that was supposed to be intended for the entire enemy family but apparently you cant add containers for families. Here is an excerpt. Thanks for the help!!

    https://imgur.com/a/BwgSzdI

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