# Health Bar

## Introduction

Health bars are common UI elements in games. They are similar to progress bars, so this example could be used to create either. This example explains how to create a health bar component.

## Creating the Component

First we'll define the component:

1. Open Gum
2. Open or create a new Gum project
3. Right-click on the **Components** folder
4. Name the component "HealthBar"
5. Resize the **HealthBar** component so it is wider than it is tall. For example, assign `Width` to `200` and `Height` to `32`.

## Adding a Background

Next we'll add a background to our **HealthBar** Component

1. Drag+drop a **ColoredRectangle** into the **HealthBar**\\

   <figure><img src="/files/kqA0Zx8Z0KYl0Kh7u9mj" alt=""><figcaption><p>Add ColoredRectangle to HealthBar</p></figcaption></figure>
2. Select the newly-created **ColoredRectangleInstance**
3. Select the **Alignment** tab
4. Click the **Fill Dock** button\\

   <figure><img src="/files/yj5JZGB8I1B3odT7k6pz" alt=""><figcaption><p>Fill Dock to make the background take up the entire size of its parent HealthBar component</p></figcaption></figure>
5. Change the ColoredRectangleInstance color to black\\

   <figure><img src="/files/X35dic7Z2VAQAVKQzdpj" alt=""><figcaption></figcaption></figure>

Now we have a black background in our **HealthBar**

## Creating an Inner Container

The HealthBar displays its current health with another rectangle. This second rectangle will be contained inside a container to provide a boundary. To add an inner container:

1. Drag+drop a **Container** onto the **HealthBar**
2. Select the **Alignment** tab
3. Enter a **Margin** value of 4
4. Click the **Fill Dock** button

<figure><img src="/files/etcwn6WHwn7zzzlL8Ss0" alt=""><figcaption><p>Set Margin to 4, then click the Fill Dock button</p></figcaption></figure>

Now we have a ContainerInstance with the proper margin

<figure><img src="/files/2LwFEbjtO7IOFgpAklOD" alt=""><figcaption></figcaption></figure>

## Adding the Foreground Rectangle

Finally we'll add the foreground rectangle which displays the health:

1. Drag+drop another ColoredRectangle onto the ContainerInstance. Be sure to drop it on ContainerInstance so that the newly-added ColoredRectangle is a child of the container
2. Click the Alignment tab
3. Set Margin back to 0
4. Click the Fill Dock button
5. Change the following values:
   1. X Units to Pixels from Left
   2. X Origin to Left
   3. Width to Percentage of Container
   4. Width to 100

Now, the Width value can change between 0 and 100 to indicate the health percentage.

<figure><img src="/files/BZ6XGUvSQ7ORx1alFvYL" alt=""><figcaption></figcaption></figure>

## Expose Width

Next we'll expose the inner ColoredRectangle's `Width` property so it can be assigned per HealthBar instance:

1. Select the inner ColoredRectangle instance
2. Right-click on its `Width` variable and select **Expose Variable**\\

   <figure><img src="/files/WEnGiNvYiiVkJHyCV8sp" alt=""><figcaption><p>Click Expose Variable</p></figcaption></figure>
3. Enter an appropriate name such as "Percentage" and click OK

Now we can add instances of the HealthBar to a screen and control its fill percentage.

<figure><img src="/files/lYueFp26k5XU4BUjeMdO" alt=""><figcaption><p>Percentage value updated on a ScrollBar instance</p></figcaption></figure>

\\


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/gum/gum-tool/tutorials-and-examples/examples/health-bar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
