- Home /
Storing my choose your own adventure questions/actions
Hey!
I'm in the middle of developing a simple "choose your own adventure" style of game in Unity/C#.
I'm thinking about how I'm going to store my questions:
"You walk into a dark room and see a man in the corner of the room. What do you do?"
and actions "Sneak up on him" or "Try to get by him"
Seeing as I'm going to have a lot of questions and actions, I was thinking of maybe storing them all in some form of database and getting Unity to pull information from it. What would you suggest? sql? csv? or something else?
Answer by toddisarockstar · Oct 02, 2018 at 02:57 AM
i think sql or a external database might be extreme for a couple hundred or even a couple thousand questions. i dont think your game would have more than that.
a simple txt document in your asset folder should work fine. remember ... text really doesnt take up much storage. even a thousand average sentences wont even add up to a megabyte.
if you want to change things as you go you could download your text from a server.
here is an example of grabbing text from a text file and pushing it into quick organized arrays for your game. you would just use separator characters in your text document.
parcing out the data might slow you down for however many microseconds when the game starts but im sure its still faster than making a connection to a server.
public TextAsset load;//<-- drag and drop your textfile here
public string data;
[System.Serializable]//<--remove this line in your real build for speed!!!!
public class Conversation{
public string Question;
public string[] PossibleAnswers;
public Conversation(string s){
string[] a = s.Split("="[0]);
Question = a[0];
PossibleAnswers = a[1].Split(","[0]);}
}
public Conversation[] database;
void Start () {
if (load == null) {
// this is example of what your text file could look like
// notice the seperator characters so we can read it.
data = "monkeys eat?=bannannas,potatoes,green beans*game developers are?=smart,nerdy*toads are?=green,pink,blue";
} else {data = load.text;}
string[] a = data.Split ("*" [0]);
int i = a.Length;
database = new Conversation[i];
while(i>0){i--;database[i]=new Conversation(a[i]);}
//your questions are now loaded with cooresponing answers
// here is a print out of what the script found in the text so you can see
while (i<database.Length) {
print("question #"+i+" is:"+database[i].Question);
int i2 = 0;
while(i2<database[i].PossibleAnswers.Length){
print ("possible answer: "+database[i].PossibleAnswers[i2]);
i2++;}i++;}}
You also may want to use CSV files, they're easy to deal with, for localisation purposes for example. Also, if the same Answer can be given to different questions, and as you will probably end up with some sort of conversation tree, you may want to put everything in Dictionaries or HashTable.
Ahh right, I thought the sql database might be a little overkill for what I'm doing! I'll look into storing it in a text file and play around with the code you have provided. Thanks a bunch!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Store data for tile based system 1 Answer
Any solution for NoSQL, local database, C#, compatible with Unity? 3 Answers
Distribute terrain in zones 3 Answers
A real head-scratcher 0 Answers