# Rapid, Scalable Game development using Functional Reactive Programming

Ray Shulang Lei

### Imperative vs Functional programming

• Imperative - describes computation in terms of statements that change a program state.
• Functional - treats computation as the evaluation of mathematical functions and avoids state and mutable data, thus expressing the logic of a computation without describing its control flow.

### Imperative Example: Count all numbers that can be divided exactly by 5 from 1 to 150.

```			int sum = 0;
for (int i = 0; i < 150; i++)
{
if (i % 5 == 0)
sum += i;
}
printf(sum);
```

### Functional Example: Count all numbers that can be divided exactly by 5 from 1 to 150.

```			show \$ length [x | x <- [1..150], x `mod` 5 == 0]
```

• Expressive
• Easy to debug and test
• Easy to scale thus good modularity
• Easy to parallelized, adapt to concurrency well
• Rapid development
• Matchmatically elegant

### Pitfalls of functional programming

• Difficult to learn
• Hard to predict time and space complexity(due to lazyz eval)
• Hard to program for some real life problem because it abstracts from hardware more than imperative

### Problems of imperative programming in Game development

• Scaling requires lots efforts
• Debugging and testing requires lots efforts
• Hard to understand large projects
• Hard to extend modularly

### Great! Let's just use Functional programming for games then, easy:

```            GameLoop(
Players(), AIs(), Trees(), Guns(), Bullets(), ... )
```
• Wait a second! Something doesn't seem right here...
• Games have time-varying states
• Game objects interact with each other
• Game objects reacts to physical laws and player IOs

### I got it, give me one more chance, I can still do it, boss!

```            gameState = [playerPosition, playerSpeed,
t = currentTime()
GameLoop(
Players(gameState, t), AIs(gameState, t),
Trees(gameState, t), Guns(gameState, t),
Bullets(gameState, t), ... )
```
• This is ugly! Look at the giantic gameState!
• Mutable GameState variable
• This isn't functional programming, this is imperative!
• Games don't like functional programming, what are we gonna do?

# Functional Reactive programming to the rescue!

Definiation of FRP - functions react to an input signal and returns an updated signal.

A signal is a type value related to continuous time:
` Signal a = Time -> a`
A signal function is a function from Signal to Signal:
` SF a b = Signal a -> Signal b`
You can't touch the signals without using sigal functions.
Signal function combinators composite function(s) to a Signal Function: You can't touch the signal functions without using combinators.

### Nice! we can now do:

```signal(position, speed, mouseXY)
-> PlayerFunction
-> signal(position, speed, mouseXY)
```

An event is a type to represent discrete events such as a mouse click:

`data Event a = NoEvent | Event a`

### Looks like we are almost there:

```signal(position, speed, mouseXY), event(gamepad, collision)
-> PlayerFunction
-> signal(position, speed, mouseXY)
```

But but but, what if my player dies? How do I remove and spawn?

Also, how does my object know if it is colliding with each other?

### The last piece of the puzzle: Switches!

Switch switches from one subordinate signal function into another when a switching event occurs.
```            switch :: SF a (b, Event c) ->
(c -> SF a b) -> SF a b
```

What is this non-sense? What are these strange symbols stand for?

It's haskell...I know. It says a switch will take an activated signal function with a trigger event, and another signal function as input. If the trigger event occurs, it applies the second signal function, else it applies the original signal function.

### The big picture

We use switches to route events and switch on/off game objects for spawn/kill.

### Conclusion

Q:What framework can I start to use FRP for game dev?
A:My presentation is based on the Yampa framework in Haskell

Q:How well is this method perform?
A:Paul Hudak made a game "PaddleBall" with 17 lines of code in FAL

Q:Is there example of complex game out there made with FRP?
A:Check out Frag, a first personal shooter game

### Reference

• Henrik Nilsson, Antony Courtney, John Peterson, "The Yampa Arcade", 2003
• Mun Hon Cheong, "Functional Programming and 3D games", 2005