Skip to content

Fix Unity static state resetting for generic types - #5738

Open
ImDreamerDev wants to merge 4 commits into
clockworklabs:masterfrom
ImDreamerDev:fixUnityReset
Open

Fix Unity static state resetting for generic types#5738
ImDreamerDev wants to merge 4 commits into
clockworklabs:masterfrom
ImDreamerDev:fixUnityReset

Conversation

@ImDreamerDev

Copy link
Copy Markdown
Contributor

Description of Changes

After more testing and experimentation with Domain Reload in Unity, I found that we get the Error:

Method 'SpacetimeDB.RemoteTableHandleBase`2.ResetStaticFields' is in a generic type, but [RuntimeInitializeOnLoadMethod] methods cannot be in generic types
UnityEngine.RuntimeInitializeOnLoadManager:GetAllValidRuntimeInitializeOnLoadMethods ()

Both for RemoteTableHandleBase and DbConnectionBase

This fixes the static state resetting for both of these in Unity.

The existing ResetStaticFields method was also removed from DbConnectionBase.
DbConnectionBase.IsTesting was the only field being reset by this mechanism, and leaving it uncleared should not affect Unity.

I've also updated documentation for RemoteTableHandleBase to clarify how its static state interacts with Unity domain reloading.

API and ABI breaking changes

No API or ABI breaking changes.

The changes are internal implementation details of the Unity SDK and do not change the public API.

Expected complexity level and risk

2/5 — Low complexity and low risk.

The changes are limited to Unity-specific static state initialization and reset behavior. The main interaction is with Unity's subsystem registration lifecycle and the static state of RemoteTableHandle.

Testing

  • Verified the project compiles with the updated static reset implementation, without warnings or errors related to this
  • Verified entering and exiting Unity Play Mode repeatedly with domain reload disabled. (Both with "Reload Scene only" and "Do not reload Domain or Scene")
  • Verified normal Unity Play Mode behavior with domain reload enabled.

Removed ResetStaticFields since [RuntimeInitializeOnLoadMethod] cannot be used on methods within generic classes.

DbConnectionBase.IsTesting is the only field reset by this mechanism, and leaving it uncleared should not affect Unity.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Provides a mechanism for registering and resetting static fields during Unity's subsystem registration phase.
Unity's RuntimeInitializeOnLoadMethod can't be used in generic classes.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Updated the static constructor documentation for RemoteTableHandleBase to clarify its functionality regarding Unity's domain reloading. Removed the older ResetStaticFields method that was specific to older Unity versions.

Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant