Friday, December 6, 2013

Domain Specific Languages

In one of our previous Game Engine Design lectures we watched a presentation that covered scripting as technology and domain specific languages (DSL). For this blog I will be focusing on the subject of a DSL.

First things first, what in the hell is a DSL?

A DSL is a type of programming language that is designed to solve a specific class of problems. DSLs are (or should be) designed in way that is easy to understand and easy to use. The idea is that while a DSL is indeed a programming language, it should not have a learning curve as high as a programming language such as C++ or Java. In fact, a DSL is usually built on top of a lower-level language such as C/C++ and is used to simplify the process of programming for designers. DSLs can simplify statements such as:

if(playerIsHit == true)
{
            player->removeHealth(weapon.damage)
}

To plain old english:

When player is hit by weapon remove health

As I mentioned before, DSLs are designed to solve a specific class of problems which can include game elements such as AI, cutscenes, and basic game logic. DSLs are stored in text files, in a similar fashion to scripts, meaning you don't have to recompile your engine every time you change a tidbit of game play logic. In addition to the time management benefits, DSLs also help keep engine code clean and much easier to debug for programmers.

A rudimentary but good example of a DSL are the controls for text-based adventure games. When you type a message such as "go west" the text is read in, and interpreted using a dictionary defined by a programmer, then the appropriate functions are called. A DSL would essentially work in the same way except the instructions could be stored in a file and fed to the program.

The fact that creating a text-based framework is so easy, disproves a common misconception that creating your own language is difficult. The reason this is a misconception  There are a few things one has to keep in mind when designing a DSL. Namely you have to identify the core problem you are trying to solve using a DSL. Next you'll want to select algorithms and solutions for any identified problems and break down the solution into separate logical pieces. Finally you would implement a DSL concept for each piece.

In terms of how code is stored, it is probably a good idea to use existing file formats, such as XML since there are libraries openly available that allow you to parse the code, making it easier focus on optimizing the engine itself and the actual design of the DSL.

In conclusion, DSLs can be useful if they are used and designed wisely. It can take a bit of effort to get one complete, but in the end it's definitely worth it and plus it doesn't hurt to experiment (don't do drugs kids).

No comments:

Post a Comment