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?
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:
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
Thank you, that was very helpful and informative.