Advertisement

Anyone know a cool program for editing datafiles?

Started by April 18, 2020 05:47 AM
4 comments, last by IntrepidUniverse 4 years, 7 months ago

I am learning programming and have built a small game that uses data files. Basically I wanted to edit text files and have them read instead of hardcoding everything into the code. So like I could have a text file called "room1, room2, room3" etc.

I made my own text file for this... basically it uses a "key" string. The code can either grab the single line (omitting the key) or grab all the lines between to keys...

something like this. (This is just sudo code)

//Line String -:blah blah blah blah blah blah blah blah blah blah blah blah

//Block String
halb halb halb
halb halb halb
halb halb halb
//Block String

So the code can fetch

string == "blah blah blah blah blah blah blah blah blah blah blah blah"
List<string> = {"halb halb halb", "halb halb halb", "halb halb halb"}

Anyway, everything is working but as i get more into the project editing the data files is becoming laborious. So I thought maybe there is a kind of standard data files I could copy a template from, and even better if I use a standard template file I would be able yo find some kind of free/opensource editor that makes editing and entering data into the file super easy?

TL;DR 1 :

Anyone got any links to standard data files templates that are basically just text files that I can use in my project?

: TL;DR 2 :

Does anyone know a text editor that is designed for creating or editing these kind of data files. Like something with a "add key" and "edit key's data" type thing instead of editing a massive text file in NotePad++?

Thanks!

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

There are a number of standard text storage standards, where libraries likely exist for reading or writing such files.

The INI format https://en.wikipedia.org/wiki/INI_file is pretty simple, and even easy to implement reading or writing. Works best if you have a number of groups, where a group is key/value pairs.

For more general data, JSON https://en.wikipedia.org/wiki/JSON or https://www.json.org/json-en.html JSON gives you a lists, and maps, of strings, and numbers. A little harder to implement, but more flexible in what you can store.

YAML is a super-set of JSON, it is more convenient for manual editing, but much harder to implement yourself, so find a library if you want this.

The inevitable option XML https://en.wikipedia.org/wiki/XML is by far the most flexible, but it's a big beast. Not really designed for manual editing, although it is done. Biggest advantage is that just about everything can read or write xml.

If the standard formats don't fit, it is not too complicated to write your own file reader. All programming languages can open and read data from a file, and most have special handling for reading text files, for eg reading a line of text (as a string). From there you can use string operations to inspect the contents of the line, and split and convert the data to whatever you want to have.

As an example, you could decide that one value is written at one line of text. If you have multiple values,, you thus need multiple lines. You could use an empty line or a line with a special marker like “---” for ‘end of the value’. This is easy to program.

If you have a complicated file format in your text files, you end up in the world of parsers, which can manage reading text files like your program source code. I don't think you'll get anywehere near that complexity though ?

As for editing, the mostly standard approach is a text-editor like notepad++. Some have special support for the standard file formats like JSON. You should treat the contents of such files like you treat your program code. Make it as readable as you can, use multiple files if that is simpler, etc.

Advertisement

Alberth said:

For more general data, JSON https://en.wikipedia.org/wiki/JSON or https://www.json.org/json-en.html JSON gives you a lists, and maps, of strings, and numbers. A little harder to implement, but more flexible in what you can store.

I still do not really understand how to format jason files to write the data files… but I did some googling and found a neat library called Jason.Net that works in a c# project. I think I will use this.

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

The file format of a JSON file is shown at its home page: https://www.json.org/json-en.html , the ‘weird’ line diagrams with black lines and white boxes. It's a railroad-diagram ( https://en.wikipedia.org/wiki/Syntax_diagram ), since you read the diagram like a train follows its railroad (no sharp turns).

The JSON format is very simple.

A number is written like a number, eg “123”, or “1.31e-56” . The “number” diagram at the JSON page should allow my examples. A piece of text is written as a string “hello world”. You also have the boolean values “true” and “false”, and “null” for ‘no value’.

You can have a sequence of elements (it's named ‘a list’) by writing “[” and “]” around it and seperating the elements by “,”, eg “[1,2,3,4]” are 4 numbers in a list. Lists in lists etc is also allowed, ie “[1, [4,56], [ ] ]” where the “[ ]” is an empty list, a list without any element.

You can have a dictionary (also known as map) of named values. JSON names it “object”. It looks like

{ "length" : 3,
  "description" : "short",
  "value": [1, 2, 3]
}

It's much like a list, except its elements are of the form ‘ “text” : value ’ where ‘value’ can be any value allowed in JSON, including another dictionary.

So for your example in the first post, you could do something like

# I use lines starting with a '#' as comment. JSON doesn't allow comment, so you
# have to delete those lines if you want to use the example in your code.

# GENERAL IDEA:
# JSON insists you start with an 'object', so the file must have a disctionary
# as its outermost value.
# I used an object with an element named "strings" that has the list of strings
# that you want.
{ "strings" : [
      ....
  ]
}

# FIRST ATTEMPT
# The simplest is to use a list of lists of strings, where a list with 1 string
# is a line string, and a list with multiple values is a block string:
{ "strings" : [
      [ "line string" ],
      [ "another line string" ],
      [ "block string",
        "more text",
        "and another one" ],
  ]
}

# SECOND ATTEMPT
# A problem with the above is that your data values only have an index number, ie
# 'object["strings"][1]' is '["another line string"]'.
# You may want to attach a name to your data instead.
{ "strings" : [
      { "name": "first-string",
        "value": [ "line string" ] },

      { "name": "another-string",
        "value": [ "another line string" ] },

      { "name": "first-block",
        "value": [ "block string",
                   "more text",
                   "and another one" ] }
  ]
}

# 'object["strings"][1]' is now another dictionary, where you can access a
# 'name' and a 'value' field to know what data value you have. In this way
# It is simpler to get the right string value at the right spot in your program.

As you can see, I deliberately formatted the JSON data to make it more readable. The JSON file format allows any amount of white space, including none at all, so

{"strings":[{"name":"first-string","value":["line string"]},{"name":
"another-string","value":["another line string"]},{"name":"first-block","value":[
"block string","more text","and another one"]}]}

is also allowed. It's however much less easy to understand what it specifies. This is what I meant with ‘treat the contents of such files like you treat your program code’. Make your data file as readable as you can, it avoids errors and saves time in understanding what you wrote.

Hi A4L - We don't have a template file format but if you are happy to invent your own format we have a json editor that shows a tree view where you can "add key" using a right click context menu and "edit key's data" in a pane to the right of the document tree.

It allows you to put new lines in the text easily - in JSON this is hard because you have to escape the newline in the text i.e put '\n' in your text string. Our editor does that for you so large text strings are easier to edit.

The editor is written in python making it simple to embed in your projects and it is very small in size. It could be a simple in game editor - at least for development purposes. It can be run over ssh so you could run it on a game server that has no video output and it would use your local desktop screen if you have a X windows server running there.

If it sounds like it could help take a look at:

https://www.intrepiduniverse.com/projects/jsonEditor.html

Good luck with your projects!

This topic is closed to new replies.

Advertisement