Add UI to all players

I am trying to add a UI for all players using this code:

Problem: it only adds one player UI

AddUI():void=
    Players := GetPlayspace().GetPlayers()
    for(Player : Players):
        if(PlayerUI := GetPlayerUI[Player]):
            PlayerUI.AddWidget(CreateUI())

UI:

    CreateUI():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

Answer: You need to initialize the widget in the UI block

2 Likes

Having a similar issue. Are you able to post a screenshot of the fix?

1 Like

I see the issue most likely which is not shown in his code above. The Text widget at the bottom of CreateUI() has probably been initialized and made somewhere only once. However in order for it to work you need a seperate widget for every player you are drawing the canvas for. That would look something like this:

AddUI():void=
    Players := GetPlayspace().GetPlayers()
    for(Player : Players):
        if(PlayerUI := GetPlayerUI[Player]):
            Text : widget = text_block{DefaultText := DefaultText, DefaultTextColor := DefaultTextColor, DefaultTextOpacity := DefaultTextOpacity, DefaultJustification := DefaultJustification, DefaultOverflowPolicy := DefaultOverflowPolicy}
            PlayerUI.AddWidget(CreateUI(Text))

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
1 Like

I believe my issue was similar but my device is set up a little different. I think I resolved it with that For loop mentioned. Will have to test.

Thank you!

1 Like

I have this device controlling the main functions, and the UI is separate but the two interact. I think that having this device decide who to display the timer to, and the other device display based on this, should fix it.

Tested solo and it still worked properly, but will test with another later.

    OnBegin<override>()<suspends> : void =            CaptureAreaDefuse.ControlChangeEvent.Subscribe(OnCaptureAreaControlChange)        CaptureArea.ItemIsDeliveredEvent.Subscribe(OnCaptureAreaItemDelivered)
            Players := GetPlayspace().GetPlayers()
            for (Player : Players):
                if:
                    PlayerUI := GetPlayerUI[player[Player]]
                then:
                    set CountdownTimer = countdown_timer_blue{MaybePlayerUI := option{PlayerUI}, RemainingTime := InitialCountdownTime}
                    CountdownTimer.CountdownEndedEvent.Await()
                    ExplosiveDevice.Explode(Player)
                else:
1 Like

It did not resolve the issue. No errors, but it only displays for me. Do you see anything wrong with my for loop?

I can’t really see how you are creating the UI which is the part that is probably causing the main issue. If you want to display the UI for seperate people you will have to make seperate instance of the UI canvas/widget aswell.

1 Like

Thank you for your assistance. I have found a built-in UI that works for this purpose

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.