|
Post by amasinton on Dec 6, 2021 14:27:20 GMT
I am trying to "reset" a working database back to its original, unaltered state by copying from an unaltered working database.
My game has three save slots. Each one has its own copy of the original database. When the player restarts one of these save slots I would like the database copy associated with that save slot to also reset to its original state without alterations the player will have made in their previous playthrough. (This would, of course, wipe out any changes the player made.)
Does that make sense?
I just don't know how to do this. I would love to just overwrite the save slot database with the untouched original, but I just don't quite know how to do that at runtime. Is there some way to do "overwrite if exists" at runtime for a particular instance of a working database?
Thank you for your help!
|
|
|
Post by echo17 on Dec 6, 2021 14:46:49 GMT
The easiest way would be to first close the database connection with: dbManager.Close(); dbManager.Dispose();
Then you can copy the untouched database to whatever filename you want using .NET File methods, like File.Copy. Something like: File.Copy(unalteredDatabasePath, workingDatabasePath, true);
Once you have the untouched db copied, just point to the copy at runtime. Please see this FAQ on how to do this: FAQ
|
|
|
Post by amasinton on Dec 6, 2021 16:24:39 GMT
This looks like something I can handle. I'll give it a try and report back.
Thank you!
|
|
|
Post by amasinton on Dec 7, 2021 16:17:01 GMT
Okay, so, I'm nearly there, but I've run into something I don't quite understand and I hope you might have some insight.
When I File.Copy() the untouched database, I also use the command's built-in ability to also change the name of the new file and overwrite any existing file with the exact same name. Which is pretty handy, because I use it to change the name of the fresh copy of the database to the name of the database associated with the save slot I have just reset.
When I assign this new copy of the original database to my database manager, ALL of the other database managers in my scene also get their Database File fields set to this new copy which sits in the working folder (in my case, Application.PeristentDataPath), rather than the original database which sits in the project folder. This happens even though I only explicitly set the Database File in ONE manager in the scene. This change is only reflected in the editor once I exit play mode. And it persists in the editor for the rest of the session.
But when I quite Unity and then reopen it and reopen the project, the Database File field in all of my database managers is set back to the original file in the project folder.
So ... What's going on here and is this something I should be worried about?
Some more context:
My scene has four separate GOs each with a SimpleSQLManager script attached. Each manager is assigned the original database from the project folder as its Database File. All of the managers have the ChangeWorkingName bool set to "true" and then each manager has the working name text field populated with a different string. So, the first manager has its working name field set to "dbOriginal" and is where I keep a copy of the original, untouched database in the working folder. The remaining three have their working names set to "dbSlot1", "dbSlot2", and "dbSlot3", respectively.
When I create a new copy of the "dbOriginal" database using File.Copy() I set its name to be equal to the working name of the one manager I am trying to replace the database file.
Thank you for your help again!
|
|
|
Post by echo17 on Dec 8, 2021 14:31:17 GMT
I think I'll have to see an example of this working to give you advice. Please see this FAQ: FAQ
|
|