19

I have had my eye on functional programming languages for a while, but am hesitating to actually get into them. But I think it's about time I at least starting glancing that direction to make sure I'm ready for anything.

I've seen talk of Haskell, F#, Scala, and so on. But I have no clue the differences between the languages and their communities, nor do I particularly care; except in the context of game development.

So, from a game development standpoint, which functional programming language has the most features suited for game programming? For example, are there any functional game development libraries/engines/frameworks or graphics engines for functional languages? Is there a language that handles certain data structures which are commonly used in game development better?

Bottom line: what functional programming language is best for functional game programming, and why?

I believe/hope this question will declare a clear best language therefore I haven't marked it CW despite its subjective tendency.

Tetrad
  • 30,124
  • 12
  • 94
  • 143
Ricket
  • 14,810
  • 6
  • 66
  • 82
  • 2
    "What do you mean, 2D games, or 3D games?" "Huh? I don't know - aieeee " – Cyclops Jul 28 '10 at 15:41
  • @Cyclops uh, what? I don't understand... – Ricket Jul 28 '10 at 17:15
  • @Ricket, partly it was a request to clarify what type of game you're talking about, 2D or 3D (and maybe real-time or turn-based). Partly it was a Monty-Python joke which obscured the first part. Wups. :) And I do think specifying the type of game, is useful - unless there is a single functional-language that is perfect for everything, then different languages may be better at different things. – Cyclops Jul 28 '10 at 19:06
  • I don't believe 2D or 3D impacts a language choice, except by the variety of engines/libraries available for a language. I am personally interested in both 2D and 3D so I wasn't thinking of one or the other when writing this question. But overall, 2D and 3D games share a lot of common systems and data structures (e.g. audio, input, AI, program structure, game loop) and there are many 3D games which could be ported to 2D or vice versa. And yeah I haven't seen Monty Python so that's why I didn't understand it at all. :) – Ricket Jul 28 '10 at 23:13
  • "I don't believe 2D or 3D impacts a language choice, except by the variety of engines/libraries available for a language." True, but that's exactly what you asked - "are there any libraries/engines/frameworks or graphics engines for functional languages?" So I thought clarification might be useful. (Then again, given that all game-related Functional language tools can probably be counted on the fingers of one hand, it might not matter whether you're looking for 2D or 3D. :) – Cyclops Jul 29 '10 at 13:49
  • 1
    I don't think there's a definite answer about the best language. – Wei Hu Aug 12 '10 at 19:30

3 Answers3

12

F# being part of Microsofts .net family has access to XNA, which is a fantastic base for building games. A bit of googling turns up some tutorials, videos and articles. The great XNA docs should help as well.

There has been some movement in games for Haskell as well, see here. You'll probably be using openGL bindings.

Since Scala plays in Java land, it integrates with all the available Java engines/bindings, see this SO post.

I think it comes down to preference, while I've only played with Haskell, I'd imagine each functional language has it's own idiosyncrasies. It may be worth whipping up a small test game in each and seeing which one feels better to you.

Like I said I only have minimal Haskell experience, so I can't comment on the best, but these resources should get you started hopefully.

cmjreyes
  • 341
  • 2
  • 5
  • 2
    Between problems with JVMs and the immaturity of the compiler implementation, I've found Scala's performance unacceptable when using a functional style, even though the language itself supports it well. It ends up being about as fast as, say, Python or Ruby, not F# or OCaml. (Scala's speed is competitive for more OO / procedural styles.) –  Jul 28 '10 at 14:35
  • I actually got an F# project running XNA (that is an empty XNA window with cornflower blue, only the code was F# and not C#. – RCIX Jul 28 '10 at 16:06
8

I would say Lisp.

It has been used in games as a scripting language, at Naughty Dog at least (or it was I think), and is a very mature language.

The advantage of Lisp is in deserialization, which is a major part of what we call asset management. Deserializing lisp is trivial, the code and the data are one. This makes it easier to have a file format for assets and behaviors. It's not like you a one in json/xml/yaml/bin and one F# file for AI. You can save everything as s-expressions which is going to simplify the asset pipeline a lot.

Jonathan Fischoff
  • 1,148
  • 9
  • 14
  • TBH i'd recommend against this. Scripting languages are not just for the developer to use; modders will want to mod the game, and there's nothing that will scare them off faster than (()())))()((()). Plus, even as far as functional languages go, Lisp is esoteric. – RCIX Jul 29 '10 at 00:45
  • 7
    If scaring off modders is something that worries you, going for a functional language is probably not the right move. – Paul Wicks Jul 29 '10 at 20:31
  • @RCIX: Have you even tried lisp? From what I'm reading there it doesn't seem like you've even looked at it — only heard of it. – Joe D Nov 23 '10 at 16:42
  • @Joe: I haven't actually tried it, but i've seen a lot of it. Everything I see indicates it simply wasn't designed to be used as A: an introductory programming language (which is what it will be for at least a few people) or B: one that is very lightweight (something you also want in your scripting languages). – RCIX Nov 23 '10 at 22:21
  • @RCIX: I will agree with you on A, that it wasn't designed to be introductory programming language. But I have to disagree about B, Scheme is a very lightweight programming language, perfectly suitable for game scripting – in fact their's a version designed just for embedding: GNU Guile. – Joe D Nov 24 '10 at 18:48
  • 3
    @RCIX & @Joe - Necro powers ACTIVATE. Anyway, Scheme is used as a teaching language. SICP is something like half a CS degree in one book and is used by a bunch of schools (including MIT until recently), and HTDP is a less hardcore introduction. – Jesse Millikan Feb 15 '11 at 04:03
  • 1
    @RCIX & Joe indeed. lisp & scheme are the introductory languages at a lot of universities. – oberhamsi May 26 '11 at 14:12
  • 1
    Lisp is not something you want to use. Naughty Dog had ONE, single Lisp programmer and when he quit they transitioned all their engine(s) to C++. No idea why an obsolete engine from one company gets dragged out as a shining example all the time =) – Patrick Hughes Nov 24 '11 at 17:38
  • Well you can include an IDE that hides the parens of Lisp. The parens are just one way of representing code trees, you can also represent them as nested boxes. – aoeu256 Apr 23 '21 at 16:07
8

After gaining some practical experience with it, I would vote for Clojure - already written one little game in it, and planning to do more.

Reasons:

  • Productivity - Clojure is a dynamic language. It has great features for interactive REPL-style development, for example you can use the REPL to query or alter state inside a running game application instance. Code in Clojure is also often ridiculously concise. I'd guess I am 3-5 times more productive in Clojure than in Java or C#.
  • Impure - while I appreciate the beauty and elegance of pure functional languages like Haskell, game programming is a messy pursuit where pragmatism wins. I want to be able to put side effects in my functions when I need them, and Clojure allows this. Having said that, idiomatic Clojure style is to be purely functional most of the time.
  • Metaprogramming - Clojure's Lisp heritage as a homoiconic language makes it great for metaprogramming / code generation. It's easy to "extend the language" to make your own DSLs or eliminate biolerplate. You could easily use Clojure as it's own game scripting language, and the dynamic code compilation means that your scripts will run just as fast as your core game engine.
  • Access to Java libraries. You can use all the sound / graphics / AI / networking libraries from Java, which is a huge advantage vs. most other functional languages (where you'll have to either write these from scratch or develop a lot of tricky interfacing code). This can be a life-saver.
  • Cross-platform - this comes for free from running on the JVM, but it's a nice bonus.
  • Performance - for a dynamic functional language, Clojure is about as fast as you can get. It is always JIT compiled, never interpreted. with intelligent optimization you can match the speed of pure, low-level Java code in Clojure. For example, you can use Java primitive arrays, or provide type hints to ensure that the compiler produces statically typed function calls in performance-critical code.
  • Concurrency - Clojure has a great model for managing concurrent state, supported by an impressive software transactional memory system. Worth seeing this video for more insights on how this works.

The only time I wouldn't consider Clojure would be if I was writing a state-of-the-art graphically intensive game where you need cutting edge performance (where I still think you need C/C++).

mikera
  • 1,046
  • 6
  • 10