Try to load from multiple save list.
Posted: Sun Feb 18, 2024 11:51 pm
Hi, I am trying to have the ability to load and save when the player wants. I have set up a save function that allows the player to name their save. and a load function that lists of all save files there are. But I am having an issue with it. I am not sure if i am maybe misunderstanding some of the functions of ES3 or there is something else wrong. Was hoping maybe someone might be able to give a pointer on what may be wrong. Currently, no matter what save I am trying to load, it loads the most recent save.
My save and load function
Code to list all files and determine what the buttons do
My save and load function
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using UnityEngine.UI;
public class SaveLoadManager : MonoBehaviour
{
[SerializeField] TMP_InputField inputField;
[SerializeField] Transform saveButton;
string inputText;
private void Awake()
{
if (PlayerPrefs.HasKey("SelectedSave"))
{
string saveName = PlayerPrefs.GetString("SelectedSave");
ES3AutoSaveMgr.Current.settings.path = saveName;
ES3AutoSaveMgr.Current.Load();
}
saveButton.GetComponent<Button>().onClick.AddListener(() =>
{
updateInputText(inputField.text + ".es3");
});
}
void Update()
{
}
void updateInputText(string inputText)
{
this.inputText = inputText;
Debug.Log(this.inputText);
saveGame();
}
void saveGame()
{
ES3AutoSaveMgr.Current.settings.path = this.inputText.ToString();
ES3AutoSaveMgr.Current.Save();
}
}
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using System.Linq;
using UnityEngine.SceneManagement;
public class LoadSystem : MonoBehaviour
{
[SerializeField] Transform loadButtonTemplate;
float offsetValue = -70;
float xValue = 120.0f;
System.DateTime newestDateTime = new System.DateTime(0);
private void Awake()
{
PlayerPrefs.DeleteKey("SelectedSave");
var saveFiles = ES3.GetFiles().OrderByDescending(f => ES3.GetTimestamp(f)).ToArray();
loadButtonTemplate.gameObject.SetActive(false);
int index = 0;
for (int i = saveFiles.Length - 1; i >= 0; i--)
{
var filename = saveFiles[i];
Transform button = Instantiate(loadButtonTemplate, transform);
button.gameObject.SetActive(true);
button.GetComponent<RectTransform>().anchoredPosition = new Vector2(xValue, offsetValue * index);
index++;
string fileName = System.IO.Path.GetFileNameWithoutExtension(filename);
button.Find("fileName").GetComponent<TextMeshProUGUI>().text = fileName;
button.GetComponent<Button>().onClick.AddListener(() =>
{
//string selectedFilename = filename;
string selectedFilename = button.Find("fileName").GetComponent<TextMeshProUGUI>().text.ToString();
PlayerPrefs.SetString("SelectedSave", selectedFilename);
GameSceneManager.load(GameSceneManager.Scene.gameScene);
});
}
}
}