Hi all,
I’ve created a class that has a number of structs of key(string)-value(object) pairs for ease of sending messages over the network, but I’ve run into an interesting problem. For one struct and only one struct, when I try to set the key and later get the value, it returns null.
Here’s the class (shortened from over 1700 lines)
[Serializable]
public class messageItem
{
[Serializable]
struct stringPairs //All of the structs are pretty much like this, just have different types for values
{
public string key;
public string value;
}
[Serializable]
struct intPairs //works fine, removed the lines
[Serializable]
struct intArrayPairs //works fine, removed the lines
[Serializable]
struct itemPairs //works fine, removed the lines
[Serializable]
struct vector3Pairs //works fine, removed the lines
[Serializable]
struct boolPairs //works fine, removed the lines
[Serializable]
struct characterPairs //works fine, removed the lines
[Serializable]
struct messagePairs //This one is the problem
{
[SerializeField]
public string key;
[SerializeField]
List<stringPairs> strings; //All of these hold and return the proper assigned values.
[SerializeField]
List<intPairs> ints;
[SerializeField]
List<intArrayPairs> intArrays;
[SerializeField]
List<itemPairs> items;
[SerializeField]
List<vector3Pairs> vector3s;
[SerializeField]
List<boolPairs> bools;
[SerializeField]
List<characterPairs> characters;
#region methods
public static implicit operator messagePairs(messageItem item)
{
messagePairs returnItem = new messagePairs();
returnItem.strings = item.strings;
returnItem.ints = item.ints;
returnItem.intArrays = item.intArrays;
returnItem.items = item.items;
returnItem.vector3s = item.vector3s;
returnItem.bools = item.bools;
returnItem.characters = item.characters;
return returnItem;
}
public static implicit operator messageItem(messagePairs item)
{
messageItem returnItem = new messageItem();
returnItem.strings = item.strings;
returnItem.ints = item.ints;
returnItem.intArrays = item.intArrays;
returnItem.items = item.items;
returnItem.vector3s = item.vector3s;
returnItem.bools = item.bools;
returnItem.characters = item.characters;
return returnItem;
}
//A bunth of public methods to deal with the above lists
#endregion
}
[SerializeField]
List<stringPairs> strings = new List<stringPairs>();
[SerializeField]
List<intPairs> ints = new List<intPairs>();
[SerializeField]
List<intArrayPairs> intArrays = new List<intArrayPairs>();
[SerializeField]
List<itemPairs> items = new List<itemPairs>();
[SerializeField]
List<vector3Pairs> vector3s = new List<vector3Pairs>();
[SerializeField]
List<boolPairs> bools = new List<boolPairs>();
[SerializeField]
List<characterPairs> characters = new List<characterPairs>();
[SerializeField]
List<messagePairs> messages = new List<messagePairs>();
#region methods
//A bunch of methods to deal with the above lists.
public void putObject(string key, messageItem value)
{
messagePairs newMessage = new messagePairs();
int index = characters.FindIndex(x => x.key == key);
if (index >= 0)
{
throw new Exception("Key " + key + " already exists in messages.");
}
else
{
Debug.Log("Key is: " + key); //Value is the correct one put into the method
newMessage.key = key;
newMessage = value;
messages.Add(newMessage);
Debug.Log("Message item key is: " + newMessage.key); //newMessage.key is blank
}
}
//More methods dealing with the messages list
public int messageSize(out string[] keys)
{
keys = new string[messages.Count];
for(int i = 0; i < keys.Length; i++)
{
keys[i] = messages[i].key; //If you debug these, it returns null.
}
return messages.Count;
}
#endregion
}
I cannot, for the life of me, figure out why the struct key value keeps returning as null or “”