Hello and thanks for taking the time to review my question.
My question: how do I pass classes of a parent class to a function's parameter?
What I HAVE, and what I WANT to do:
HAVE:
- I have a "DataContainerScript" which has several classes.
- I have a "SavingScript" which has a 'Save' function that checks if a file exists. If it does, then deserialize it (which opens it) and assign the 'fullData' variable to this deserialized file as a FullDatClass type. If the file doesn't exist, then simply create one.
- Then it checks what scene we're in, and starts the 'SaveToVariableScene' function, which saves the data shown in the script.
- Then at the end, it serializes the 'playerSaveFile' with 'fullData' and closes it.
WANT:
- So at the end of the day, what I FINALLY want to do is have a parameter of 'SaveToVariableScene' that I can feed from whichever if statement's body, which ideally would call the 'SaveToVariableScene' function with parameter: "fullData.[sceneDataContainer]"; and then 'SaveToVariableScene' function would set fullData's variables to match the GameControl's (GameControl is a static script found in every scene).
DataContainerScript:
using System;
using System.Collections.Generic;
[Serializable]
public class FullDataClass
{
public GameData gameData = new GameData();
public PlayerItemsData playerItemsData = new PlayerItemsData();
public PlayerPositionData playerPositionData = new PlayerPositionData();
public PlayerhouseData playerhouseData = new PlayerhouseData();
public IslandData islandData = new IslandData();
}
[Serializable]
public class GameData
{
public string playerScene;
}
[Serializable]
public class PlayerItemsData
{
public string currentlyHoldingItemName;
}
[Serializable]
public class PlayerPositionData
{
public float playerPosX;
public float playerPosY;
public float playerPosZ;
}
[Serializable]
public class PlayerhouseData
{
public bool hasPlayerVisitedThisScene;
// entities
public List polygonCiviliansX;
public List polygonCiviliansY;
public List polygonCiviliansZ;
// items
public List playerPoopX;
public List playerPoopY;
public List playerPoopZ;
}
[Serializable]
public class IslandData
{
public bool hasPlayerVisitedThisScene;
// entities
public List polygonCiviliansX;
public List polygonCiviliansY;
public List polygonCiviliansZ;
// items
public List playerPoopX;
public List playerPoopY;
public List playerPoopZ;
}
And SavingScript:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SavingScript : MonoBehaviour {
public static SavingScript savingControl;
void Awake ()
{
if(savingControl == null)
{
DontDestroyOnLoad(gameObject);
savingControl = this;
}
else if(savingControl != this)
{
Destroy(gameObject);
}
}
public void Save (bool savePlayerItems, bool savePlayerScene, bool, savePlayerPositions)
FileStream playerSaveFile;
FullDataClass fullData = new FullDataClass();
BinaryFormatter newBF = new BinaryFormatter();
if (File.Exists(Application.persistentDataPath + "MySave.data"))
{
playerSaveFile = File.Open(Application.persistentDataPath + "MySave.data", FileMode.Open);
fullData = (FullDataClass)newBF.Deserialize(playerSaveFile);
}
else
{
playerSaveFile = File.Create(Application.persistentDataPath + "MySave.data");
}
#region Player info
if(savePlayerItems)
{
fullData.playerItemsData.currentlyHoldingItemName = GameControl.control.currentlyHoldingItemName;
fullData.playerItemsData.isAtomicPoopCompressorEquipped = GameControl.control.isAtomicPoopCompressorEquipped;
}
if(savePlayerPositions)
{
fullData.playerPositionData.playerPosX = GameControl.control.playerPosX;
fullData.playerPositionData.playerPosY = GameControl.control.playerPosY;
fullData.playerPositionData.playerPosZ = GameControl.control.playerPosZ;
}
if(savePlayerScene)
{
fullData.gameData.playerScene = GameControl.control.playerScene;
}
#endregion
#region Scene save section
// check what scene we're saving to...
if(Application.loadedLevelName == "Scene_tier1_playerhouse")
{
SaveToVariableScene(fullData.playerhouseData?);
}
else if(Application.loadedLevelName == "Scene_island")
{
SaveToVariableScene();
}
else if(Application.loadedLevelName == "Scene_tier1_polygonCivilianComplex_entrance0")
{
SaveToVariableScene();
}
#endregion
newBF.Serialize(playerSaveFile, fullData);
playerSaveFile.Close();
}
void SaveToVariableScene (FullDataClass fullData)
{
fullData.hasPlayerVisitedThisScene= GameControl.control.hasPlayerVisitedThisScene;
fullData.polygonCiviliansX = GameControl.control.polygonCiviliansX;
fullData.polygonCiviliansY = GameControl.control.polygonCiviliansY;
fullData.polygonCiviliansZ = GameControl.control.polygonCiviliansZ;
fullData.playerPoopX = GameControl.control.playerPoopX;
fullData.playerPoopY = GameControl.control.playerPoopY;
fullData.playerPoopZ = GameControl.control.playerPoopZ;
}
Thanks for any advice/answers/knowledge/help in advance! :)
↧