Home > Blockchain >  Why if the string is empty it's still saving the game file?
Why if the string is empty it's still saving the game file?

Time:08-24

using UnityEngine;
using System.Collections;
using System.IO;
using TMPro;

public class SavingGame : MonoBehaviour
{
    public int resWidth = 1920;
    public int resHeight = 1080;
    public SaveLoad saveLoad;
    public Description description;
    public TextMeshProUGUI savedGameDescriptionText;

    private static int countName;

    private void Start()
    {
        countName = 0;

        string[] dirs = Directory.GetDirectories(Application.persistentDataPath   "\\"   "Saved Screenshots",
            "*.*", SearchOption.TopDirectoryOnly);

        if(dirs.Length > 0)
        {
            countName = dirs.Length;
        }
    }

    public static string ScreenShotName(int width, int height)
    {
        return string.Format("{0}/Saved Screenshots/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
            Application.persistentDataPath,
            countName,
            width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    }

    void Update()
    {
        if (Input.GetKeyDown("k"))
        {
            description.StartFading(true);
        }
    }

    public void Save()
    {
        var time = description.StartFading(false);

        StartCoroutine(StartSaving(time));
    }

    IEnumerator StartSaving(float time)
    {
        yield return new WaitForSeconds(time);

        string filename = ScreenShotName(resWidth, resHeight);
        string directory = Path.GetDirectoryName(filename);
        Directory.CreateDirectory(directory);
        ScreenCapture.CaptureScreenshot(filename);
        string descriptionContent = savedGameDescriptionText.text;
        if (descriptionContent != "")
        {
            string descriptionFileFolder = directory   "\\"   "Description.txt";
            File.WriteAllText(descriptionFileFolder, descriptionContent);
        }
        StartCoroutine(saveLoad.SaveWithTime(directory, Path.GetFileNameWithoutExtension(filename)   ".savegame.txt"));

        countName  ;
    }
}

I'm checking if descriptionContent is not empty but even if it does it's entering inside like it's not empty :

descriptionContent is empty "" but it's still entering

In the editor the variable savedGameDescriptionText is a TextMeshPro - Text (UI) and it's empty. what is not empty is the Placeholder :

The Enter description text is in the Placeholder not the savedGameDescriptionText :

TextMeshPro - Text (UI) is empty

The Placeholder contain the Enter description text :

Placeholder

When i click on the Save ui button in the editor it's calling the Save method.

I want that only if the Text(savedGameDescriptionText) is not empty then create the description text file. it seems like checking for "" is not working as i expected.

CodePudding user response:

I suspect the string comparison is dangerous. Try savedGameDescriptionText.text.Equals("") or just see if it's length is 0 like savedGameDescriptionText.text.Length == 0

CodePudding user response:

The problem is that i tried to get direct access to the child text but what i had to do is to get access to the inputfield : TMP_InputField and then to the TMP_InputField text.

This is working :

using UnityEngine;
using System.Collections;
using System.IO;
using TMPro;

public class SavingGame : MonoBehaviour
{
    public int resWidth = 1920;
    public int resHeight = 1080;
    public SaveLoad saveLoad;
    public Description description;
    public TMP_InputField _inputField;

    private static int countName;

    private void Start()
    {
        countName = 0;
        
        string[] dirs = Directory.GetDirectories(Application.persistentDataPath   "\\"   "Saved Screenshots",
            "*.*", SearchOption.TopDirectoryOnly);

        if(dirs.Length > 0)
        {
            countName = dirs.Length;
        }
    }

    public static string ScreenShotName(int width, int height)
    {
        return string.Format("{0}/Saved Screenshots/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
            Application.persistentDataPath,
            countName,
            width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    }

    void Update()
    {
        if (Input.GetKeyDown("k"))
        {
            description.StartFading(true);
        }
    }

    public void Save()
    {
        var time = description.StartFading(false);

        StartCoroutine(StartSaving(time));
    }

    IEnumerator StartSaving(float time)
    {
        yield return new WaitForSeconds(time);

        string filename = ScreenShotName(resWidth, resHeight);
        string directory = Path.GetDirectoryName(filename);
        Directory.CreateDirectory(directory);
        ScreenCapture.CaptureScreenshot(filename);
        string descriptionContent = _inputField.text;
        if (_inputField.text.Length != 0)
        {
            string descriptionFileFolder = directory   "\\"   "Description.txt";
            File.WriteAllText(descriptionFileFolder, descriptionContent);
        }
        StartCoroutine(saveLoad.SaveWithTime(directory, Path.GetFileNameWithoutExtension(filename)   ".savegame.txt"));

        countName  ;
    }
}
  • Related