0

Let me explain:

Let say I have a Boolean variable and at the time I am writing the code of the program I set it to False. Now each time I build/run the application this Boolean variable reasets to False.

I want that in case the user inputs a specific string that Boolean will be changed to True and then each time I rerun the application it will remain with the value of True, In other words, the variable will now be reset to True.

NOT_A_ROBOT
  • 113
  • 1
  • 15
Twins96
  • 15
  • 5
  • 2
    That would be a file, database, or registry entry. – ProgrammingLlama Nov 04 '20 at 06:42
  • 3
    you want to save data persistently? [this answer](https://stackoverflow.com/a/15279002/5174469) could be a first step – Mong Zhu Nov 04 '20 at 06:46
  • You can encrypt the persisted data if you want, it should still be easy to break though. I guess you are trying to implement a serial key/copy protection scheme? Basically, if companies like Microsoft or Adobe can't prevent piracy of their products, I don't think you need to put too much effort into it. – vgru Nov 04 '20 at 07:51
  • 2
    Sounds like a job for [Using Application Settings and User Settings](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/using-application-settings-and-user-settings?view=netframeworkdesktop-4.8) – Fildor Nov 04 '20 at 07:59

1 Answers1

0

You can save the boolean value.

Here's how you do it:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class SaveSystem
{

// =====Saving=====

// "path" should be filled with the full path of the save directory, including the file name and the file extension.
    public static void Save(bool boolean, string path)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Create);

        formatter.Serialize(stream, boolean);
        Debug.Log($"Bool saved at {path}");
        stream.Close();
    }


// =====Loading=====

// "path" should be filled with the full path of the save directory, including the file name and the file extension.
    public static bool LoadOptions(string path)
    {
        if(!File.Exists(path))
        {
            Console.WriteLine($"Options file not found in {path}"); //For debugging, is removable
            return false;
        }
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Open);
        bool stuff = formatter.Deserialize(stream) as bool;
        Debug.Log($"Bool loaded at {path}");
        stream.Close();
        return stuff;
    }
}

Just make sure to load it on start. This saving method also works with any other thing, such as ints, and your own classes (<!> provided it has "[System.Serializable]" on top <!>, and you modify the type of data it saves/loads.)

[EDIT] This is one of many saving algorithms. This is a method of saving to a binary file. If you want to save to a text file instead, other answers may help. Keep in mind that binary files are harder to tamper with than text/xml files, so this is the recommended way to save things.

NOT_A_ROBOT
  • 113
  • 1
  • 15
  • Is there a way to save is so a outside user cannot read it? I don't want to give the user the ability to read/write/change the file content and by that the Boolean itseld – Twins96 Nov 04 '20 at 07:15
  • Technically, there isn't. But, if you want to make it hard to do it, this is the way. Saving to a text file is usually bad because users can easily just modify some values using Notepad. This is not the case if you save to a binary file. – NOT_A_ROBOT Nov 04 '20 at 07:26
  • I am tring to use a `BinaryWriter` like you used but after the file was created I opened is using notepad and unfortunately I could read it in plain text. As you know I want that it will not be easy to read. This is the code I have used: `using (BinaryWriter writer = new BinaryWriter(File.Open("Binary.bin", FileMode.Create))) { writer.Write("string text\n"); }` – Twins96 Nov 04 '20 at 07:52
  • @Twins96: you can easily just create a copy of the file before running the app and replace it afterwards. Or perhaps just delete it. Or run everything inside a virtual machine and just revert to a snapshot after every run. There is no way to prevent a user from cracking the scheme where you store a value locally. You could improve your odds by using an authentication server each time, but 1) your customers could get angry if they need internet for the app to work, and 2) a determined hacker will just crack the executable anyway. – vgru Nov 04 '20 at 07:52
  • I used a BinaryFormatter, not a BinaryWriter – NOT_A_ROBOT Nov 04 '20 at 07:55
  • OR simply use the tool that's made for the task: Settings. – Fildor Nov 04 '20 at 08:01
  • @NOT_A_ROBOT Can you explain the different? – Twins96 Nov 04 '20 at 08:01
  • No, but this link can: https://stackoverflow.com/questions/40749926/difference-between-binarywriter-and-binaryformatter-serialize – NOT_A_ROBOT Nov 04 '20 at 08:02
  • @Fildor Can you explain? – Twins96 Nov 04 '20 at 08:02
  • @Twins96 See my comment on the question – Fildor Nov 04 '20 at 09:28
  • @NOT_A_ROBOT How to I know what types of veriables has System.Serializable? – Twins96 Nov 04 '20 at 10:50
  • If you make your own class, check if it has [System.Serializable] on top (including the brackets). If it's a variable that you did not make (e.g. bool), right click on it, see the code behind it, and check for the System.Serializable tag. – NOT_A_ROBOT Nov 04 '20 at 13:18