Event Handler Issue With Dictionary Keys and Loop

Discuss modding questions and implementation details.
Post Reply
User avatar
Magicono43
Posts: 279
Joined: Tue Nov 06, 2018 7:06 am

Event Handler Issue With Dictionary Keys and Loop

Post by Magicono43 » Sun May 03, 2020 10:17 pm

Alright so am having some problems working on a mod. I'm using a few event handlers as triggers for blocks of code that will run, as one would expect. Problem is, now that I have been using save-data, I have also been using dictionaries in this mod, with what i'm currently trying to do I want to loop through all the values in this dictionary and check each key and a value inside every in-game day using the "OnNewDay" event handler.

All seemed to be going fine, and I tried using an example from the C# docs for a foreach loop that only used the keys from the dictionary, but started seeing some problems with this and did a bunch of trouble-shooting with Burt both were getting the same errors.

Code: Select all

/*private static void Testing_OnNewDay()
        {
            Dictionary<int, ShopData>.KeyCollection sbKeys = ShopBuildingData.Keys;
            
            foreach (int s in sbKeys)
            {
                Debug.LogFormat("Key = {0}", s);
            }
        }
So the event "OnNewDay" works fine, but as soon as the foreach loop is put into this example, it and other event handlers in the same script stop functioning, with what appears to be an exception like this from Unity:

Code: Select all

Error (584): Internal compiler error: Unexpected error when loading type `System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection.Enumerator'
Error (): 0(198,29): SaveDataTestingScript1.SaveDataTestingScript1.Testing_OnTransitionInterior(DaggerfallWorkshop.Game.PlayerEnterExit.TransitionEventArgs)
However, when the code-block is changed to something like this:

Code: Select all

public static void Testing_OnNewDay()
        {    
            int dicCount = ShopBuildingData.Keys.Count;
            Debug.Log("Key count: " + dicCount.ToString());
            foreach( KeyValuePair<int, ShopData> kvp in ShopBuildingData )
            {
                Debug.LogFormat("Key = {0}, Value = {1}", kvp.Key.ToString(), kvp.Value.BuildingQuality.ToString());
            }
        }
It works fine. So I originally though that maybe it was the loop causing a problem, but when Burt showed me this working code, he seems to think it has something to do with how the keys are being retrieved from the custom dictionary.

So we are both not 100% sure why the first example was causing problem, but he thought that maybe somebody would know better on the forums, thanks for your time, hopefully this explains the context well enough.

User avatar
TheLacus
Posts: 1150
Joined: Wed Sep 14, 2016 6:22 pm
Contact:

Re: Event Handler Issue With Dictionary Keys and Loop

Post by TheLacus » Sun May 03, 2020 11:55 pm

There is a compiler issue:

Code: Select all

Error (584): Internal compiler error: Unexpected error when loading type `System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection.Enumerator'
It has been reported in the past that the portable compiler sometimes has issues with generics, even though i never experienced them myself. If the second method you posted works fine i'd say to use it and not to worry too much about this. As an alternative you can precompile the mod and bundle a resulting .dll, skipping run-time compilation step.
Mod System documentation - Learn how to create mods for Daggerfall Unity.
Modder Discussion - General help and discussion for the mod system.
Github Issues - Submit a bug report for the game, including the mod system.

User avatar
Magicono43
Posts: 279
Joined: Tue Nov 06, 2018 7:06 am

Re: Event Handler Issue With Dictionary Keys and Loop

Post by Magicono43 » Mon May 04, 2020 12:16 am

TheLacus wrote:
Sun May 03, 2020 11:55 pm
There is a compiler issue:

Code: Select all

Error (584): Internal compiler error: Unexpected error when loading type `System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection.Enumerator'
It has been reported in the past that the portable compiler sometimes has issues with generics, even though i never experienced them myself. If the second method you posted works fine i'd say to use it and not to worry too much about this. As an alternative you can precompile the mod and bundle a resulting .dll, skipping run-time compilation step.
I think I will take your first point and just not worry too much about this occurrence, lol, especially since I have no clue how to do that last point. When I hear compiler issue in this case, I assume that it's probably something on Unity's side more than anything, at least if i'm understanding that right.

Thanks for the reply Lacus, I think i'll continue on with what is working and not concern myself with errors from the compiler.

User avatar
TheLacus
Posts: 1150
Joined: Wed Sep 14, 2016 6:22 pm
Contact:

Re: Event Handler Issue With Dictionary Keys and Loop

Post by TheLacus » Mon May 04, 2020 12:29 pm

Not really related to Unity, but to the C# compiler that is included with the game. It allows to compile raw C# scripts at runtime, which is great, but has a few limitations.

Good luck with your mod. :)
Mod System documentation - Learn how to create mods for Daggerfall Unity.
Modder Discussion - General help and discussion for the mod system.
Github Issues - Submit a bug report for the game, including the mod system.

Post Reply