This is about the boss battle health UI display.
I’m trying to implement a system where the UI appears only for players who enter a specific mutator zone and disappears when they leave.
I want all players in the zone to share the same UI. Instead of displaying individual health reductions, I want the UI to update and be shared among all players when someone attacks the boss.
I’ve spent a lot of time on this, but I just can’t get it to work.
As an application of what I’ve learned from your amazing tutorials, I really want to make this happen. If anyone knows how to do it, please let me know!
要約
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/SpatialMath}
using { /Verse.org/Colors }
using { /Verse.org/Colors/NamedColors }
BossBattle := module:
health_bar := class():
var iTotalHP<private> : float = 10000.0
var iCurrentHP<public>: float = 10000.0
var iBossName<private> : string = "Demon Mutatant"
BossBarColorBase<private> : color_block = color_block{
DefaultColor := MakeColorFromHex("000000")
DefaultDesiredSize := vector2{X := 900.0, Y := 40.0} }
var BossBarColorTop<private> : color_block = color_block{}
var Overlay<private> : overlay = overlay{}
@editable
BossEliminatedTrigger : trigger_device = trigger_device{}
HPBarTitleTextBlock<private> : text_block = text_block{DefaultTextColor := MakeColorFromHex("FFFFFF")}
HPTitleText<private><localizes>(HPText : string) : message = "{HPText}"
Init(BossName : string, BossTotalHP : float):void=
set iBossName = BossName
set iTotalHP = BossTotalHP
set iCurrentHP = BossTotalHP
#HPBarTitleTextBlock.SetText(HPTitleText("{iBossName}:{iCurrentHP}/{iTotalHP}"))
if (Current := Round[iTotalHP], Total := Round[iCurrentHP]): # int型に変換
HPBarTitleTextBlock.SetText(HPTitleText("{iBossName}:{Current}/{Total}"))
TakeDamage(DamageAmount : float):void=
set iCurrentHP -= DamageAmount
if (iCurrentHP < 0.0):
set iCurrentHP = 0.0
HealthPercentage := iCurrentHP / iTotalHP
BarSize := BossBarColorBase.GetDesiredSize()
NewHealthBarWidth := BarSize.X * HealthPercentage
if (Current := Round[iCurrentHP], Total := Round[iTotalHP]): # int型に変換
HPBarTitleTextBlock.SetText(HPTitleText("{iBossName}:{Current}/{Total}")) # ここで整数を表示
UpdateHealthBar(NewHealthBarWidth)
Heal(HealAmount : float):void=
block:
BossEliminationCheck(Player : player):void=
if (iCurrentHP = 0.0):
Print("Boss Eliminated")
RemoveHealthBar(Player)
set iTotalHP = 10000.0
set iCurrentHP = 10000.0
ShowUIForPlayer(Player : player):void=
if (PlayerUI := GetPlayerUI[Player]):
PlayerUI.AddWidget(CreateUI())
Print("Show UI")
RemoveHealthBar(Player : player):void=
Overlay.RemoveWidget(HPBarTitleTextBlock)
Overlay.RemoveWidget(BossBarColorTop)
Overlay.RemoveWidget(BossBarColorBase)
Print("Remove UI")
ShowUIToAllPlayers():void=
block:
MyButton : button_device = button_device{}
UpdateHealthBar(NewWidth : float):void=
Overlay.RemoveWidget(HPBarTitleTextBlock)
Overlay.RemoveWidget(BossBarColorTop)
set BossBarColorTop = CreateTopHealthBar(NewWidth)
Print("Update UI")
Overlay.AddWidget(CreateOverlaySlot(BossBarColorTop, horizontal_alignment.Left))
Overlay.AddWidget(CreateOverlaySlot(HPBarTitleTextBlock, horizontal_alignment.Center))
CreateOverlaySlot<private>(TheWidget : widget, HAlignment : horizontal_alignment):overlay_slot=
overlay_slot:
Widget := TheWidget
HorizontalAlignment := HAlignment
CreateTopHealthBar<private>(Width : float):color_block=
ColorBlock := color_block{
DefaultColor := MakeColorFromHex("ff2a00")
DefaultDesiredSize := vector2{X := Width, Y := 40.0} }
set BossBarColorTop = ColorBlock
CreateOverlay<private>() : overlay=
TheOverlay := overlay:
Slots := array:
overlay_slot:
Widget := BossBarColorBase
overlay_slot:
Widget := CreateTopHealthBar(900.0)
overlay_slot:
Widget := HPBarTitleTextBlock
set Overlay = TheOverlay
CreateUI<private>(): canvas=
TheCanvas : canvas = canvas:
Slots := array:
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.9}, Maximum := vector2{X := 0.5, Y := 0.9}}
Offsets := margin{Top := 0.0, Left := 0.0, Right := 0.0, Bottom := 100.0}
Alignment := vector2{X := 0.5, Y := 1.0}
Widget := stack_box:
Orientation := orientation.Vertical
Slots := array:
stack_box_slot:
Widget := CreateOverlay()
return TheCanvas
Boss_Battle := class(creative_device):
@editable
BossMonster : creative_prop = creative_prop{}
@editable
BossPropManip : prop_manipulator_device = prop_manipulator_device{}
#ミューテーターゾーン
@editable
BossBattle_Area : mutator_zone_device = mutator_zone_device{}
@editable
BossBattle_Area_Respawn : mutator_zone_device = mutator_zone_device{}
@editable
BossBattle_Area_Bonus : mutator_zone_device = mutator_zone_device{}
@editable
NextBossFightTimer : timer_device = timer_device{}
@editable
BossFightingTimer : timer_device = timer_device{}
@editable
BossEliminatedTrigger : trigger_device = trigger_device{}
@editable
PlayerCount : player_counter_device = player_counter_device{}
@editable
EliminationGold : item_granter_device = item_granter_device{}
BossBar : health_bar = health_bar{}
OnBegin<override>()<suspends>:void=
BossPropManip.DamagedEvent.Subscribe(OnTheBossDamaged)
BossBar.Init("Demon Mutatant", 10000.0)
BossBattle_Area.AgentEntersEvent.Subscribe(OnPlayerEntersArea)
#BossBattle_Area.AgentEntersEvent.Subscribe(PlayerCheck)
BossBattle_Area.AgentExitsEvent.Subscribe(OnPlayerLeavesArea)
#ボスファイト開始時のイベント
NextBossFightTimer.SuccessEvent.Subscribe(OnBossFightStart)
#ボスファイト終了時のイベント
BossFightingTimer.SuccessEvent.Subscribe(OnBossBattleTimeFinish)
#ボスの体力が0になった時のイベント
BossEliminatedTrigger.TriggeredEvent.Subscribe(OnBossEliminated)
OnPlayerEntersArea(Agent : agent):void=
AllPlayers:=GetPlayspace().GetPlayers()
for (Player : AllPlayers):
if (BossBattle_Area.IsInVolume[Agent]):
if (Fort:=Agent.GetFortCharacter[]):
#if (Player := player[Agent]): # エージェントをプレイヤーに変換
BossBar.ShowUIForPlayer(Player)
Print("Player Enters Area")
OnPlayerLeavesArea(Agent : agent):void=
if (Player := player[Agent]): # エージェントをプレイヤーに変換
BossBar.RemoveHealthBar(Player)
Print("Player Leaves Area")
OnBossFightStart(Agent : ?agent):void=
BossBattle_Area.Enable()
Print("Boss Fight Start")
OnTheBossDamaged(Agent :agent):void=
BossBar.TakeDamage(10.0)
if (Player := player[Agent]):
if(BossBar.iCurrentHP = 0.0):
BossBar.BossEliminationCheck(Player)
BossEliminatedTrigger.Trigger(Agent)
OnBossEliminated(Agent : ?agent):void=
BossBattle_Area.Disable()
BossBattle_Area_Respawn.Enable()
BossBattle_Area_Bonus.Enable()
BossBattle_Area_Respawn.Disable()
BossBattle_Area_Bonus.Disable()
Print("Boss Eliminated")
OnBossBattleTimeFinish(Agent :?agent):void=
BossBattle_Area.Disable()
BossBattle_Area_Respawn.Enable()
BossBattle_Area_Respawn.Disable()
Print("Boss Battle Time Finish")