Is it possible to save variables set/changed in play mode? - c#

I was trying to make a Stardew Valley-esque day manager where the in-game day, season, and or year is changed by interacting with a bed, but once play mode is exited, the variables are reverted back. I tried to set the variables within a StartCoroutine, and while in play mode, it worked and the variables were changed, but when I exited play mode, the variables reverted back.

To persist data you probably need to save your variables somewhere else like a database. For this you can just use a txt file. Maybe just look up how to write and read data to a file.
Do something like this:
When you start the game read the data
Right before closing the game write (update) the data

Related

Backing up and reloading console command history

When using a C# console app, all the commands I input get buffered during the life of the session (ie. until I close the Console).
I would now like to be able to back up this buffer and retrieve it next time I relaunch my console app.
I have read a few threads on SOF and got some pointer to "SetConsoleHistoryInfo", but I am not sure that is the way to go, nor how to use it for that matter.
Has anyone ever done something similar?
You could read each command you input and save it into a .txt file. If you are writing directly to the console you can save the string from the Console.ReadLine() function into a variable and then write it to the historical file.
The next time you run your program, have a method that checks the .txt file and retrieves the commands in whatever format you desire (array, line by line etc.)
This gives you flexibility as to how you manage the .txt file as well, you can keep the historical commands from every time your program has run or just the previous execution.

Recovering inspector values from Unity build

I made the stupid mistake of moving a C# script out of the project and back in. This, of course, cleared all of the important inspector values back to their default values from the script.
All I need is to see those values again and re-enter them. I attempted decompiling the assembly-csharp.dll from my last build using dotPeak. While this did recover the correct classes and their fields, all of the fields are not defined. Where in a Unity build are these values stored, and is it possible to decompile them there?
Thanks in advance!
As far as I know there are stored in the scene file (.unity) or, if it's a prefab, in the prefab file (.prefab) (and if it's a prefab in the scene, it's stored in the prefab file with a list of modifications in the scene file). You might have success finding some values in there, but those are serialized and you can only really read them with Asset Serialization Mode "Force Text". It might also be that they lose their value when you open Unity between moving the script out and moving it back in.
Edit:
I missed the part that you wanted to read them from the build. I don't think that's possible, as they are normally serialized (if they are not in Streaming Assets). As scene files (which I think would contain the data) are also just files and not scripts that get compiled, I think the values are in one of the serialized files.
Also: You don't happen to have any kind of version control? Because then you could rollback to an old commit that contains that data, or if that's not possible look at the specific file in question.

Firebase in Unity - Disable persistence

I'm making a mobile game for Android using Firebase. This game has a system that allows the player to play offline and update/retrieve data from the database depending on the saved date-time.
This date-time (written in a local file) should be the same as the one saved in the database, and to do so I wrote this:
reference.Child("users/"+this.id+"/updateDate").SetValueAsync(updateDate).ContinueWith(task =>
{
if (task.IsCompleted) {
Debug.Log("Setting new update date into file: "+updateDate);
this.lastUpdateDate = updateDate;
WriteFile();
}
});
The problem is that, even if there is no internet connection, the value gets modified but the condition below is never satisfied so what happens is this:
If you read the value, even without internet connection, you read the "new" date-time, while the file still contains the "old" one (the one actually in the database).
I know the best solution is to delete everything and let offline persistence do its work, but I did a couple tests and it doesn't even work on Android's build. So my question is the following:
Is there a way to disable offline persistence in Unity?
Because I've tried "FirebaseDatabase.DefaultInstance.setPersistenceEnabled(false)" but it seems like that function is not even present in the Unity's API (it gives me compile errors).
Thank you for your assistance!
To disable persistence, do:
Firebase.FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false);
If you're coming from Google with the "Failed to initialize persistence storage" crash when testing Unity with 2 editors, do
#if UNITY_EDITOR
Firebase.FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(false);
#endif

How to make a "Settings" file

I'm working on a C# WinForms app and I would like to know how can I make a "save state" button. I've heard about serialization, but I want the form to be restarted every run, but allow the user to reload the saved state.
For example, in games you can save your progress and then when you run the game you can either start a new game, or load the save (in either way, when you run the game you always get to the main menu, whether you want to start a new game or load the saved game), that's what I'm looking for.
I hope I've made myself clear.
you have to use the Isolated Storage in .NET to store application data clickhere
Many ways to do it however depending on the size and security specification of the information which needs to be loaded you could take different approaches.
One approach is surly what Dhaval Patel mentioned earlier [Isolated Storage in .NET]. If this information of yours is part of a wizard or something similar I would personally store them in a database table. This way your data will be quite accessible[Query-able] and you could enforce your security constraint much easier and you will have lots of room for maneuver in terms of future improvements.
Another advice[Only my personal opinion] : Don't pollute your config file with those settings in case you had that in your mind.

How to save and load the contents of multiple text boxes in C#?

I'm making my first program in C# which is just going to be a simple note making program. I currently have ten tabs, each with a text box for a title and a description. However the program isn't too useful without having a way to save the content in each text box automatically either before shutdown or after editing the content, and then open them up again the next time the application is loaded, so that a potential user could just carry on with their previous notes etc. the next time they want to use the program. It isn't too good having notes deleted every time you close the app. Also, another option might be saving the state of the whole application as a cache or something, which would be easier than saving twenty text files (two per tab) if its even possible. I havent a clue how to do this anyway, and there is nothing too specific on here or any tutorials that I can find on google.
Thanks for the help anyway, I'd just like ideas and examples if you can!
Bye!
When researching the subject use the keyword persistence:
In computer science, persistence refers to the characteristic of state
that outlives the process that created it.
So persistence has many forms: text files, XML, databases, shoot even memory outside the application fits this definition (its a little silly to store things in memory because a restart would destroy everything).
Assuming this is a winforms application then we are talking about driving the application with events. You can have a submit button which somebody clicks to perform a Save to the "persistence" thing you chose (usually a database).
private void Save(object sender, EventArgs args)
{
//Gather text info e.g.: var myText1 = myTextBox1.Text;
//Save to the database or wherever using Stream
//or SqlConnection or something else. See below
}
You could choose to save to the database with a SqlConnection or to a file with StreamWriter.

Categories

Resources