Valheim-Modding / Jotunn

Jötunn, the Valheim Library.
https://valheim-modding.github.io/Jotunn/
MIT License
119 stars 40 forks source link

[BUG] icons visual issues (hammer piece table) #389

Open galathil opened 1 year ago

galathil commented 1 year ago

Details: icons in hammer table has sometimes visual issues Jotunn Version: 2.10.1.0 Jotunn Submodule(if applicable): none Repeatability(Consistent(100%), Inconsistent(50%), Rare(1%), Unknown(==1)): Inconsistent or rare

Problem Description: I've develop a mod that add vanilla prefabs as building pieces in the game. The code wich generate the icon is :

private static Sprite CreatePrefabIcon(GameObject prefab)
{
    Sprite result = RenderManager.Instance.Render(prefab, RenderManager.IsometricRotation);
    return result;
}

The mod is available here : https://github.com/galathil/MoreVanillaBuilds Note that my mods icons are affected by this random issue, not vanilla icons. You need to restart the game to clear this issue. Other mods in plugin folder :

Maybe i make a mistake with RenderManager but i look other mods that include custom content in the game and syntax is the same.

Expected Behaviour: No visual glitches

Actual Behaviour: Visual glitches sometimes :

Link to your server's and client's LogOutput.log No error in logs

sirskunkalot commented 1 year ago

Hey, thanks for the report. I am not sure why this happens yet, has to be investigated more. Which will be hard when its not reproducible. But as some sort of "workaround" you could use the Render Manager cache for caching the generated icons to disk. This will also improve mod loading time a lot since you generate quite a lot of icons. Check out the tutorial section for more information: https://valheim-modding.github.io/Jotunn/tutorials/renderqueue.html#caching-icons

galathil commented 1 year ago

Thanks for you reply. i'll implement caching in my mod and if i find any additionnal infos (or evidence) about the bug, i will post here.

MSchmoecker commented 1 month ago

The underling issue seems to be a race condition. When rendering an icon the same frame as the game is switching scenes, the bug can occur. Many important Valheim singletons are created during a scene change, like ZNetscene.Awake or ObjectDB.Awake.

A reliable workaround is to wait one frame before rendering icons.

For example a coroutine can be used:

private List<GameObject> prefabsToRender = new List<GameObject>();

private void Awake() {
    prefabsToRender.Add(prefab);
}

private void OnObjectDBAwake() {
    StartCoroutine(RenderSprites());
}

private static IEnumerator RenderSprites() {
    yield return null; // wait one frame

    foreach (var prefab in prefabsToRender) {
        Sprite result = RenderManager.Instance.Render(prefab, RenderManager.IsometricRotation);
    }
}