# Multiple players accesing a variable globally defined widget

**URL:** <https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368>\
**Category:** Programming & Scripting\
**Tags:** fortnite-creative, unreal-editor-for-fortnite, question, fortnite\
**Created:** [April 24, 2023, 2:46pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368 "2023-04-24T14:46:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinestCreative](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finestcreative/32/1034971_2.png) [@FinestCreative](https://forums.unrealengine.com/u/FinestCreative)\
**Post date:** [April 24, 2023, 2:46pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/1 "2023-04-24T14:46:36Z")

</div>

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?

```auto
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

```

.  
.

```auto
# Spawn the StarTimer function to start a countdown
OnBegin<override>()<suspends>:void={
        spawn{StartTimer()}

```

.  
.

```auto
CreateUI<private>(Agent:agent):canvas={
...
set Slots += array{             
            canvas_slot:
                Widget := WidgetsTimer
                ...

        }
...
return canvas{Slots:= Slots}

```

```auto
# Start UI Timer
    StartTimer<private>()<suspends>:void={
        loop:
            set Timer -= 1
            WidgetsTimer.SetText(UIMessageTimer(IntToTime(Timer)))
            Sleep(1.0)
            if(Timer = 0):
                break
    }

```

[https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/d/0/dd0ade0cc2b3714e951c45ce93e0a01cc15f6cab.mp4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/d/0/dd0ade0cc2b3714e951c45ce93e0a01cc15f6cab.mp4)

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.

```auto
# 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}
    }

```

---

<div class="post-metadata">

**Author:** ![EV\_WAKA](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EV\_WAKA](https://forums.unrealengine.com/u/EV_WAKA)\
**Post date:** [April 24, 2023, 7:14pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/2 "2023-04-24T19:14:30Z")

</div>

Just a guess, but does the specifier restrict it to one agent?

---

<div class="post-metadata">

**Author:** ![FinestCreative](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finestcreative/32/1034971_2.png) [@FinestCreative](https://forums.unrealengine.com/u/FinestCreative)\
**Post date:** [April 25, 2023, 9:27am UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/3 "2023-04-25T09:27:20Z")

</div>

which specifier

---

<div class="post-metadata">

**Author:** ![LionUnchained](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lionunchained/32/1039015_2.png) [@LionUnchained](https://forums.unrealengine.com/u/LionUnchained)\
**Post date:** [May 11, 2023, 9:15am UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/4 "2023-05-11T09:15:27Z")

</div>

I’m pretty sure that’s what private does. Once its called from playerUI it can no longer be accessed anywhere else.

What if you made multiple versions of the timer?  
I don’t know how this would work in code but in devices my thought is you set a timer, that then triggers a relay to push out a start event to an array of timers so that there is 1 timer per player in the game.

To do it in code I think you may want to look into passing additional information by wrapping a subscribe function.  
[https://forums.unrealengine.com/t/guide-to-event-subscribing-with-additional-parameters-handler-functions/793925](https://forums.unrealengine.com/t/guide-to-event-subscribing-with-additional-parameters-handler-functions/793925)

If you figure this out I’d be very curious to know how

---

<div class="post-metadata">

**Author:** ![FinestCreative](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finestcreative/32/1034971_2.png) [@FinestCreative](https://forums.unrealengine.com/u/FinestCreative)\
**Post date:** [May 12, 2023, 6:40pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/5 "2023-05-12T18:40:59Z")

</div>

I fixed it by giving every player their own widget, instead of refering to a global one

---

<div class="post-metadata">

**Author:** ![LionUnchained](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lionunchained/32/1039015_2.png) [@LionUnchained](https://forums.unrealengine.com/u/LionUnchained)\
**Post date:** [May 12, 2023, 11:40pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/6 "2023-05-12T23:40:38Z")

</div>

Was about to say Mark’s last tutorial had a solution there too. Glad you got it working!

---

<div class="post-metadata">

**Author:** ![AxelCapek](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/axelcapek/32/950074_2.png) [@AxelCapek](https://forums.unrealengine.com/u/AxelCapek)\
**Post date:** [May 13, 2023, 12:09am UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/7 "2023-05-13T00:09:19Z")

</div>

Make a UI class that contains your widgets, spawn + assign a new class for each player.

---

<div class="post-metadata">

**Author:** ![Patothief](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Patothief](https://forums.unrealengine.com/u/Patothief)\
**Post date:** [May 15, 2023, 10:39am UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/8 "2023-05-15T10:39:16Z")

</div>

I face the same issue. Spawning separate widgets solved the presentation issue but still all players are accessing the same global variable, in your example that would be “var Timer” with effect that loop would decrease the timer faster than intended e.g. if there are two players, then Timer would decrease by 2 every seconds.

---

<div class="post-metadata">

**Author:** ![LionUnchained](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lionunchained/32/1039015_2.png) [@LionUnchained](https://forums.unrealengine.com/u/LionUnchained)\
**Post date:** [May 25, 2023, 7:58pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/9 "2023-05-25T19:58:47Z")

</div>

I know this is seriously janky but could you have the timer decrease at `(1/P)*1 sec` where P is players?  
In theory having your global variable scale to the player count.

---

<div class="post-metadata">

**Author:** ![Patothief](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Patothief](https://forums.unrealengine.com/u/Patothief)\
**Post date:** [May 25, 2023, 8:35pm UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/10 "2023-05-25T20:35:58Z")

</div>

> [@AxelCapek](#):
>
> Make a UI class that contains your widgets, spawn + assign a new class for each player.

This is the correct approach

---

<div class="post-metadata">

**Author:** ![itascreations](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@itascreations](https://forums.unrealengine.com/u/itascreations)\
**Post date:** [August 18, 2023, 12:51am UTC](https://forums.unrealengine.com/t/multiple-players-accesing-a-variable-globally-defined-widget/1139368/11 "2023-08-18T00:51:07Z")

</div>

it’s over for me, I have the same problem and i transferred the canvas method + show/remove into another class, but now I have to transfer all the widgets, the problem is: i work with them inside the device class… How do I even edit all the players widgets
