problem with health bar

jackhammer1000jackhammer1000 Member Posts: 7
edited August 2014 in Working with GS (PC)

Hello.
im new around here and im trying to learn some stuff around GS.
im having a little issue. i have in a level a spawer who spawns several regular enemies every second.
when an actor spawns in it will also spawn a health bar right above it.
i managed to constrain the health bar's width to the enemy's health points but because there are several enemies (of the same type) when i shoot one enemy, the health bar of every enemy will change.
also there is a timer that destroys the enemy after 3.5sec so i had to put a similar timer on the health bar to destroy it as well. in reallity what happens is that if i shoot an enemy a couple of times (without destroying it) and wait more than 3.5sec, the health bar of every enemy will regenerate to full (because in that time the spawner spawns new enemies with new health bars)

i guess what im trying to ask here is how do i seperate each health to be constrained to its own enemy?
thanks in advance

Comments

  • colandercolander Member Posts: 1,610
    edited August 2014

    If your enemies are not spawning at the same time try this;

    1. Create a game level integer attribute call it something like enemyCount and set it to 1

    2. Create a self integer attribute called ID in both your enemy and health bar actors

    3. In your enemy put a change attribute self.ID To game.enemyCount and then spawn your health bar below it i.e. every enemy spawns it's own health bar.

    4. In the health bar actor put two change attributes first one self.ID To game.enemyCount second one to game.enemyCount To game.enemyCount + 1 this will give each enemy and it's health bar the same unique ID

    5. Use this ID as a row reference in a table to swap the damage information between each enemy to it's health bar.

    This will point you in a direction (There is probably other ways to do this) but if you spawn enemies at the same time it will not work. You could use the enemyCount attribute to keep the spawning sequential and eliminate the possibility of two enemies spawning at the same time.

  • jackhammer1000jackhammer1000 Member Posts: 7

    thats a nice trick. thank you.
    i didnt understand the 5th point though... how exactly can i use a table here?

  • colandercolander Member Posts: 1,610

    When the enemy is hit it suffers damage which the health bar needs to display the health of the enemy. Use the self.ID as the row number for a "Change Table Value" action which will record it to a table when the enemy is hit. The health bar value can be constrained to the same row using it's self.ID because it is the same number it will read the same cell.

    Every time the enemy suffers damage only the health bar constrained to that enemy will be updated. Also use the "Copy Table" action to make a copy of a master table for each level.

  • jackhammer1000jackhammer1000 Member Posts: 7
    edited August 2014

    got it.
    im starting to understand the use of tables. thanks.

    another question:
    is it possible to change the amount of rows in a table as an action within the game?
    the problem is that if i have 3 rows in the table then i can spawn 3 enemies. if i spawn the 4th he will be exploded (since there is no value in the 4th row, his health will be 0)

    i want the spawner to add another row to the table with certain values and only then to spawn another enemy. is this even possible?
    and how about using a function in a table cell? is it possible?

  • colandercolander Member Posts: 1,610

    @jackhammer1000 said:
    got it.
    im starting to understand the use of tables. thanks.

    another question:
    is it possible to change the amount of rows in a table as an action within the game?
    the problem is that if i have 3 rows in the table then i can spawn 3 enemies. if i spawn the 4th he will be exploded (since there is no value in the 4th row, his health will be 0)

    i want the spawner to add another row to the table with certain values and only then to spawn another enemy. is this even possible?

    This is only available in the Nightly Builds which only Pro users have access to. It will eventually find its way into a new release. You could just make the table way bigger than you need so a game level could never possibly spawn that many enemies. Or you if you know the enemies won't last long recycle the table i.e go back row 1 and overwrite the old enemy data.

  • jackhammer1000jackhammer1000 Member Posts: 7

    my regular enemies destroys themselves after 3 or 4 sec or after crossing certain position in the game where the player cant hit them anymore (havent decided which method to use yet). so i guess i could reset the enemy count attribute after 30-40 sec so the table will be recycled.

    im facing with another issue (im not sure if its ok to ask everything here or should i open a new thread for each question?)...
    i want the health bar to move with their respective enemies. i tried having the enemy actor "upload" its X and Y into 2 cells in a table but you cant do that... the constrain attribute doesnt allow you to constrain a table cell to an attribute but only an attribute to a table cell (which is kinda weird).
    i though maybe i'll put a timer every 0 sec "change table value" to self.position.x
    but im not sure how good is this idea performance-wise...
    any thoughts?

    again, thanks for the help

  • colandercolander Member Posts: 1,610

    You could create a bunch of game level attributes and recycle them. You need to be careful of over using constrain it will cause your game to stutter if you have too many of them.

    I have been thinking more about this and a better way would be to change the image. Draw separate images of your enemy character including a health bar for each level of damage. Number and name them accordingly and constrain the image of the damage level each time it changes using the expression editor.

    For example if image name is 2_EnemyDamage.png (Which shows a damage level of 2) use your damage attribute in the expression editor with the rest of the name i.e. Use a Constrain Attribute in the enemy actor self.Image To game.EmemyDamage.."_EnemyDamage.png".

    This way you don't have to spawn a health bar, create a table, etc it can all be done with an image change.

  • colandercolander Member Posts: 1,610

    @jackhammer1000 I saw your solution in the thread "Spare code dump it here" it would be good to put it here also. That way anyone who has searched and found this thread looking for a solution in the future will see it.

  • jackhammer1000jackhammer1000 Member Posts: 7
    edited August 2014

    yeah you are right. im gonna paste it here.

    Hello. im new around here and i started to create a simple game (untill i level up my skills) and found myself facing an issue. i needed to create a health bar that will be shrinking from one side only i.e. aligned to the left. i found some solutions and videos on this matter, however, they all refer to a static health bar. it didnt work for me because my health bar was dinamically moving along with its corresponding enemy. and since this health bar was also spawn dinamically i couldnt go into its backstage and do changes to it. i dont know if this method is the best but since it took me quite some time to figure out the formula i thought mybe i'll share it here.

    so first i created a table with 2 columns (health, positionX,) next, i set a timer on the enemy actor "every 0 sec" change table value "table1.positionX" to self.position.x (i.e. report to the table its current X position)

    in the health bar actor i used a constrain "self.size.width" to "table1.health" and another constrain "self.position.x" to a formula that goes like this:

    table1.positionX-(((table1.positionX-table1.health/2)+100/2)-table1.positionX)

    a little explanation (think of the formula as A-(((B-C/2)+100/2)-D):

    A is table1.positionX (constantly changing by the enemy actor).

    the "minus" after A is only because my enemies are moving from right to left on the screen. you will need to adjust that according to your enemies' movement.

    B-C is the same current position subtracted by half of the current health.

    100 is the initial health for this enemy (so if you have a different value for your enemy you need to change this or you can use a global attribute).

    and D is again the positionX from the table.

    this will result in a moving health bar (according to its corresponding enemy actor) by aligning it to the center of the enemy minus 5 pixeles for each hit, half the damage each hit causes, and will shrink from the right. i hope i didnt make it too complicated.

    you will also need to do another one exactly like that for the Y position.

    good day.

Sign In or Register to comment.