Hello, to make it easy for you I am trying to make some sort of altitude meter and display it on every player’s screens through Verse. The widget should refresh every 0.1s to display new value.
The issue I am facing is that the UI is correctly displayed to players, the value displayed is the right one for each player, but the widget is not removed (after every refresh, a new instance of the widget is created and it stacks onto the previous one).
I do think that the issue is coming from the WidgetText widget textblock that I made, I used the DefaultText Data instead of the .SetText() function but I am unable to use it because of the “transacts” effect.
If anyone can help me on this one it would be much appreciated !
Here is the full code:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /Fortnite.com/Characters }
using { /UnrealEngine.com/Temporary/SpatialMath }
# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.
# A Verse-authored creative device that can be placed in a level
altitude_o_meter := class(creative_device):
var AltitudeMap : [player]int = map{}
var WidgetText : widget = text_block{}
var Canvas : canvas = canvas{}
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
Players := GetPlayspace().GetPlayers()
Print("Adding UIs..")
AddUI()
Print("UIs added")
loop:
Sleep(0.1)
for (Player : Players):
GetAltitude(Player)
UpdateUI()
GetAltitude(Player : player):void=
if (Fort := Player.GetFortCharacter[], Altitude := Round[Fort.GetTransform().Translation.Z]):
if (set AltitudeMap[Player] = Altitude):
UI_Text<localizes>(Value:int):message = "{Value}m"
AddUI():void=
Players := GetPlayspace().GetPlayers() # Get all players
for(Player : Players):
GetAltitude(Player) # Get the current altitude of players and store it inside AltituteMap
if(PlayerUI := GetPlayerUI[Player], Altitude := Floor(AltitudeMap[Player] / 100)):
set WidgetText = text_block{DefaultText := UI_Text(Altitude)}
PlayerUI.AddWidget(CreateUI(WidgetText))
CreateUI(Text : widget):canvas=
UI := canvas:
Slots := array:
canvas_slot:
Anchors := anchors{Minimum:= vector2{X:= 0.5, Y:= 0.1}, Maximum:= vector2{X:= 0.5, Y:= 0.1}}
Offsets := margin{Top:= 0.0, Left := 0.0, Right := 0.0, Bottom := 0.0}
Alignment := vector2{X:= 0.5, Y:= 0.5}
ZOrder := 10
Widget := Text
UpdateUI():void=
for (Player->Alt : AltitudeMap):
if (Altitude := Floor(Alt/100), PlayerUI := GetPlayerUI[Player]):
set WidgetText = text_block{DefaultText := UI_Text(Altitude)}
PlayerUI.AddWidget(CreateUI(WidgetText))