How do i Make this saving system apply the save data in my game.
Hi I followed the Brackerys save and load tutorial, and I thought I would implement something similar in my game. But I don't know how to tell the game to use the specific variables as data.
So I'm currently making the Main Menu Scene where the player can change Sounds, music, and other settings. I just want players' data, so when the player starts the game Music and Sound will be at the same levels and if the player reopens the game, they should have the same values.
What I tried: Here's the SettingsData Script. This script controls the volume sliders and the toggle buttons.
[Header("Options")]
[SerializeField]
private AudioMixer audioMixer;
[Space]
public bool speedRunClock; //Public becuase this is data that needs to be saved when the game is starting & if true the game will start with a timer
public bool fullScreenMode; //If true then the game will be played in fullsreen
public bool cameraShakeEffect; //If true then there will be camera shake effect in the game
[Space]
public float currentVolumeForMaster;
public float currentVolumeForSoundFX;
public float currentVolumeForMusic;
// Start is called before the first frame update
void Start()
{
//If there is no data don't load this
LoadSettingsData();
}
#region OptionsMenu
//This might not be needed
//This function is done in MessySlider Script
public void SetMasterVolume(float volume)
{
audioMixer.SetFloat("MasterVolume", Mathf.Log10(volume) * 20);
volume = currentVolumeForMaster;
}
public void SetSoundFXVolume(float SoundsVolume)
{
audioMixer.SetFloat("soundsVolume", Mathf.Log10(SoundsVolume) * 20);
SoundsVolume = currentVolumeForSoundFX;
}
public void SetMusicVolume(float musicVolume)
{
audioMixer.SetFloat("musicVolume", Mathf.Log10(musicVolume) * 20);
musicVolume = currentVolumeForMusic;
}
public void SetFullScreen(bool isFullScreen)
{
Screen.fullScreen = isFullScreen;
fullScreenMode = isFullScreen;
}
public void SpeedRunClockActivated(bool _SpeedRunClock)
{
speedRunClock = _SpeedRunClock;
}
public void CameraShakeEffect(bool isCameraShakeEffect)
{
cameraShakeEffect = isCameraShakeEffect;
}
#endregion
public void SaveSettingsData()
{
SaveSystem.SavePlayerPreferences(this);
}
public void LoadSettingsData()
{
SavingSettingsData savingSettingsData = SaveSystem.LoadPlayerPreferences();
//Bool values
speedRunClock = savingSettingsData.speedrunClock_Save;
fullScreenMode = savingSettingsData.fullScreenMode_Save;
cameraShakeEffect = savingSettingsData.cameraShake_Save;
//Slider aka the Float value
currentVolumeForMaster = savingSettingsData.masterVolume;
currentVolumeForMusic = savingSettingsData.musicVolume;
currentVolumeForSoundFX = savingSettingsData.soundFXVolume;
}
I then have another script where I will create classes that use the data above and store it in different variables.
[System.Serializable]
public class SavingSettingsData
{
public float masterVolume;
public float musicVolume;
public float soundFXVolume;
public bool speedrunClock_Save;
public bool fullScreenMode_Save;
public bool cameraShake_Save;
public SavingSettingsData(SettingsDataManager settings)
{
//This will only work if the data from SettingsDataManager is converted to currentvolumeversion
masterVolume = settings.currentVolumeForMaster;
musicVolume = settings.currentVolumeForMusic;
soundFXVolume = settings.currentVolumeForSoundFX;
//public bools
speedrunClock_Save = settings.speedRunClock;
fullScreenMode_Save = settings.fullScreenMode;
cameraShake_Save = settings.cameraShakeEffect;
}
}
Now for the last script, I wanted to create a saving system that actually saves all the data on the computer.
public static class SaveSystem
{
public static void SavePlayerPreferences (SettingsDataManager settings)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "Player.Preferences";
FileStream stream = new FileStream(path, FileMode.Create);
SavingSettingsData settingsData = new SavingSettingsData(settings);
formatter.Serialize(stream, settingsData);
stream.Close();
}
public static SavingSettingsData LoadPlayerPreferences()
{
string path = Application.persistentDataPath + "Player.Preferences";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
SavingSettingsData savingSettingsData = formatter.Deserialize(stream) as SavingSettingsData;
stream.Close();
return savingSettingsData;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
I think the problem is currently in my SettingsData script because there is no time where I'm telling the program that for example, currentVolumeForMaster should be used as a volume. So my questions have can save the Player Settings Data and load it when switching to another scene or when the player reopens the game? And another quick question is; If this saves system works will the slider remember its slider value and fill the area accordingly?
Your answer
Follow this Question
Related Questions
[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer
How To Save and Load player position using Serialization? 0 Answers
Saveing and Loading Problem 0 Answers
Saving current progression question 0 Answers
Vroid Opening Incorrect Save,Vroid opens the model above the one I click 0 Answers