• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How to create a dialog Health Bar

Level 17
Joined
Jun 9, 2007
Messages
2,493
Read before starting
In this guide i will teach you how to create a dialog, or to be more specific, a health bar.
This guide will step by step tell you what to do in order to create a Health Bar (Picture Below)
Healthbar2[1][1].pngHealthbar1[1][1].png
Before you read this guide, you should already know how to work with the trigger editor, i will not explain how to create triggers, variables, etc.
Note: If the width of an dialog item is 0, it will set the width to "infinite", so that means that if a unit have 0 health, the bar will go all the way to the edge of the dialog.
An easy way to solve this is to add a "+ 1" to the width calculations, that way it will never reach 0 in width and you wont have to worry about this.
Also keep in mind that this system gives you an exact value of the units health, mana, shield, vitality, or whatever you decided to choose.
This is a picture to show what i mean:
ExactValue[1][2].png
If you want to change this so that it only shows a clean number, set "Any Precision" in the Update triggers text actions to 0. This will set the amount of decimals used to 0.

Okay, so lets get started.

1. Creating the dialog

a) Create a trigger, i decided to name it "Unit Bar UI ini"

b)Set the event in your newly created trigger to Map initialization

c)Create the dialog by using the action: Create Dialog
You will now get this:
  • Dialog - Create a Modal dialog of size (500, 400) at (0, 0) relative to Center of screen
You can choose if you want the dialog to be modal or Non-modal, i have yet to find any difference between them.
in the dialog size the first value is the width of the dialog, and the second value is the height. This may get quite confusing later, as the actual frame of the dialog counts toward the total size, so you will have to add that into your calculations.
at (0, 0) you choose the position of the dialog, relative to X of the screen. the first value is the X position of the dialog, and the second value is the Y position of the dialog. X is from what point of the screen (0, 0) will be.

d)Create a global variable of the type Dialog for your newly created dialog so that it can be modified later on. I will call it" Unit Bar UI Dialog"

e)Set the variable to your newly created dialog by going back to your trigger and add this action: Set Variable
You will now get this line:
  • Variable - Set Variable = Value
Set Variable to your newly created variable and set Value to Last Created Dialog.
Like this
  • Dialog - Set Unit Bar UI Dialog = (Last Created Dialog)
You have now created your very own dialog and it should look something like this
  • Unit Bar UI ini
    • Events
      • Game - Map initialization
    • Local Variables
    • Conditions
    • Actions
      • Dialog - Create a Modal dialog of size (500, 400) at (15, 15) relative to Top Left of screen
      • Variable - Set Unit Bar UI Dialog = (Last created dialog)
2. Adding dialog items to your dialog

Create your own health bar. (This is the hardest part of the tutorial)

a) Create a dialog image by using the action: Create Dialog Item (Image). You will get this:
  • Dialog - Create an Image for Dialog (Last created dialog) with the dimensions (200, 50) anchored to Top Left with an offset of (50, 50) setting the tooltip to "" using the image Assets\Textures\white32.dds as a Normal type with tiled set to true tint color White and blend mode Normal
You should already know how dimensions work by now as i explained how it works in the 1st part.
Something that is worth mentioning is that you anchor the object relative to the dialog this time, and not the entire screen.
Offset is already explained in the 1st part aswell.
The tooltip is what message you want to appear in a small box when you mouse-over the dialog item.
So if you set it to Health Bar, it will say "Health Bar" when you mouse-over the item in-game.
The image path is the image that you will use for the object. I will use this image: Assets\Textures\ui_loadingbarsegment_center.dds
and for the type, just choose normal as i wont explain how the others work, atleast now not.
And for the tiled set options, just ignore them, they have no effect on the normal type that we use.

b) Create a global variable of the type Dialog Item and set it to the dialog item you just created.

Now we want a string on the bar that display your health like this: 150/150

c) First create a dialog object of the type label. You will get this:
  • Dialog - Create a Label for Dialog (Last created dialog) with the dimensions (200, 50) anchored to Top Left with an offset of (50, 50) with the text Text color set to White text writeout set to false with a writeout duration of 2.0
Set the text to anythinng you want, it does not matter. This is becouse we will have a trigger that updates the text constantly in order to make it accurate. You can just set it to nothing if you want.
Do not use the writeout option. It will make the characters appear one by one, and we do not want that. So set it to false. You can just ignore the duration, as it wont do anything with writeout disabled.

d) Create a global variable and set it to the dialog item that we just created, so that we can change the text later.

We have now finished the layout of the dialog and the ini trigger is done.

3. Keeping the Health Updated
In order to do this we need a unit.
This part is almost only about math, so you may get stuck on this part if you have problems with math.

a) We want to keep the health updated, so create a new trigger, and use the event:
  • Events
    • Unit - Any Unit Life changes
b) Add the action "Set Dialog Item Text". You will get this:
  • Dialog - Set (Last created dialog item) text to Text for (All players)
Change Last created dialog item to the variable that you connected your dialog item (text) to.
Now click on Text and choose "Combine Text Multiple".
When you select your action, there will appear a small box below the trigger box. It should appear like this:
  • Set Dialog Item Text
    • Dialog Item: Unit Bar Health Text
    • Text: Combine Text Multiple
      • Text <Text+>
    • Players: All Players
Click on Text <Text+> and go to functions and choose "Convert Real to Text".
Now change "Players: All Players" to convert player to player group, then owner of triggering unit and then triggering unit.
The box will now appear like this:
  • Set Dialog Item Text
    • Dialog Item: Unit Bar Health Text
    • Text: Combine Text Multiple
      • Text <Text+>
        • Text: Convert Real To Text
          • Value: 0.0
          • Precision: Any Precision
    • Players: Convert Player To Player Group
      • Player: Owner Of Unit
        • Unit: Triggering Unit
Click on Value: 0.0 and choose the function "Unit Property"
Now the box will change again. Click on "Unit: Triggering Unit" and choose the unit you want.

Now click on "Text <Text+>" again and simply enter a / and then press OK.

go click "Text <Text+>" for the last time and choose the function Convert Real to Text.
Click on Value: 0.0 and once again choose the function "Unit Property".
Now set "Unit: Triggering Unit" to the unit you choosed before.
Change "Property: Life" to "Property: Maximum Life".

It should now look like this:
  • Set Dialog Item Text
    • Dialog Item: Unit Bar Health Text
    • Text: Combine Text Multiple
      • Text <Text+>
        • Text: Convert Real To Text
          • Value: Unit Property
            • Unit: Triggering Unit
            • Property: Life
            • Current: Current
          • Precision: Any Precision
        • Text: "/"
        • Text: Convert Real To Text
          • Value: Unit Property
            • Unit: Triggering Unit
            • Property: Maximum Life
            • Current: Current
          • Precision: Any Precision
    • Players: Players: Convert Player To Player Group
      • Player: Owner Of Unit
        • Unit: Triggering Unit
And there you go, you have now finished the health text.

Now for the Actual Health Bar.

c) Add the action "Set Dialog Item Size" It should now appear like this:
  • Dialog - Set (Last created dialog item) size to (200, 50) for (Player group((Owner of (Triggering unit))))
Set Last Created Dialog Item to the variable that you connected your dialog item health bar to.
Set Height to the function "Dialog Item Height" and change "Used dialog item" too the health bar dialog item.
Set the Width to the function "Arithmetic Multiple (Integer)".

Now check the box below the trigger and you should see something like this:
  • Set Dialog Item Size
    • Dialog Item: Unit Bar Health Bar
    • Width: Arithmetic Multiple (Integer)
      • Operator: +
      • Values <Integer+>
    • Height: Dialog Item Height
      • Dialog Item: Used Dialog Item
      • Player: 1
    • Players: All Players
Change "Operator: +" to "Operator: /".
Now click on "Values <Integer+>" and set it to function "Arithmetic Multiple (Integer)"
Change the operator below to "Operator: *".
Click on "Values <Integer+>" below the Operator and set it to your health bars default width. For me its 280.
Click on "Values <Integer+>" once again and this time choose "Convert Real to Integer".
Change "Value: 0.0" to the function Unit Property and set Unit to your choosen unit.
Now change the Property to "Property: Life (Percent)" and you are done.
Go up to the "Values <Integer+>" at the very top and click on it. Set the value to 100.
Now change "Players: All Players" to convert player to player group, then owner of triggering unit and then triggering unit.

The box should now look something like this:
  • Set Dialog Item Size
    • Dialog Item: Unit Bar Health Bar
    • Width: Arithmetic Multiple (Integer)
      • Operator: /
      • Values <Integer+>
        • Values: Arithmetic Multiple (Integer)
          • Operator: *
          • Values <Integer+>
            • Values: 280
            • Values: Convert Real To Integer
              • Value: Unit Property
                • Unit: Triggering Unit
                • Property: Life (Percent)
                • Current: Current
        • Values: 100
    • Height: Dialog Item Height
      • Dialog Item: Unit Bar Health Bar
      • Player: 1
    • Players: Convert Player To Player Group
      • Player: Owner Of Unit
        • Unit: Triggering Unit
You are now done! Just create a trigger to make the dialog show up and it should display properly.

This is what my ini trigger looks like:
  • Unit Bar UI ini
    • Events
      • Game - Map initialization
    • Local Variables
    • Conditions
    • Actions
      • Dialog - Create a Modal dialog of size (350, 85) at (15, 15) relative to Top Left of screen
      • Variable - Set Unit Bar UI Dialog = (Last created dialog)
      • Dialog - Create an Image for Dialog (Last created dialog) with the dimensions (280, 32) anchored to Top Left with an offset of (34, 21) setting the tooltip to "" using the image Assets\Textures\ui_loadingbarsegment_center.dds as a Normal type with tiled set to true tint color White and blend mode Normal
      • Variable - Set Unit Bar Health Bar = (Last created dialog item)
      • Dialog - Create a Label for Dialog (Last created dialog) with the dimensions (200, 50) anchored to Top Left with an offset of (120, 27) with the text "150/150" color set to Black text writeout set to false with a writeout duration of 2.0
      • Variable - Set Unit Bar Health Text = (Last created dialog item)
      • Dialog - Show Unit Bar UI Dialog for (All players)
And this is my update trigger:
  • Unit Bar Update
    • Events
      • Unit - Any Unit Life changes
    • Local Variables
    • Conditions
    • Actions
      • Dialog - Set Unit Bar Health Text text to (Combine ((Text(((Triggering unit) Life (Current))) with 0 decimal places), "/", (Text(((Triggering unit) Maximum Life (Current))) with 0 decimal places))) for (Player group((Owner of (Triggering unit))))
      • Dialog - Set Unit Bar Health Bar size to ((/ ((* (280, (Integer(((Triggering unit) Life (Percent) (Current)))))), 100)), (Height of Unit Bar Health Bar for (Owner of (Triggering unit)))) for (Player group((Owner of (Triggering unit))))
 
Last edited:
Level 10
Joined
Nov 28, 2008
Messages
655
  • BossBar Do 2
    • Events
      • Timer - Every 0.05 seconds of Game Time
    • Local Variables
      • CurrentHealth = 0.0 <Real>
      • DefaultHealth = 0.0 <Real>
      • Ratio = 0.0 <Real>
      • Width = 0.0 <Real>
      • Untitled Variable 001 = 0 <Integer>
    • Conditions
    • Actions
      • General - For each integer Untitled Variable 001 from 0 to 16 with increment 1, do (Actions)
        • Actions
          • Variable - Set CurrentHealth = (SelectedUnits[Untitled Variable 001] Life (Current))
          • Variable - Set DefaultHealth = (SelectedUnits[Untitled Variable 001] Life (Default))
          • Variable - Set DefaultHealth = (DefaultHealth + 1.0)
          • Variable - Set Ratio = (CurrentHealth / DefaultHealth)
          • Variable - Set Width = (210.0 * Ratio)
          • Dialog - Set Bar size to ((Integer(Width)), 20) for (Player group(Untitled Variable 001))
That is my solution that I am using, if LTGH has a better solution, please post it.
 
Level 17
Joined
Jun 9, 2007
Messages
2,493
You could simply have a loop that takes 0 to 15 and then insted of choosing player, you take picked integer.

This is an easy progress if you have experience with the trigger editor, and i mentioned in the guide that you already should know how to use the trigger editor :)

Edit:
Seems like tagg1080 was first, but you should change 0 to 16. It should be 0 to 15. You will get an error otherwise as there is no player 16. That would give you a total of 17 players.
Also you do not have to take integers from a variable, there is an action to simply create a loop that takes number X to Y.
 
Level 17
Joined
Jun 9, 2007
Messages
2,493
Yeah you could add that to your tutorial :) Also maybe some texture paths for a energy or shields bar would be nice :)

I checked trough all the texture files once, and that is the only texture i can remember that actually look like a bar.
So i took print screen of the preview, cutted it out. Changed its width to 1 to decrease its file size. Made it black & white, and then changed the colors with photoshop. (you can use Gimp or other image editing programs aswell)

The result is a nice bar, but it only takes up about 100 bytes when compressed xD
 
Level 17
Joined
Jun 9, 2007
Messages
2,493
And why the hell 0.02 and 0.05s intervals, which are not going to work as both 0.02 and 0.05 convert into 0.0625 s intervals.

Okay... I am sorry.. This guide is infact outdated! i will update it later.
I do not even understand why i have a timer at all.
I use this event myself:
  • Unit - Any Unit Life changes
I will update the first post.

Edit:
I have updated the post now.
 
Level 7
Joined
Sep 24, 2008
Messages
281
For some reason my text size remains the same, no matter how big its box is. How can I fix this?
 
Level 17
Joined
Jun 9, 2007
Messages
2,493
For some reason my text size remains the same, no matter how big its box is. How can I fix this?

Do you mean the font size of the text or the length?
i guess you mean the font size. And i do not think the box size is related to the font size. Though, there is almost always an option to use different text types when you write a text. Almost.
So you should not even have to change the size of anything, just change the text style (cant remember what the thing is named) when you are at the field where you enter the text.
 
Level 7
Joined
Sep 24, 2008
Messages
281
Do you mean the font size of the text or the length?
i guess you mean the font size. And i do not think the box size is related to the font size. Though, there is almost always an option to use different text types when you write a text. Almost.
So you should not even have to change the size of anything, just change the text style (cant remember what the thing is named) when you are at the field where you enter the text.

Alright, I'll try that out and get back to you in a moment. Thanks :thumbs_up:
 
Top