- Home /
XML File, or Array?
Hi, quick question:
I'm making (or attempting to) make a dialogue system using Xml to store the information. I've got my head around how to do it, but I was wondering, do I need to store the Xml info in an array, or can I just access the file each time it's needed without any performance/loading issues.
The files themselves aren't that big, and are brought into the script as a TextAsset.
Answer by ArkaneX · Sep 20, 2013 at 08:14 AM
Accessing a file is always much slower than accessing data in memory. Especially when you have to search for data.
If the files aren't big and loading them won't introduce memory problems (which might be important on mobiles), then I suggest preloading the data at the beginning of game. Or alternatively, if different files are loaded at different stages of your game, then you can load required files at the beginning of the stage.
I don't know the structure of your files, but if you can simply load their raw data (without xml tags) into arrays - go for it. But if the structure is more complicated (nodes have attributes, child nodes, etc.), then you need to prepare a proper classes in your game and then use them to deserialize your files. If you don't know what deserialization is, please read this Unity Wiki page and maybe this MSDN page.
Thanks for the reply, ArkaneX.
I am (as you've probably guessed) quite new to this. I was under the impression that loading the Xml file into a TextAsset and accessing that was effectively loading it into memory (I realise now the title of the question is misleading), is this not so?
As for the structure, it will be fairly complex. The files don't just contain the dialogue, but also flags for when the dialogue can be applied, and information on what the player can respond with and where to go next, etc.
I'm using Xml because the other person working on the game can get their head around Xml easier, so they can edit the files.
You're right - if you load it once and then only access it as 'in memory representation', then it will definitely be faster than accessing actual file on disk.
Anyway - after you have TextAsset instance, you still has the whole X$$anonymous$$L string in memory, and searching it might not be a best option. It is possible to load it into XmlDocument class, and then search using xpath, but preparing dedicated classes and using deserialization is still better approach.
Thanks. I do currently have it as a TextAsset that is then loaded into an XmlDocument class, but I will bone up on dedicated classes as an alternative.
Thanks again.
Your answer
Follow this Question
Related Questions
Save and load arrays into an XML file 1 Answer
Problems with Arrays 2 Answers
IndexOutofRangeException 1 Answer
my QandA array is not working when you choose 3 wrong answers 1 Answer
Array empties values on RunTime? 1 Answer