Serialisation – JSON in C#, using Json.NET

Last week we compared a number of different data-interchange formats and I explained why my current favourite is JSON.

JSON’s syntax is very simple. So easy in fact, that it can be expressed on a single page.

This makes it easy to remember and write. Further, it makes writing a JSON parser very easy as well.

However, there already are many solutions for all major, and many smaller languages and environments.

So instead of reinventing the wheel, today I would like to give an introduction to Json.NET, the library I have been using to read and write JSON for about two years now.

Continue reading →

Serialisation – Comparing XML, SDL, TOML, JSON

Script files are an important asset for a lot of games.

There are a lot of different uses for script files. In general, we use the term to describe two different files, those containing scripts – as in code – and those containing data.

In this post we will look at the second kind. Specifically we will look at a number of representations of actual objects we might encounter in our game environment.

We will further only look at human readable formats. Binary formats certainly have their place, but for most purposes having files that can be opened and changed in any text editor has huge advantages that I do not want to miss.

I will introduce four examples, chosen somewhat arbitrarily. The goal is to see some of the different approaches people take, and explore the advantages and disadvantages of either using an example.

Continue reading →

Random elements, subsets and shuffling collections – LINQ style

LINQ was a major part of the release of .Net Framework 3.5, and has since been one of the most powerful features of the C# standard libraries.

Part of LINQ’s usefulness comes from the clarity that it can give to complex code handling collections.

In fact, it is not only useful for programs where one would classically use queries – like database applications – but can be used in a variety of ways in almost any application, including games. The most common use cases of LINQ deal with selecting, converting, combining and returning items from collections in clearly defined and deterministic way.

When it comes to selecting items from collections, many games have additional requirements not covered by LINQ.

One of them is randomness. We often need to:

  • select a random element from a set;
  • select a subset of elements from a set;
  • or shuffle an ordered collection.

In this post we will construct methods for exactly these purposes, using the same extension method style of LINQ.

Continue reading →

Angles, directions, and their binary representation

Something that probably every game developer has worked with at one time or another – and some of us might work with it daily – is angles.

Angles are used to represent directions and rotations and especially in 2D they are an easy way of doing so, since only a single number is needed to represent an orientation.

In 3D we need three angles – pitch, yaw and roll -, making it slightly more complicated.

In this post I want to explore the way we represent the concepts of angle, direction, orientation and rotation.

Continue reading →