I'm making a visual novel and my game uses some custom classes that store important data.
Example:
public class Speech
{
public Sprite CharacterHead; // Holds a reference to a sprite which is the "head" of who's currently speaking
public LocalizedString SpeechOrigin; // Holds a reference to a localizedstring that is what that character is saying based on the language. (Uses Unity.Localization)
public string Name; // the name of the character that is speaking
}
This "Speech" class should never be changed during runtime and only needs to be read during dialogues.
My question is: what is a general good-practice way to store this type of data? I'm fairly new to unity and the only way i found to do this yet is through declaring a List on monobehaviour classes and editing them through the Inspector but this does not seems to be a effective way of handling this in a game that's going to have 1000+ different "Speech".
Ps:Don't mind all the variables being public, they are this way right now to make some parts of development easier, i will change the necessary ones when the time comes.
Why don't you just use ScriptableObject? It's the default way for Unity to store data in the game. You can also edit it from the Unity Editor instead of using another external editor. You can also drag-and-drop any resource from the game project's folder into it (eg. the texture of the head, the whole localized sting as data, the dialog audio clips, etc)
https://docs.unity3d.com/Manual/class-ScriptableObject.html
https://youtu.be/aPXvoWVabPY
There are a few good ways to store this information, especially if you are localising them too. Essentially, you would store an ID that you refer to the various strings with, which would be constant across language files.
CSV format files
Basically, you can use some spreadsheet software (like Google Sheets) and have all your speech there; a column for the ID of the string, and a column for the actual value. You can export the sheet as a CSV file and load into Unity.
You can then write some simple parsing code to read each line from the file, and split out the ID and the value and store it in a dictionary.
JSON format files
With JSON, if you architect it correctly, you can use Unity's built in JSON utility, or other better ones, to read the data directly into C# object classes.
A decent video explaining this can be found here.
YAML format files
YAML is basically the same as JSON, but with less brackets. It is also the format that Unity uses for its serialisation. However, you can only use this format by writing your own parser, or using a third party one, because there is no built in parser.
JSON files are generally industry standard for data storage these days, but eventually it is up to you which format you find easiest to work with. I've tried to list these in the order of how easy it is to add and remove strings from the file.
You should not store a lot of data in c# code use external files that are designed for data by creating a JSON file, XML file, or any other type of both human and computer readable format.
XML should be the perfect choise for your usecase, it is widely used for dialogue trees
Here is an example of what you can do with XML in your usecase
<npcs>
<npc name="Oren">
<dialogue>
<text>Hi #{PlayerName} do you want to eat falafel? </text>
<options>
<option action="yes">yes, I would like</option>
<option action="no">no...</option>
</options>
</dialogue>
</npc>
</npcs>
it's fairly simple to use XML, and it fits dialogues perfectly
Related
I want to develop a asp.net web application which should do the following task
a) user should be able to add content to the document. Content to be added can include text as well as image, screen shots etc.
b) user should be able to search based on some keywords. when searching with the keyword appropriate content along with images(if any) should be shown to user.
I am not sure what should be the proper approach for this. One way i think is to store text content in some xml file and later search for keywords by going though each node of xml and displaying. but i am not sure how to attach image content with xml. Also this method doesn't seem to be nice and efficient if with time document size increases a lot.
Anyone please suggest some proper way to do above requirement. Any hint would be appreciated.
Split it to two tasks. Editation and search.
Full text search is solved problem. Simply use Sphinx Search and you are done. Sphinx is simple to use and can do everything you will need. It has MySQL interface (your app connects to sphinx the same way as to second MySQL database).
Editation is a bit more complicated. If I understand correctly, you want multiple users to edit single document concurrently.
I recommend using websockets to notify other clients about changes in document. Long-polling and Server Sent Events have ugly side effects, like stopping browser from making another requests to server. To implement client side in Javascript, I would use React, Angular or similar framework to make updates as easy as possible.
Server side requires modification-friendly representation of a document, so if one user changes one part, and another user another part, your app should be able to merge changes. Changing completely different parts is easy, but it may be tricky to change the same paragraph or document node. Exact representation of each change depends on format of your document.
I do not see much benefits of using XML rather than any other format. It may be practical for document representation, but it will not help with merging of colliding modifications. I would start with plain array of strings, each representing a single paragraph. Extending it to full XML document is the easy part, once two users can edit the same paragraph.
To store images in XML, simply store files using their hash as a file name and then use such name to link the file in XML. Git does the same thing and it works nicely. You may want to count references to identify unused files.
I have been programming a map editor to use when making 2D games. I have nearly completed it, however I can't load maps.
I have multiple options for saving and loading maps. I have:
DAT File
BIN File
MAP File
XML File
XML isn't the issue but the other file types are. I need to be able to read and write image data to these other 3 file types. I would suggest just removing these file types, but sometimes having maps that aren't XML are useful.
UPDATE:
I'm not asking to follow an algorithm that is already produced. What I mean is to write the image data to a file, and then read the data back into a bitmap object.
It sounds like what you're talking about is having a proprietary format for your game map data.
What you want to do is take a look at Object Relational Mapping (you could start here: http://en.wikipedia.org/wiki/Object-relational_mapping). You want to come up with a good way to structure your object (map image) in a store (a binary file). You'll want to write up some kind of class that follows this kind of interface:
public interface ORMap
{
public void Save(MyMap map, string fileIdentifier);
}
It sounds like you've already done this for XML.
You could the implement that interface to transform and/or serialize your MyMap object to the desired format and write it to disk using fileIdentifier as the name.
There is no one standard way of doing this, but if you have examples from other similar games you could try to look at their API and/or file structure and imitate it.
I'm looking to make my own file format .
that format should contains pictures/pdf/and other files ...
also need to know how I can packer-unpacker for this format to unpack files from it/pack in it & reading the pictures from my own format to picture boxes on my WinForm for example.
I've searched but didn't really found what I'am looking for
I hope someone can help me , thank you
Zip is an excellent choice. Because you can encrypt the file and of course reduce the file size in some cases (text and uncompressed things). But if you want to create your own file format you can easily decide rules for your storage and order inside the file. Then serialize the info into the file. For example by object serialization or by writing the binary date to file object by object .
if you really want to write your own file format then I would suggest one of two things. One, you could do it entirely in binary at which point you would want to do a 'chunk' format. Chunk format is to basically have a header to each subsection. The header contains the size of both the header as well as the size of the payload. Create a serialization class for your header then add the bytes to the filestream from your payload. Actually pretty easy to do.
Second (and easier) way to do this would be to create an XML format. Create a master class for your format then add all of the data as collections of sub classes under that. Once you have that, use any of .net xml serialization classes to serialize it out to disk.
You can also use SQLite for your purposes. It provides dbms power without needing server. That is popular solution for your problem.
System.Data.SQLite is an ADO.NET adapter for SQLite.
Not quite sure how to word this. Looking for info on VB.net or C#, either one will do.
I'm trying to make a generic file parser that will use an XML file to define the file layout (field names, data types, delimiter type, file type, etc).
The idea is to use this in programs that have differing input file layouts and types (delimited/flat file) without having to modify and recompile. Would like it this way as occasionally the local offices change the format (add/remove fields) and it breaks production until the code is recompiled, re-tested etc.
Unfortunately, as I said, I have no idea how to even look this up.
Any help pointing me in the right direction would be appreciated.
I'd consider to change input file to be xml (or to have corresponding xml).
In this case - you can create your xsd schema, and validate it using xsd.
You can even send this xsd to your offices to validate there inputs before they send it to you.
One other way it to use SSIS
You can use for that an open source solution called FinTp.
I have two TextRanges from two different RichTextBoxes, and four strings from regular textboxes. I would like to save all this information in one file, and then be able to load it later. Whats the best approach?
I've been reading some about it, and it seems that reading all into one memorystream and then save it to a file is one way to do it. And then parse this content later.
Anyone that want to share some experience, and simple code?
For a simple approach consider creating a class with string properties for each of your textbox texts. You could then set the properties when you want to save your text, use XML serialization to save the class to an XML formatted file, and then read it back at a later time.
The advantage of this approach is that you will not need to hande low-level file handling or parsing yourself.
Searching for C# and XML Serialization will yield plenty of code examples.
One solution is already provided by you: save in file and read after.
Another could be, in case if the data is too big, is using http://msdn.microsoft.com/en-us/library/dd997372.aspx Memory Mapped Fies.