I have been trying all day to get this to sync from server to client, funny thing is the size of the list syncs up. It says there is 63 items, but every value is completely null. Am I doing something wrong? What am I missing?
Its for an inventory data base, and I know I could have them all load the same file at start, but I wanted servers to be able to customize them the way they want to.
I have 2 lists that need syncing, one that contains the values each player will have, these will always be changing, like quantity, health, and such.
Then I have another list that takes care of all the other things like descriptions, smelting times, stuff like that.
Here is the code. Help please! and thank you!
Code
public class InventoryDataBase : NetworkBehaviour {
public GameManager gameManager;
//public GameManager_ServerOptions serverOptions;
public class SyncListInventoryData : SyncListStruct<InventoryData>{ }
public SyncListInventoryData inventoryDataBase = new SyncListInventoryData();
public class SyncListItemData : SyncListStruct<ItemData>{ }
public SyncListItemData itemDataBase = new SyncListItemData();
private JsonData itemData;
public bool itemDataLoaded, itemTempLoaded;
public bool clientNeedsToSync;
[SyncVar]
public int sizeOfDataBase;
void Start()
{
//SetInitialReferences();
gameManager = GetComponent<GameManager>();
StartCoroutine(WhenGameManagerReady());
}
IEnumerator WhenGameManagerReady()
{
//yield return new WaitUntil(GameManagerReady);
while (gameManager == null)
{
yield return null;
}
yield return new WaitUntil(gameManager.ServerDataReady);
while(gameManager.weKnowIfWeAreServer == false)
{
yield return null;
}
if (gameManager.isServer)
{
LoadData();
}
else
{
itemDataBase.Callback = OnStructItemDataChanged;
inventoryDataBase.Callback = OnStructInventoryDataChanged;
StartCoroutine(WaitForSync());
}
}
void LoadData()
{
if (!OverdoseNetworkManager.isVanilla)
{
itemData = gameManager.dataItems;
}
else
{
//itemData = JsonMapper.ToObject(File.ReadAllText(Application.streamingAssetsPath + "/Items.json"));
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/ItemData.json"));
}
Debug.Log("InventoryDataBase: LoadData() Called, is this game vanilla? OverdoseNetworkManager.isVanilla = " + OverdoseNetworkManager.isVanilla);
ConstructItemDatabase();
}
int i = 0;
void Update()
{
if(itemDataLoaded)
{
if(Input.GetButtonDown("DoAllDebug"))
{
Debug.Log(inventoryDataBase[i].Slug);
i++;
}
}
}
void ConstructItemDatabase()
{
for (int i = 0; i < itemData.Count; i++)
{
inventoryDataBase.Add(new InventoryData((int)itemData[i]["id"], (string)itemData[i]["title"], (string)itemData[i]["description"], (int)itemData[i]["value"],
(int)itemData[i]["maxQuantity"], (int)itemData[i]["maxSpecialQuantity"], (string)itemData[i]["type"], (string)itemData[i]["secondType"], (string)itemData[i]["specialType"], (string)itemData[i]["specialSecond"],
(string)itemData[i]["specialThird"], (string)itemData[i]["specialFourth"], (int)itemData[i]["attachments"], (bool)itemData[i]["smuggle"], (string)itemData[i]["ammoType"],
(string)itemData[i]["attachmentTypeA"], (string)itemData[i]["attachmentTypeB"],
(string)itemData[i]["attachmentTypeC"], (string)itemData[i]["attachmentTypeD"], (string)itemData[i]["attachmentTypeE"], (string)itemData[i]["attachmentTypeF"],
(string)itemData[i]["attachmentTypeG"], (string)itemData[i]["attachmentTypeH"], (string)itemData[i]["attachmentTypeI"], (string)itemData[i]["attachmentTypeJ"],
(bool)itemData[i]["battery"], (bool)itemData[i]["fuel"], (int)itemData[i]["fuelBurn"], (bool)itemData[i]["stackable"], (bool)itemData[i]["smeltable"], (int)itemData[i]["smeltID"],
(int)itemData[i]["smeltAmount"], (int)itemData[i]["smeltTime"], (string)itemData[i]["slug"]));
itemDataBase.Add(new ItemData((int)itemData[i]["id"]));
}
itemDataLoaded = true;
sizeOfDataBase = inventoryDataBase.Count;
}
IEnumerator WaitForSync()
{
while (sizeOfDataBase != inventoryDataBase.Count)
{
yield return null;
}
itemDataLoaded = true;
}
private void OnStructItemDataChanged(SyncListStruct<ItemData>.Operation op, int index)
{
Debug.Log("list changed " + op);
}
private void OnStructInventoryDataChanged(SyncListStruct<InventoryData>.Operation op, int index)
{
Debug.Log("list changed " + op);
}
public InventoryData FetchInventoryDataByID(int id)
{
for (int i = 0; i < inventoryDataBase.Count; i++)
if (inventoryDataBase[i].ItemID == id)
return inventoryDataBase[i];
return inventoryDataBase[0];
}
public ItemData FetchItemDataByID(int id)
{
for (int i = 0; i < itemDataBase.Count; i++)
if (itemDataBase[i].ItemID == id)
return itemDataBase[i];
return itemDataBase[0];
}
public static ItemData CreateNewItemData(ItemData oldData)
{
ItemData newData = new ItemData();
newData.ItemID = oldData.ItemID;
newData.Quantity = oldData.Quantity;
newData.Health = oldData.Health;
newData.Power = oldData.Power;
newData.SpecialQuantity = oldData.SpecialQuantity;
return newData;
}
}
public struct ItemData
{
public int ItemID { get; set; }
public int Quantity { get; set; }
public int SpecialQuantity { get; set; }
public float Health { get; set; }
public float Power { get; set; }
//public string Slug { get; set; }
public ItemData(int itemId)
{
this.ItemID = itemId;
this.Quantity = 1;
this.SpecialQuantity = 0;
this.Health = 1000;
this.Power = 0;
//this.Slug = slug;
}
}
public struct InventoryData
{
public int ItemID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Value { get; set; }
public int MaxQuantity { get; set; }
public int MaxSpecialQuantity { get; set; }
public float Health { get; set; }
public string Type { get; set; }
public string SecondType { get; set; }
public string SpecialType { get; set; }
public string SpecialSecond { get; set; }
public string SpecialThird { get; set; }
public string SpecialFourth { get; set; }
public int Attachments { get; set; }
public bool Smuggle { get; set; }
public string AmmoType { get; set; }
public string AttachmentTypeA { get; set; }
public string AttachmentTypeB { get; set; }
public string AttachmentTypeC { get; set; }
public string AttachmentTypeD { get; set; }
public string AttachmentTypeE { get; set; }
public string AttachmentTypeF { get; set; }
public string AttachmentTypeG { get; set; }
public string AttachmentTypeH { get; set; }
public string AttachmentTypeI { get; set; }
public string AttachmentTypeJ { get; set; }
public bool Battery { get; set; }
public bool Fuel { get; set; }
public int FuelBurn { get; set; }
public bool Stackable { get; set; }
public bool Smeltable { get; set; }
public int SmeltID { get; set; }
public int SmeltAmount { get; set; }
public int SmeltTime { get; set; }
public string Slug { get; set; }
//public Sprite ItemIcon { get; set; }
//public GameObject Item3P { get; set; }
//public GameObject ItemFP { get; set; }
//public GameObject ItemGround { get; set; }
public InventoryData(int itemId, string title, string description, int value, int maxQuantity, int maxSpecialQuantity, string type, string secondType, string specialType, string specialSecond, string specialThird,
string specialFourth, int attachments, bool smuggle, string ammoType, string attachmentTypeA,
string attachmentTypeB, string attachmentTypeC, string attachmentTypeD, string attachmentTypeE, string attachmentTypeF, string attachmentTypeG, string attachmentTypeH,
string attachmentTypeI, string attachmentTypeJ, bool battery, bool fuel, int fuelBurn, bool stackable, bool smeltable, int smeltID, int smeltAmount, int smeltTime, string slug)
{
this.ItemID = itemId;
this.Title = title;
this.Description = description;
this.Value = value;
this.MaxQuantity = maxQuantity;
this.MaxSpecialQuantity = maxSpecialQuantity;
this.Health = 1000;
this.Type = type;
this.SecondType = secondType;
this.SpecialType = specialType;
this.SpecialSecond = specialSecond;
this.SpecialThird = specialThird;
this.SpecialFourth = specialFourth;
this.Attachments = attachments;
this.Smuggle = smuggle;
this.AmmoType = ammoType;
this.AttachmentTypeA = attachmentTypeA;
this.AttachmentTypeB = attachmentTypeB;
this.AttachmentTypeC = attachmentTypeC;
this.AttachmentTypeD = attachmentTypeD;
this.AttachmentTypeE = attachmentTypeE;
this.AttachmentTypeF = attachmentTypeF;
this.AttachmentTypeG = attachmentTypeG;
this.AttachmentTypeH = attachmentTypeH;
this.AttachmentTypeI = attachmentTypeI;
this.AttachmentTypeJ = attachmentTypeJ;
this.Battery = battery;
this.Fuel = fuel;
this.FuelBurn = fuelBurn;
this.Stackable = stackable;
this.Smeltable = smeltable;
this.SmeltID = smeltID;
this.SmeltAmount = smeltAmount;
this.SmeltTime = smeltTime;
this.Slug = slug;
//this.ItemIcon = Resources.Load<Sprite>("Inventory/Icons/" + "Icon_" + slug);
//this.ItemFP = Resources.Load<GameObject>("ItemPrefabs/FirstPerson/" + "ItemFP_" + slug);
//this.Item3P = Resources.Load<GameObject>("ItemPrefabs/" + "Item3P_" + slug);
//this.ItemGround = Resources.Load<GameObject>("ItemPrefabs/ThirdPerson/" + "Item_" + slug);
}
}