Skip to content

TerrainOp Registration Improvements - #504

Open
searica wants to merge 3 commits into
Valheim-Modding:devfrom
searica:terrain-op-fixes
Open

searica wants to merge 3 commits into
Valheim-Modding:devfrom
searica:terrain-op-fixes

Conversation

@searica

@searica searica commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This has taken me a while to get around to doing but I finally did it. I've revised the changes to handling TerrainOp registration to no longer rely on ZNetScene and instead a postfix patch runs anytime that ObjectDB.UpdateRegisters is called. This has the added benefit of catching any possible removals of registered custom TerrianOps that occur during gameplay, which did seem to occur intermittently during gameplay.

Currently any prefab or CustomPrefab that is added via a call to AddPrefab is checked for a TerrainOp component and added to the cached map of custom TerrainOp instances that need to registered in ObjectDB. From my understanding, this should catch any custom items that are added and have TerrainOp components as well as ItemManager does call AddPrefab when adding and registering custom items.

I also included a TerrainOp registration check for GameObjects in RegisterToZNetScene though, as that method is public and I know some mods call it directly to add prefabs without ever calling AddPrefab. The check in RegisterToZNetScene first checks if the name hash is already in the cached map of custom TerrainOps before attempting to get a TerrainOp component to avoid doubling up on GetComponent calls for prefabs added via the AddPrefab method. If the GameObject does has a TerrainOp component and is not already tracked, it does then immediately register the new TerrainOp and add to the cached map to ensure it remains registered.

I did not include a safety check ObjectDB.instance is not null when registering a TerrainOp as the registration method is private and only called as a postfix to ObjectDB.UpdateRegisters and via RegisterToZNetScene so it would be an unnecessary defensive check that would be repeated for every single TerrainOp added.

From my testing AdvancedTerrainModifiers, applying a postfix patch to ObjectDB.UpdateRegisters to re-register custom TerrainOps does completely solve the issue of intermittently unregistering them during gameplay as I have had not gotten any more bug reports regarding that in the month since I added a similar patch to AdvancedTerrainModifiers.

I have not investigated how these changes function when adding things via the AssetManager but given that there were no reported issues with TerrainOp components included in custom assets once the previous approach of registering TerrrainOps in RegisterToZNetScene, I believe there should be no issues introduced by these changes as they preserve that behavior.

One thing I did not add but have considered is whether there should be hookable event that occurs upon registration of the TerrianOps? I'd appreciate any input on that and welcome any other feedback regarding the proposed changes in this PR.

This branch has not been deployed

No deployments
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