Dictionary alternatives? (specific code)

Hi,

I’ve used a tutorial to make a highscoreboard that uses Dictionary and it’s not compatible with Photon.

Does anyone know how to change this line :

    Dictionary< string, Dictionary<string, int> > playerScores;

-into something that does the same but that would work with photon?

All help is greatly appreciated

try and check whether Dictionary<string, int> works
if it does, we’ll figure something out

1 Like

Thank you for your help. I’ve found out that it’s never going to work with photon because of how much info needs to be sent, unfortunately. I’ll try something different altogether. Have a great day

1 Like

the problem was in serialization.
Dictionary<string, Dictionary<string, int>> is a deeply nested dictionary, and that kind of serialization is not supported.

An old-style hack is to manually (in code) flatten it. Turn it into an array of strings with entries like “35#dogs#loud”. That’s basically what XML does, automatically converting and unconverting, but wastes a lot of space.

If you intend to lift your entire current implementation, your easiest route is probably to make your own serializable classes to store the same data the dictionary does.

using System

[Serializable]
public class StringAndList {
    public string name;
    public List<StringAndInt> scores;
}
[Serializable]
public class StringAndInt {
    public string name;
    public int amt;
}

//in use
List<StringAndList> playerScores

Can’t recall now for photon RPC, but RaiseEvent will need to serialize into a byte array on the sender, and deserialize back into a class on the recipient. You’ll need to look into binary formatter for that.

2 Likes

exactly, that’s what I meant to suggest as a solution

that’s a decent workaround

Thanks everybody for your help. I’ve decided to work around the issue with a different approach, but will definitely remember the alternatives for Dictionary for the future :wink:

It’s may be helpful or Nested dictionary alternative.

Create Sample.cs script and test it.

 public Dictionary<string,Tuple<string,string,string,int>> _planets = new Dictionary<string, Tuple<string,string, string, int>>();
    void Start()
        {
          
           string myKey = string.Concat("1","MetalMine","Level");
           if(!_planets.ContainsKey(myKey))
           {
               _planets.Add(myKey,Tuple.Create("1","MetalMine","Level",0));
           }
           Debug.Log("_planets mykey "+myKey+" ==> "+_planets[myKey].Item4);
        }