# Need help with weird static variable behaviour c++

**URL:** <https://forums.unrealengine.com/t/need-help-with-weird-static-variable-behaviour-c/1276793>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, CPP, question\
**Created:** [August 29, 2023, 6:08pm UTC](https://forums.unrealengine.com/t/need-help-with-weird-static-variable-behaviour-c/1276793 "2023-08-29T18:08:45Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![IrSoil](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IrSoil](https://forums.unrealengine.com/u/IrSoil)\
**Post date:** [August 29, 2023, 7:10pm UTC](https://forums.unrealengine.com/t/need-help-with-weird-static-variable-behaviour-c/1276793/3 "2023-08-29T19:10:15Z")

</div>

c++'s statics will be global for editor and all pie sessions, that’s why your `Initialized` is `true`. But also, as you can see:

> I start to lose some informations on each of the GridElement\*

your `GridElement*`s are killed by garbage collector since your array isn’t an `UPROPERTY()` (and iirc you can’t mark static variable as `UPROPERTY()`)

As for solution, i like the answer from this thread: [Singleton pattern in c++ - #2 by smugdev](https://forums.unrealengine.com/t/singleton-pattern-in-c/430452/2) - don’t use c++'s singletons and use ue’s local “singletons” from [gameplay framework](https://docs.unrealengine.com/4.26/en-US/InteractiveExperiences/Framework/QuickReference/).

> all my instances to have the same grid

Depending on exact goals, something like GameMode, GameInstance, GameState or as said in other comment [Subsystem](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Subsystems/) - makes sense. You can create a single `USharpPattern*` (or just an array of `UCraftBox`’es) here and use it when you create instance of object or whenever you need it.

---

_[View the full topic](https://forums.unrealengine.com/t/need-help-with-weird-static-variable-behaviour-c/1276793)._
