I made a UI that includes a timer, but the widget shows for only up to 1 agent at a time. I think it is because the WidgetsTimer is defined outside the CreateUI function. So when all agents call CreateUI it will return this globally defined widget, but only for 1 agent for whatever reason.
How to show the widget for all players. while mainting my timer functionality where it counts down on the screen?
team_selector := class(creative_device):
var WidgetsTimer<private>:text_block = text_block{DefaultTextColor:=NamedColors.White}
UIMessageTimer<private><localizes>(Time:string) : message = "{Time}"
var Timer<private>:int = 75
.
.
# Spawn the StarTimer function to start a countdown
OnBegin<override>()<suspends>:void={
spawn{StartTimer()}
.
.
CreateUI<private>(Agent:agent):canvas={
...
set Slots += array{
canvas_slot:
Widget := WidgetsTimer
...
}
...
return canvas{Slots:= Slots}
# Start UI Timer
StartTimer<private>()<suspends>:void={
loop:
set Timer -= 1
WidgetsTimer.SetText(UIMessageTimer(IntToTime(Timer)))
Sleep(1.0)
if(Timer = 0):
break
}
For extra detail, here is the entire CreateUI function. I had the same issue with WidgetsTeam
and WidgetsCount
, but it was fixed by defining them INSIDE CreateUI
, rather than globally. But I think I have to keep the Timer global because of the way it counts down using a global variable int.
# Create the UI
CreateUI<private>(Agent:agent):canvas={
Print("creating uUI")
AgentTeam := TeamManager.GetTeam(Agent)
var WidgetsTeam:[]text_block = array{text_block{},text_block{},text_block{},text_block{}}
var WidgetsCount:[]text_block = array{text_block{},text_block{},text_block{},text_block{}}
for(X -> Widget:WidgetsCount, TeamWidget := WidgetsTeam[X]):
TeamCount := TeamManager.GetAgentsInTeam(X + 1).Length
Widget.SetText(UIMessageCount(TeamCount))
TeamWidget.SetText(UIMessageTeam(X + 1))
# Set Widget Color
var Color:color = NamedColors.White
if(X = AgentTeam - 1, set Color = TeamColors[X]){}
Widget.SetTextColor(Color)
TeamWidget.SetTextColor(Color)
var Color2:color = NamedColors.White
if(set Color2 = TeamColors[AgentTeam - 1]){}
var Slots:[]canvas_slot = for:
X -> CountWidget:WidgetsCount
TeamWidget := WidgetsTeam[X]
Array : array{
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.2 + (0.2 * X), Y := 0.05}, Maximum := vector2{X := 0.2 + (0.2 * X), Y := 0.05}}
Alignment := vector2{X := 0.5, Y := 1.0}
SizeToContent := true
Widget := TeamWidget
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.2 + (0.2 * X), Y := 0.05}, Maximum := vector2{X := 0.2 + (0.2 * X), Y := 0.05}}
Alignment := vector2{X := 0.5, Y := 0.0}
SizeToContent := true
Widget := CountWidget
}
do:
Array
set Slots += array{
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.0, Y := 0.0}, Maximum := vector2{X := 1.0, Y := 0.1}}
Alignment := vector2{X := 0.5, Y := 0.5}
SizeToContent := true
Widget := color_block {DefaultColor:=NamedColors.Black, DefaultOpacity:=0.7}
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.075, Y := 0.05}, Maximum := vector2{X := 0.075, Y := 0.05}}
Alignment := vector2{X := 0.5, Y := 0.5}
SizeToContent := true
Widget := WidgetsTimer # text_block{DefaultText := UITest}# WidgetsTimer
}
Print("slots {Slots.Length}")
return canvas{Slots:= Slots}
}