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.
Related
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
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.
I want to have pointer to xml file and when ever i need to read from him some info i will go directly to the place in this xml file and bring the fresh info.
How can i hold this xml ?
I need to give the ability to change the info in this file also.
You can use the FileSystemWatcher class to detect whenever the XML file gets modified (and react by reloading it). The example on MSDN is quite instructive.
You can't really have a "pointer to xml file" as such. I suggest using XmlReader and XmlWriter classes for reading / writing the XML if the file is expected to be rather big, or LINQ to XML and the XElement class if its size is more likely to be moderate.
If you come across any particular implementation problems, specify them.
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.
I am writing a client windows app which will allow files and respective metadata to be uploaded to a server. For example gear.stl (original file) and gear.stl.xml (metadata). I am trying to figure out the correct protcol to use to transfer the files.
I was thinking about using ftp since it is widely used and a proven method to transfer files, except that I would have to transfer 2 files for every actual file (.stl and .stl.xml). However, another thought had also crossed my mind ... What if I create an object and wrap the file, metadata and the directory I needed to tranfer it to, serialize the object and then submit a request to a webservice, to transfer the file.
Original file size would range from 100k to 10MB. Metadata size would probably be less than 200k
The webservice call seems like an easier process to me to deserialize the object and distribute the file and respective metadata accordingly. However I'm not sure if this is a sound idea or if there is a better way to transfer this data other than the two methods I have mentioned.
If someone can point me in the right direction it would be much appreciated.
You could wrap it in a zip file like the "new" office document format does. You might even be able to use their classes to package it all up.
Edit:
Take a look at the System.IO.Packaging.Package class. It seems to be what you need. This class resides in the WindowsBase.dll assembly and became available in .NET 3.0.
PS: Remember that even though it is a zip file, it doesn't need to be compressed. If you have very large files, it may be better to keep them uncompressed. It all depends on how they're going to be used and if the transport size is an issue.