How to get my custom saver to wait a few seconds
Posted: Mon Feb 26, 2024 1:23 am
Hay there, I'm trying to get my pins to remember there connections using the spawnedObjects components "key" identifier that is generated every time an object is spawned into the scene. The issue im having is that if I have an array of items that need to find there connections using the "key" the first one in the list is not remembering it's connection. However if I do everything in reverse (like i did in the second vid) things remember.
If I had to guess it's that each pinSaver script is running in the order it's found in the hierarchy. Which means while the first object dose remember which spawned object key to look for, that object which is further down the list has not loaded it's key yet, so the first object can't find what it's looking for. If this is the case do you think I could save a list of objects that can have components assigned to it when I save? I think there called structs but its 12:30am now and I cant remember. Perhaps this way I can use this external pin saver to link everything up after it loads everything first. So load components, then connect.
Any idea why and how to save and load all my things and everyone find everyone else? I'll provide the saver so you have an idea what I'm trying to do.
Sorry for the lack of audio I thought my mic was recording...apparently not. But I think you can see the problem.
Script:
using System;
using System.Collections;
using PixelCrushers;
using UnityEngine;
using UnityEngine.UI;
public class PinSaver : Saver
{
[SerializeField] GameObject corkBoard;
public string point_1name;
[SerializeField] DrawLine drawLine;
[Serializable]
public class Data
{
public string point_1name;
public string myKey;
}
/*
private void Start()
{
if (corkBoard == null)
corkBoard = GameObject.FindGameObjectWithTag("CorkBoard");
}*/
public override string RecordData()
{
var data = new Data();
// Save name of texture
data.point_1name = point_1name;
// Save my own key
data.myKey = this.GetComponentInParent<SpawnedObject>().key;
drawLine.myKey = data.myKey;
/*if (data.point_1name != null)
print(data.point_1name);*/
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return; // If we didn't receive any save data, exit immediately.
var data = SaveSystem.Deserialize<Data>(s);
if (data == null) return; // Serialized string isn't valid.
// Assign point 1 name
point_1name = data.point_1name;
drawLine.point_1PinName = data.point_1name;
// Assign myKey
drawLine.myKey = data.myKey;
if (data.point_1name != String.Empty)
{
// (1)My array to loop threw...the parent of every object on board
GameObject[] corkBoard;
// (2)
corkBoard = GameObject.FindGameObjectsWithTag("Pin");
// (3)
for (int i = 0; i < corkBoard.Length; i++)
{
//print(corkBoard.name + " " + i);
print("My key is " + corkBoard.transform.GetComponent<DrawLine>().myKey);
if (corkBoard.transform.GetComponent<DrawLine>().myKey == point_1name)
{
//print(data.point_1name);
//print(corkBoard.transform.GetComponent<SpawnedObject>().key);
drawLine.point_1 = corkBoard.transform;
print("I found my object");
return;
}
else
{
print("Nothing. If there is a connection something is still wrong. My key is " + corkBoard.transform.GetComponent<DrawLine>().myKey);
}
}
}
}
}
Video 1:
Video 2:
If I had to guess it's that each pinSaver script is running in the order it's found in the hierarchy. Which means while the first object dose remember which spawned object key to look for, that object which is further down the list has not loaded it's key yet, so the first object can't find what it's looking for. If this is the case do you think I could save a list of objects that can have components assigned to it when I save? I think there called structs but its 12:30am now and I cant remember. Perhaps this way I can use this external pin saver to link everything up after it loads everything first. So load components, then connect.
Any idea why and how to save and load all my things and everyone find everyone else? I'll provide the saver so you have an idea what I'm trying to do.
Sorry for the lack of audio I thought my mic was recording...apparently not. But I think you can see the problem.
Script:
using System;
using System.Collections;
using PixelCrushers;
using UnityEngine;
using UnityEngine.UI;
public class PinSaver : Saver
{
[SerializeField] GameObject corkBoard;
public string point_1name;
[SerializeField] DrawLine drawLine;
[Serializable]
public class Data
{
public string point_1name;
public string myKey;
}
/*
private void Start()
{
if (corkBoard == null)
corkBoard = GameObject.FindGameObjectWithTag("CorkBoard");
}*/
public override string RecordData()
{
var data = new Data();
// Save name of texture
data.point_1name = point_1name;
// Save my own key
data.myKey = this.GetComponentInParent<SpawnedObject>().key;
drawLine.myKey = data.myKey;
/*if (data.point_1name != null)
print(data.point_1name);*/
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return; // If we didn't receive any save data, exit immediately.
var data = SaveSystem.Deserialize<Data>(s);
if (data == null) return; // Serialized string isn't valid.
// Assign point 1 name
point_1name = data.point_1name;
drawLine.point_1PinName = data.point_1name;
// Assign myKey
drawLine.myKey = data.myKey;
if (data.point_1name != String.Empty)
{
// (1)My array to loop threw...the parent of every object on board
GameObject[] corkBoard;
// (2)
corkBoard = GameObject.FindGameObjectsWithTag("Pin");
// (3)
for (int i = 0; i < corkBoard.Length; i++)
{
//print(corkBoard.name + " " + i);
print("My key is " + corkBoard.transform.GetComponent<DrawLine>().myKey);
if (corkBoard.transform.GetComponent<DrawLine>().myKey == point_1name)
{
//print(data.point_1name);
//print(corkBoard.transform.GetComponent<SpawnedObject>().key);
drawLine.point_1 = corkBoard.transform;
print("I found my object");
return;
}
else
{
print("Nothing. If there is a connection something is still wrong. My key is " + corkBoard.transform.GetComponent<DrawLine>().myKey);
}
}
}
}
}
Video 1:
Video 2: