# I came up with a way to make singletons in Verse

**URL:** <https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, fortnite\
**Created:** [April 24, 2023, 3:22pm UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453 "2023-04-24T15:22:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattRix](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mattrix/32/686548_2.png) [@MattRix](https://forums.unrealengine.com/u/MattRix)\
**Post date:** [April 24, 2023, 3:22pm UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/1 "2023-04-24T15:22:24Z")

</div>

I’ve seen people looking for ways to connect between different devices at a global level, so I came up with a way to make device singletons that work in Verse.

```auto
global := class<varies><concrete>(): 
   var MaybeMainDevice : ?main_device = false

GetMainDevice():main_device = 
    if(MainDevice := Global.MaybeMainDevice?) then MainDevice else main_device{}

Global : global = global{}

```

and the in the device’s OnBegin you call

```auto
set Global.MaybeMainDevice = option{Self}

```

after that you can call `GetMainDevice()` from anywhere in the rest of code to access it

I’d be curious if anyone knows a better way to do it, but this was the way that worked for me. I’ll break down why I did it this way:

- You can’t have a mutable variable at module/global scope, so it had to be inside a class (hence the `global` class).

- In order to instantiate a class at global scope, it can’t be `<transacts>`, it has to be `<varies>` (note: it also can’t be `<computes>` because of the mutable variable).

- Because it’s `<varies>`, we can’t create the “fake” initializer class (`main_device{}`) directly, instead we have to use an `option`.

- We could access the `option` directly, but then we’d have to always do that in a failure context despite knowing it exists, which is annoying, so instead we create a non-failable `GetMainDevice()` function to access it

Hope that explains it! There might be some easier way to do this that I missed, so I’d be interested to learn if there are any other approaches to this out there.

---

<div class="post-metadata">

**Author:** ![CosmicDanAU](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/cosmicdanau/32/642728_2.png) [@CosmicDanAU](https://forums.unrealengine.com/u/CosmicDanAU)\
**Post date:** [May 6, 2023, 2:24am UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/3 "2023-05-06T02:24:38Z")

</div>

Not bad. Personally, I just add custom properties (an `@editable`) where the variable type is my other device (the actual class).

---

<div class="post-metadata">

**Author:** ![AndrewScheidecker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andrewscheidecker/32/926976_2.png) [@AndrewScheidecker](https://forums.unrealengine.com/u/AndrewScheidecker)\
**Post date:** [May 6, 2023, 12:05pm UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/4 "2023-05-06T12:05:37Z")

</div>

Good find!

Unfortunately, there is a reason why we don’t let you declare a global `var`: we’re not done implementing global variables, and want to reserve the right to change their behavior without breaking everybody’s code, so for now we disallow them.

We recently discovered this particular loophole ourselves, and are in the process of closing it. I recommend against using it in the meantime, as once we close the loophole, your code won’t compile.

For now, you only have round-scoped variables on device classes.

---

<div class="post-metadata">

**Author:** ![CosmicDanAU](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/cosmicdanau/32/642728_2.png) [@CosmicDanAU](https://forums.unrealengine.com/u/CosmicDanAU)\
**Post date:** [May 7, 2023, 4:44am UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/5 "2023-05-07T04:44:43Z")

</div>

Is this because using Verse code in this way allows one to circumvent memory limits? If so, that makes complete sense.

So, having multiple creative\_devices reference each other via `@editable` remains an appropriate means of “inter-script communication”? Assuming yes, since the scripts involved still need to be placed within the world (instanced), just as you normally would. Oh, and, of course, we can’t have a circular dependency (i.e. two scripts depending on each other) - I am pretty sure I’ve gotten that specific error in the past 🙂

---

<div class="post-metadata">

**Author:** ![Conan\_Reis](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/conan_reis/32/1016955_2.png) [@Conan\_Reis](https://forums.unrealengine.com/u/Conan_Reis)\
**Post date:** [June 9, 2023, 6:38pm UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/6 "2023-06-09T18:38:44Z")

</div>

Since this loophole is now closed as of 25.0 you probably want to use _Gameplay Tags_ instead.

> **[Gameplay Tags](https://dev.epicgames.com/documentation/en-us/uefn/gameplay-tags-in-verse)**
>
> Find all actors marked with a Gameplay Tag while the game is running, using Verse.

For _now_ using tags is probably the best way to programmatically gather one or more devices.

We are discussing this internally at Epic and more related mechanisms will come in the future.

---

<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:** [October 28, 2023, 10:42pm UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/7 "2023-10-28T22:42:16Z")

</div>

With weakmaps implemented is this now viable again?

---

<div class="post-metadata">

**Author:** ![LoPing](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/loping/32/737300_2.png) [@LoPing](https://forums.unrealengine.com/u/LoPing)\
**Post date:** [October 29, 2023, 12:46am UTC](https://forums.unrealengine.com/t/i-came-up-with-a-way-to-make-singletons-in-verse/1139453/8 "2023-10-29T00:46:25Z")

</div>

I make a main device and have other custom devices (or arrays of custom devices) be @editable to the main device. This way it’s easy to get devices communicating with each other. It also can ensure that devices initialize as they should.
