- Home /
How to use SharedStatic? ECS Burst SystemBase
So i recently posted a question about accessing and writing to static readonly variables and it has led me to shared static. Does anyone here know how to initialize a shared static variable such as this.
public static NativeMultiHashMap<int, QuadrantData> quadrantMultiHashMap = new NativeMultiHashMap<int, QuadrantData>(0, Allocator.Persistent);
but in shared static.
to be clear the only reason i need this is to write to static readonly variable.
Answer by andrew-lukasik · Jun 03, 2021 at 11:30 PM
I know what that error message said... but it was misleading in this case. You don't need SharedStatic
to fix BC1042
as this is NativeMultiHashMap
and you're not synchronizing pointers to different NativeMultiHashMaps across managed-unmanaged memory domains, aren't you?
To the contrary - you have a singleton of type NativeMultiHashMap
and just trying to access it's data in different points of your code, which in turn triggers this ambiguous compiler error.
To fix this BC1042
here, you need to stop accessing static fields inside burst-compiled code blocks, that's it.
What's more, this doesn't has to be a static
field at all. If you remove this static
keyword, it will remain accessible from other systems (given dependencies are resolved) as long as it's a public field:
var quadrantSystem = World.GetExistingSystem<QuadrantSystem>();
var quadrants = quadrantSystem.quadrantMultiHashMap;
I will try your solution out it seems to be a sensible answer.
we are getting close but another error has popped up insisting that error DC0004: Entities.ForEach Lambda expression captures a non-value type 'quadrantSystem'. This is only allowed with .WithoutBurst() and .Run()
Yes, it is to be expected. Don't reference systems (a class) in a job. Reference structs there only (native hash map is one)
Answer by critterawesome · Jun 04, 2021 at 08:58 PM
Thanks andrew lukasik! i implemented your suggestion after a long time of errors and debugging and it finally works!
Your answer
Follow this Question
Related Questions
why my static timer variable print 0 everytime i call the getcolor function?! 0 Answers
Acessing static variable from Entities.ForEach 1 Answer
Will DOTS Netcode be easier than Photon? 2 Answers
SystemBase ECS unity doesnt let me acess NativeMultiHashMap because it isnt readonly? 2 Answers
How to set mobile control and scale in size in android setting? 1 Answer