====== HP Bar (Icon) ======
=====Finished Product=====
{{:developer_center:developer_editor:202507251447.mp4?nolink&400|}}
===== UI Design=====
{{:developer_center:developer_editor:screenshot_1753428785.png?nolink|}}
===== Script =====
local UI_ID = nil
local HEART_TEXT_ID = nil
local FULL_HEART = "◆"
local EMPTY_HEART = "◇"
local function UpdateHeartText(e)
local pid = e.eventobjid
local attr = e.playerattr
if attr == PLAYERATTR.CUR_HP then
local _, cur = Player:getAttr(pid, PLAYERATTR.CUR_HP)
local _, max = Player:getAttr(pid, PLAYERATTR.MAX_HP)
if max <= 0 then max = 1 end
local percent = cur / max
local hearts = math.floor(percent * 10 + 0.0001)
local text = ""
for i = 1, 10 do
if i <= hearts then
text = text .. FULL_HEART
else
text = text .. EMPTY_HEART
end
end
Customui:setText(pid, UI_ID, HEART_TEXT_ID, text)
end
end
ScriptSupportEvent:registerEvent("Game.AnyPlayer.EnterGame", UpdateHeartText)
ScriptSupportEvent:registerEvent("Player.ChangeAttr", UpdateHeartText)
=====Explanation =====
==== Global Variables: ====
local UI_ID = nil
local HEART_TEXT_ID = nil
These variables are placeholders for the custom UI components:
* **UI_ID**: The ID of the custom UI interface.
* **HEART_TEXT_ID**: The ID of the text component within the UI that will display the health bar.
==== Heart Representation (Text-based):====
local FULL_HEART = "◆"
local EMPTY_HEART = "◇"
These are just visual symbols to represent health:
* FULL_HEART: Symbol for full/active health.
* EMPTY_HEART: Symbol for missing/empty health.
==== Function: UpdateHeartText(e) ====
This function updates the UI text based on the player's current health.
**1. Extract player information from event:**
local pid = e.eventobjid
local attr = e.playerattr
* **pid:** The ID of the player whose data is changing.
* **attr:** The specific attribute that changed (like health).
**2. Only proceed if the attribute is current health:**
if attr == PLAYERATTR.CUR_HP then
Ensures this logic runs only when health has changed.
**3. Fetch current and max HP:**
local _, cur = Player:getAttr(pid, PLAYERATTR.CUR_HP)
local _, max = Player:getAttr(pid, PLAYERATTR.MAX_HP)
Retrieves the player's current and maximum health values.
if max <= 0 then max = 1 end
Prevents division by zero in case max HP is 0 or invalid.
**4. Calculate percentage and number of full hearts:**
local percent = cur / max
local hearts = math.floor(percent * 10 + 0.0001)
Calculates the ratio of current health to maximum health.
Scales it to a 10-point scale (like 10 hearts).
The + 0.0001 helps prevent rounding errors.
**5. Build the heart bar as a text string:**
local text = ""
for i = 1, 10 do
if i <= hearts then
text = text .. FULL_HEART
else
text = text .. EMPTY_HEART
end
end
Loops through 10 positions:
Adds a full heart if the player has that much health.
Otherwise, adds an empty heart.
**Update the UI:**
Customui:setText(pid, UI_ID, HEART_TEXT_ID, text)
Displays the generated text (heart bar) on the player's custom UI.
==== Event Registration: ====
ScriptSupportEvent:registerEvent("Game.AnyPlayer.EnterGame", UpdateHeartText)
ScriptSupportEvent:registerEvent("Player.ChangeAttr", UpdateHeartText)
These lines ensure that the UpdateHeartText function is called:
When any player enters the game.
When a player's attributes change (like losing or gaining health).
----
----
If you have any questions about Developer Tools, feel free to join our official discord server
[[https://discord.gg/NVRZHBChBt|Mini World Global DEV discord]]