Blocks{} of classes outside entrypoints (OnSimulate/OnBegin) will run at compile, and during that time some stuff are not available yet (such as Session, on this example), that’s why it crashes…
The solution as it was said on a earlier post, is to not run that code inside scopes that can run during compilation, instead, you should make sure it only runs at runtime, already on a session/server instance.
A example fix for your code, you need to construct the class only during OnBegin like this:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
var SomeStoredObjects : weak_map(session, []some_class) = map{}
GetSomeStoredObjects()<reads>:[]some_class=
SomeStoredObjects[GetSession()] or array{}
some_class := class:
block:
option:
set SomeStoredObjects[GetSession()] = (SomeStoredObjects[GetSession()] or array{}) + array{Self}
test_weak_maps := class(creative_device):
OnBegin<override>():void =
SomeObject := some_class{} # Note that since the class is inside the OnBegin, it is constructed during runtime and not during compile.
Another solution example, you can do something like this, constructing the class at compile, but only storing it to session at runtime:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
var SomeStoredObjects : weak_map(session, []some_class) = map{}
some_class := class<unique>:
# No block{} here, we don't want to run stuff such as GetSession() during compile
test_weak_maps := class(creative_device):
SomeObject : some_class = some_class{} # This line constructs the class but runs during compile
OnBegin():void=
Session := GetSession()
# Required to initialize current session weakmap if not yet initialized
option:
not SomeStoredObjects[Session]
set SomeStoredObjects[Session] = array{}
# example 1: always add object to weakmap, this should be unfailable
set SomeStoredObjects[Session] += array{SomeObject} or Err("Unreachable")
# example 2: add object to weakmap only if not exist yet (if class is unique), also should be unfailable
option:
not SomeStoredObjects[Session].Find[SomeObject]
set SomeStoredObjects[Session] += array{SomeObject}
Note that this compile/runtime crash behavior is only for some API calls, such as GetSession (on your case), Scene Queries, GetPlayspace and some other functions… Some will crash and others will silently fail.
Most “offline” APIs such as math, color and other code-only operations/data should still work fine when running during compile (it will be like “hard-coding” that data during compile for that class)