- Home /
Is this possible to work as a temporary storage?
I am about to make a game as a test for my friend. I was wondering if it is possible to create a file rather it be PHP, xml, or even just a text file and load it into the assets inside the unity project to store data. For example, something simple like "Cash: $100" Then write a script to where if the player picks up a gameobject "Cash +20" it will access that created file in the assets and edit the value, then when the game is closed or restarted the cash value in the file will still be $120. Is that possible to do as a temporary way to store data?
Answer by phxvyper · Jun 10, 2013 at 05:58 AM
This is actually really easy, and you don't need to load it into asssets as there is no point to do so. The basics behind it is just this:
File.WriteAllLines(@"ThePathToYourFileHere", "TheTextHere");
Pretty simple! There is some other stuff you might want to do... But you might want to learn that stuff on your own!
Just use that method, and what your saying will work fine :)
Edit: (According to the askee's requests, i have made these edits)
According to your example, which is: When cutting down a tree, the player will recieve 25xp. Save this to a file and whenever the game loads up the player should be able to load that data from that file.
In order to save to a file with organization, you'll want to use one of several methods: HashTable, Dictionary, WAD (Where's All that Data?), CCC (Comma Content Control) or GCF (Game Cache File/Game Content File).
CCC is quite common so i will be showing this, you'll have to do research on the other methods if you want to use them.
Create a Dictionary with a key of string and a value of string[].
Create an index within the Dictionary with a key of "PlayerXP" and assign it to a null value.
Create a string[] that contains all of the data for any XP given to the player and organize it respectively. (e.g. Tree Cutting XP, Fighting XP, whatever you want for the PlayerXP Label)
Every once in a while (possibly every 30 seconds) and/or when the player quits the game, assign the Dictionary Value to the string[] you created, then save the dictionary to a file using the format of "[key]:[value1],[value2],[value3],[val..." using some Regex methods and the File.WriteAllLines() method that i showed you.
Create a Start method that will load all data from the file into the Dictionary and String[] that you created earlier. Using the File.ReadAllLines() method, you will have to use some Regex for this.
If you don't understand this then i suggest that you go and research the topics of which you might not understand fully:
Regular Expression (Regex)
File IO
Array/List/Dictionary Separation
I don't understand how to use it exactly. From what it looks like, I understand its getting the provided path of the file, then saving the written text. But, lets say what I am trying to do is save experience. I cut a tree, now I want it to go to the txt file and add +25XP. to the "WoodCuttingXP:" line of text. Then when I load the game up next time, It should say under my woodcutting skill that i have 25xp. So how would I do this? Cause I have looked and I can't figure that out
@Josh$$anonymous$$Beyer this is pretty simple... I'll edit my answer appropriately to your needs in a moment, check back later and the answer will explain the entire process including how to make it efficient.
Your answer
Follow this Question
Related Questions
How can I read data from a text file, putting a large amount of data into structures 2 Answers
[Closed] Place to store user data 1 Answer
Data Storage Access 0 Answers
How does a TextAsset work? 1 Answer
Best way to manage stats in a text file 2 Answers