Forum rules - please read before posting.

problem with making custom script for ui save /load

i have this error ;Assets\LastMan\GraphicOptions\LoadManager.cs(53,33): error CS0120: An object reference is required for the non-static field, method, or property 'SaveSystem.GetSaveFile(int)'

using UnityEngine;
using UnityEngine.UI;
using AC; // Namespace for Adventure Creator
using Michsky.MUIP;
using System.Collections.Generic;

public class LoadManager : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private ButtonManager loadButton; // Using ButtonManager from Modern UI Pack
[SerializeField] private Transform saveSlotParent; // Parent object for save slots
[SerializeField] private GameObject saveSlotPrefab; // Prefab for save slots

private List<SaveFile> saveFiles = new List<SaveFile>();

private void Awake()
{
    if (loadButton != null)
    {
        loadButton.buttonText = "Load";
        loadButton.useCustomContent = true;
        loadButton.onClick.AddListener(PopulateSaveSlots); // Populate the save slots list
    }

    PopulateSaveSlots();
}

private void LoadGame(int saveID)
{
    if (saveID >= 0 && saveID < saveFiles.Count)
    {
        SaveFile saveFile = saveFiles[saveID];
        if (saveFile != null)
        {
            SaveSystem.LoadGame(saveFile); // Load the game
            Debug.Log("Game loaded from slot " + (saveID + 1));
        }
    }
}

private void PopulateSaveSlots()
{
    foreach (Transform child in saveSlotParent)
    {
        Destroy(child.gameObject);
    }

    saveFiles.Clear();
    int numSaves = Mathf.Min(SaveSystem.GetNumSlots()); // Limit to the number of save slots
    for (int i = 0; i < numSaves; i++)
    {
        SaveFile saveFile = SaveSystem.GetSaveFile(i); // Retrieve save file by index
        if (saveFile != null)
        {
            saveFiles.Add(saveFile);

            GameObject slot = Instantiate(saveSlotPrefab, saveSlotParent);
            ButtonManager slotButton = slot.GetComponent<ButtonManager>();
            string saveLabel = SaveSystem.GetSaveSlotLabel(i, i, false); // Get the save slot label
            slotButton.buttonText = saveLabel;
            slotButton.useCustomContent = true;
            int saveID = i; // Local variable to avoid issues with delegates
            slotButton.onClick.AddListener(() => LoadGame(saveID));
        }
    }
}

}

Comments

  • The GetSaveFile function is non-static - you need to reference the KickStarter's instance of SaveSystem (instructions can be found here).

    To fix, replace:

        SaveFile saveFile = SaveSystem.GetSaveFile(i);
    

    with:

        SaveFile saveFile = AC.KickStarter.saveSystem.GetSaveFile(i);
    
  • 1 more question i finished it but the issue i dont have multiple id profiles for my saving i made (i made it that player can pick 1 od 3 diffrent profiles name them and load save) saves loads work but only for autosafe profile rest when clicking load arent shown in list my scripts;

    saving;

    using UnityEngine;
    using TMPro;
    using AC; // Namespace for Adventure Creator
    using Michsky.MUIP;

    public class SaveManager : MonoBehaviour
    {
    [SerializeField] private ButtonManager[] saveButtons;
    [SerializeField] private TMP_InputField[] profileNameInputs;

    private void Awake()
    {
        // Ensure the lengths of saveButtons and profileNameInputs are the same
        if (saveButtons.Length != profileNameInputs.Length)
        {
    
            return;
        }
    
        for (int i = 0; i < saveButtons.Length; i++)
        {
            int profileIndex = i;
            saveButtons[i].useCustomContent = true;
            saveButtons[i].onClick.AddListener(() => SaveGame(profileIndex));
        }
    }
    
    private void SaveGame(int profileIndex)
    {
        string profileName = profileNameInputs[profileIndex].text;
        SaveSystem.SaveGame(profileIndex, false, profileName); // Statyczne wywołanie metody SaveGame
        PlayerPrefs.SetString("ProfileName_" + profileIndex, profileName); // Zapisz nazwę profilu
    
    }
    

    }

    loading

    using UnityEngine;
    using TMPro;
    using AC; // Namespace for Adventure Creator
    using Michsky.MUIP;
    using System.Diagnostics;

    public class LoadManager : MonoBehaviour
    {
    [SerializeField] private ButtonManager[] loadButtons;
    [SerializeField] private TMP_InputField[] profileNameInputs;

    private void Awake()
    {
        // Ensure the lengths of loadButtons and profileNameInputs are the same
        if (loadButtons.Length != profileNameInputs.Length)
        {
    
            return;
        }
    
        for (int i = 0; i < loadButtons.Length; i++)
        {
            int profileIndex = i;
            loadButtons[i].useCustomContent = true;
            loadButtons[i].onClick.AddListener(() => LoadGame(profileIndex));
            profileNameInputs[i].text = PlayerPrefs.GetString("ProfileName_" + profileIndex, "Profile " + (i + 1));
        }
    }
    
    private void LoadGame(int profileIndex)
    {
        string profileName = profileNameInputs[profileIndex].text;
        SaveSystem.LoadGame(profileIndex); // Statyczne wywołanie metody LoadGame
    
    }
    

    }

  • edited May 2024

    I'm sorry, but can you rephrase your issue? I'm not clear on your meaning. Screenshots will be welcome too, if possible.

  • i tried to do 3 separee profiles but i have json error now i have 4 separete scripts 1 for starting all 2cond for holding profiles 3rd and 4rth fore save load

  • using System.Diagnostics;
    using UnityEngine;

    public class StartManager : MonoBehaviour
    {
    public ProfileManager profileManager;
    public SaveManager saveManager;
    public LoadManager loadManager;

    private void Start()
    {
        if (profileManager == null)
        {
    
            return;
        }
    
        if (saveManager != null)
        {
            saveManager.profileManager = profileManager;
        }
    
    
        if (loadManager != null)
        {
            loadManager.profileManager = profileManager;
            loadManager.InitializeProfileNames();
        }
    
    }
    

    }

  • using UnityEngine;
    using AC;
    using System.IO;
    using System.Diagnostics;

    [System.Serializable]
    public class ProfileData
    {
    public string profileName;
    // Dodaj dodatkowe pola, które chcesz zapisać
    }

    public class ProfileManager : MonoBehaviour
    {
    public int profileNameVarID; // Global variable ID for the profile name in AC

    public void SaveProfile(int slot, string profileName)
    {
        // Save the profile name using AC's Global Variables
        GlobalVariables.SetStringValue(profileNameVarID, profileName);
    
        // Save the game using AC's SaveSystem
        SaveSystem.SaveGame(slot, false);
    
        ProfileData profileData = new ProfileData
        {
            profileName = profileName
            // Dodaj dodatkowe dane do zapisania
        };
    
        string json = JsonUtility.ToJson(profileData);
        File.WriteAllText(GetSavePath(slot), json);
    
    
    }
    
    public string LoadProfile(int slot)
    {
        string path = GetSavePath(slot);
        if (File.Exists(path))
        {
            string json = File.ReadAllText(path);
    
    
            // Sprawdź poprawność JSON
            if (IsValidJson(json))
            {
                ProfileData profileData = JsonUtility.FromJson<ProfileData>(json);
                // Load the profile name using AC's Global Variables
                GlobalVariables.SetStringValue(profileNameVarID, profileData.profileName);
    
    
                return profileData.profileName;
            }
    
        }
    
        return "Empty Slot";
    }
    
    private string GetSavePath(int slot)
    {
        return Path.Combine(UnityEngine.Application.persistentDataPath, $"profile_{slot}.json");
    }
    
    private bool IsValidJson(string json)
    {
        json = json.Trim();
        if ((json.StartsWith("{") && json.EndsWith("}")) || // For object
            (json.StartsWith("[") && json.EndsWith("]")))   // For array
        {
            try
            {
                JsonUtility.FromJson<ProfileData>(json);
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    

    }

  • using UnityEngine;
    using TMPro;
    using Michsky.MUIP; // Import Modern UI Pack

    public class SaveManager : MonoBehaviour
    {
    public ProfileManager profileManager;
    public TMP_InputField[] profileNameInputs; // Input fields for profile names
    [SerializeField] private ButtonManager[] saveButtons; // Buttons for saving

    private void Start()
    {
        InitializeSaveButtons();
    }
    
    private void InitializeSaveButtons()
    {
        for (int i = 0; i < saveButtons.Length; i++)
        {
            int slot = i; // Capture the slot number
            saveButtons[i].onClick.AddListener(() => SaveGame(slot));
        }
    }
    
    public void SaveGame(int slot)
    {
        if (slot < 0 || slot >= profileNameInputs.Length)
        {
            return;
        }
    
        string profileName = profileNameInputs[slot].text;
    
        if (profileManager != null)
        {
            profileManager.SaveProfile(slot, profileName);
        }
    
    }
    

    }

  • using UnityEngine;
    using TMPro;
    using AC;
    using Michsky.MUIP; // Import Modern UI Pack

    public class LoadManager : MonoBehaviour
    {
    public ProfileManager profileManager;
    public TMP_Text[] profileNameDisplays; // Text fields to display profile names
    [SerializeField] private ButtonManager[] loadButtons; // Buttons for loading

    private void Start()
    {
        InitializeProfileNames();
        InitializeLoadButtons();
    }
    
    public void InitializeProfileNames()
    {
        for (int i = 0; i < profileNameDisplays.Length; i++)
        {
            if (profileManager != null)
            {
                string profileName = profileManager.LoadProfile(i);
                profileNameDisplays[i].text = profileName;
                Debug.Log($"Profile {i} name initialized to {profileName}");
            }
    
        }
    }
    
    private void InitializeLoadButtons()
    {
        for (int i = 0; i < loadButtons.Length; i++)
        {
            int slot = i; // Capture the slot number
            loadButtons[i].onClick.AddListener(() => LoadGame(slot));
        }
    }
    
    public void LoadGame(int slot)
    {
        if (slot < 0 || slot >= profileNameDisplays.Length)
        {
    
            return;
        }
    
        if (profileManager != null)
        {
            string profileName = profileManager.LoadProfile(slot);
            profileNameDisplays[slot].text = profileName;
            SaveSystem.LoadGame(slot); // Load the game using AC's SaveSystem
    
        }
    
    }
    

    }

  • not sure what to change i dont want do breake ac code just need 3 profiles separete

  • nvm i found what was the case i had selected as string not as object (my profile manager)

  • 5 days of looking into code for nothing xD

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.