Mods not working in 0.10.24 builds

Discuss modding questions and implementation details.
Post Reply
User avatar
Interkarma
Posts: 7242
Joined: Sun Mar 22, 2015 1:51 am

Re: Mods not working in 0.10.24 builds

Post by Interkarma »

Just noting that you're adding the ClimateCalories component twice to the same GameObject in your sample above:

Code: Select all

go.AddComponent<ClimateCalories>();
instance = go.AddComponent<ClimateCalories>();
All you need is:

Code: Select all

instance = go.AddComponent<ClimateCalories>();
I wonder if adding component twice is causing some of your issues?

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Mods not working in 0.10.24 builds

Post by Ralzar »

Yeah, I was wondering about that. I'll change that in the future.

At the moment, I'm pruning my mod code down, just commenting out more and more. I've removed everything related to save/load at the moment. I'll just keep cutting parts until something gives.

User avatar
Interkarma
Posts: 7242
Joined: Sun Mar 22, 2015 1:51 am

Re: Mods not working in 0.10.24 builds

Post by Interkarma »

I'd make that change sooner rather than later. It's a pretty glaring issue that will cause double event registration, etc. Anything that uses static instance reference vs. self reference are going to be looking at two different components with different state.

Have you tried building a .dfmod from my minimal example provided earlier? Does this work for you in 0.10.24 builds as it does for me? If not, this could indicate something wrong with the build process. Example code again below for convenience.

Code: Select all

using UnityEngine;
using DaggerfallWorkshop.Game;
using DaggerfallWorkshop.Game.Utility.ModSupport;
using DaggerfallWorkshop;

namespace MyMod
{
    public class MyMod : MonoBehaviour
    {
        static Mod mod;

        [Invoke(StateManager.StateTypes.Start, 0)]
        public static void Init(InitParams initParams)
        {
            mod = initParams.Mod;
            var go = new GameObject(mod.Title);
            go.AddComponent<MyMod>();

            DaggerfallUnity.LogMessage("MyMod Init()", true);

            PlayerActivate.RegisterCustomActivation(mod, 210, 16, TorchSound);
            PlayerActivate.RegisterCustomActivation(mod, 210, 17, TorchSound);
            PlayerActivate.RegisterCustomActivation(mod, 210, 18, TorchSound);
        }

        private void Awake()
        {
            // Do required stuff on Awake

            DaggerfallUnity.LogMessage("MyMod Awake()", true);
        }

        private static void TorchSound(RaycastHit hit)
        {
            DaggerfallUI.Instance.PlayOneShot(SoundClips.ActivateGears);
        }
    }
}

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Mods not working in 0.10.24 builds

Post by Ralzar »

I've been trying to comment out likely suspects. The mod keeps working in Editor and in live 0.10.23 while not in live 0.10.24

The first thing I tried removing was those two lines.

I was honestly expecting either something with the save/load code or the food code to be the problem, but nothing I removed fixed it.

So after removing larger and larger chunks I've now decided to just work my way up from the other end. I've literally just commented out the entire mod and am now working my way upwards in complexity to see when something breaks. The mod I have at the moment is pretty much the same as your example. I'll keep adding C&C features to it until something happens.

User avatar
Interkarma
Posts: 7242
Joined: Sun Mar 22, 2015 1:51 am

Re: Mods not working in 0.10.24 builds

Post by Interkarma »

Sounds good. Let me know once you have a minimal repro mod (the smaller the better) and I'll try to help from there. :)

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Mods not working in 0.10.24 builds

Post by Ralzar »

I think I've found it.

In a place I wasn't even looking. Of course :roll:
I'm not certain yet, but it seems to be caused by some values I declare, where they refer to methods. One or more of those methods must be simply running too early and causing something to crash. Which was never a problem before 0.10.24.
Changing Invoking state to Game instead of Start, makes the values be declared later, solving that problem, but then causing the problem with save/load not handling items and sprite clicking correctly.

I'll get back to you when I've identified exactly what is causing this.

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Mods not working in 0.10.24 builds

Post by Ralzar »

I'll keep digging for more. But I think I finally managed to nail down kinda what's going on:


https://github.com/Ralzar81/Climates-Ca ... alories.cs


Line 272: static PlayerEntity playerEntity = GameManager.Instance.PlayerEntity;
Line 274: static RaceTemplate playerRace = playerEntity.RaceTemplate;

Then in my OnNewMagicRound method I do:

Line 576: if (playerRace.ID != (int)Races.Argonian)


This makes my(or all?) OneNewMagicRound crash and start running every frame instead of every 5 seconds.


If I change line 576 to do:
if (playerEntity.RaceTemplate.ID != (int)Races.Argonian)

It works as intended.

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Mods not working in 0.10.24 builds

Post by Ralzar »

So, a summary of the three bugs I experienced in my mods in 0.10.24:

Mod Breaks
If I use [Invoke(StateManager.StateTypes.Start, 0)] and I use a nested reference, or what you'd call it, something seriously breaks. At least if it happens in OnNewMagicRound. See post above for more details.

RestoreSaveData breaks RegisterCustomItem and RegisterCustomActivation
If I use [Invoke(StateManager.StateTypes.Game, 0)] items and sprite activation in the loaded environment breaks. The items lose their className. I do not what happens to make the RegisterCustomActivation break.

GameObjects are named with "Clone" at the end.
Torch Taker (which uses StateTypes.Start,) is unable to identify the light objects placed near vanilla lightsources because sometimes they have gotten "Clone" added at the end of their name. I solved this by simply doing gameObject.name.StartsWith("DaggerfallLight [Dungeon]") but it seems like a weird thing to suddenly start happening.

User avatar
Interkarma
Posts: 7242
Joined: Sun Mar 22, 2015 1:51 am

Re: Mods not working in 0.10.24 builds

Post by Interkarma »

I'm happy to hear you've made good progress. :)

You seem to be hitting a problem we all struggle with in Unity at one point or another, which is "scripts don't always execute in the same order". This can and will change from build to build and from editor to builds. You need to code defensively and always validate input. Be careful of the time/stage you grab references, as the object you want might not have awoken yet.

If I use [Invoke(StateManager.StateTypes.Start, 0)] and I use a nested reference, or what you'd call it, something seriously breaks. At least if it happens in OnNewMagicRound. See post above for more details.
When caching your own references, you should always check reference != null when accessing later. Remember - you can't guarantee execution order in your mod so caching these references early won't always work out for you.

The design of GameManager helps you work around this and does all the reference caching for you. No need to cache these references yourself Init() or Awake(), and definitely don't do it in constructor or field setup. Just call for the race later in post-Start() methods using GameManager.Instance.Player whenever needed. And it's still a good idea to do a null check anyway and skip out if component not returned.

If I use [Invoke(StateManager.StateTypes.Game, 0)] items and sprite activation in the loaded environment breaks. The items lose their className. I do not what happens to make the RegisterCustomActivation break.
This could be impacted by above, best just use StateTypes.Start from here on out. Also if object names change or aren't what is registered, then custom activations won't work. That seems to be case in the third one below.

GameObjects are named with "Clone" at the end.
Game objects cloned from prefabs always get (clone) on the end in my experience. I'm honestly not sure how or why this seems to have changed between builds. I also note the difference you describe, but it's not something we've (intentionally) changed from build to build. Sometimes Unity just gonna Unity. BadLuckBurt had a pretty good workaround on page 2.

Code: Select all

If(gameObject.name.StartsWith("DaggerfallLight [Dungeon]"))

From the sounds of it, you're in a position to move forwards and generate new mod builds that are functional? If nothing else, we have a better idea of where the cracks are and can start work on filling them. Good luck! :)

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Mods not working in 0.10.24 builds

Post by Ralzar »

Yeah, I think everything is under control (barely) again now :D I released a new C&C build on Nexus that from my testing appears to have solved all issues.

C&C is my big "learning as I go" mod, so there will be a lot of flaws in it. Some of that code was written within days of figuring out how to even use C# at all.
The plan was, and is, to go back in a while and just re-write the whole mod from scratch. As there are several minor oddities in how it functions ingame. Not to mention that, as this incident made blatantly clear, it is voulnerable to breaking and difficult to fix since I barely know what it all does any more.

Thank you so much for the help and feedback.

Post Reply