F# for Fun and Games

Why FP matters for games

Anthony Brown @bruinbrown93

About me

This talk

  • What is F#?
  • What is MonoGame
  • Why should I combine them?

What is F#?

F# is a strongly statically typed, multi paradigm, eagerly evaluated, open source high level programming language from Microsoft that runs on the .NET framework

What is MonoGame?

Open source cross platform implementation of XNA

  • Windows Phone 8
  • Windows 8 App Store
  • Windows Desktop OpenGL/DirectX
  • Android
  • iOS
  • Ouya
  • Mac OSX
  • PlayStation Vita
  • Playstation 4
  • ?
let tex = Content.Load<Texture2D> "player.png"
do spriteBatch.Begin ()
do spriteBatch.Draw(tex, Vector2.Zero, Color.White)
do spriteBatch.End ()

Why together?

I really just wanted to play with the language

  • Speed
  • Runs everywhere
  • John Carmack says functional is good for games

Eliminated the need for prototyping

Demo time

Making a platformer

C# - Team of 5 - 1 day

F# - Team of 1 - 4 hours

REPL

Type abbreviations

type Enemy =
	| RocketMan of int * int * string * string
	| Snail of float * int * string
type Health = int
type Lives = int
type Name = string
type Catchphrase = string
type Speed = float
type Enemy =
	| Rocketman of Health * Lives * Name * Catchphrase
	| Snail of Speed * Health * Name

Units of Measure

let Move x y =
	x + y
[<Measure>]
type Metre
let Move (x:float<Metre>) (y:float<Metre>)
	x + y

Computation Expressions

let x = 32
log x
let y = 2
let z = x * y
log z
log {
	let! x = 32
	let y = 2
	let! z = x * y
	return z
}
var enemy = World.Enemy;
switch(enemy.State)
{
	case AIState.Moving:
		...
	case AIState.FindingPlayer:
		...
}
let ai = new AIBuilder()
ai {
	move 10
	let! player = World.Player
	find player
	chase player
}

Want more?

http://fsharp.org/apps-and-games/
http://monogame.net/documentation
http://bruinbrown.wordpress.com
https://github.com/bruinbrown