Not able to create static int in header file

Not able to create static global variable in c++

if the variable should have a context or lifetime you could give it to a Subsystem. So, if you gave it to a UWorldSubsystem then you could have access to it with equal access and lifetime to UWorld, and these subsystems can even be Tick-able if you need (the semi-interface is a little different but Tick-able, reasonably singleton, and has scope as needed)

an alternative if you must have true global static access then you could put this into a Blueprint Function Library which, are static by default, and can have member variables, and then just have your accessors there, (keep in mind that with regards to Blueprints a function library has not members, so you will need a getter)

Actors can kind of have static values but it looks like you might be trying to do stuff with deltaTime which doesn’t feel right to me on an actor public static (because of what static means in C++ with object instantiation)

I need a memory allocation that stays throughout the game an can be read and write by multiple thread

a true static member with multi-threaded operations, will lead to race conditions; the static member will live on the Heap and be part of the main thread, and therefore is not “thread safe”.

You can do the calculation asynch and then return it upon the thread’s lifetime expiring, but doing reads across threads is a definitive race condition.
this is why UWorld read/write is typically denied to Asynch operations outside of the spin-up or the return.

there are mechanisms to peak the asynch thread from the main thread, but this results in pausing the asynch thread to do the check, and checking to often can be worse then a synchronous operation.

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