How to pass classes of a class to a function's parameter?

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<float> polygonCiviliansX;
    public List<float> polygonCiviliansY;
    public List<float> polygonCiviliansZ;

    // items
    public List<float> playerPoopX;
    public List<float> playerPoopY;
    public List<float> playerPoopZ;
}
[Serializable]
public class IslandData
{
    public bool hasPlayerVisitedThisScene;

    // entities
    public List<float> polygonCiviliansX;
    public List<float> polygonCiviliansY;
    public List<float> polygonCiviliansZ;

    // items
    public List<float> playerPoopX;
    public List<float> playerPoopY;
    public List<float> 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! :slight_smile:

I, the author of this post, have answered my own question! Thanks for the comment _Gkxd though, you really did lead me on the right path to solving this.

The solution: Since in my ‘DataContainerScript’ I had DIFFERENT class names, although each of them (except player data and game data) had the SAME data in them, I decided to create ONE class called “GeneralSceneData” which would be referenced depending on the scene to load, or the scene to save. This way, the amount of code that I would have to write would be reduced by 22 times… for 4 SCRIPTS IN TOTAL!

In a nutshell, I fixed my problem by optimizing my code to suit my needs.

I will leave this post up for anyone having similar problems, ie. trying to feed DIFFERENT classes through ONE function’s parameter, in order to reduce repetitive code.

Final note: if someone is making a game with for example 100 levels, each with different data needed to be saved, then atleast with my current knowledge, I would have to say that that is an extremely rare case, or you simply don’t know what you’re doing.

Thanks and hope this helps anyone!