1 of 32

Game Dev for�Business Developers

Dylan Wolf

@dylanwolf

2 of 32

My background

  • Learned QBasic while in school because I wanted to make games
  • Lead to career in web and desktop software development (mainly .NET)
  • Learned XNA, Microsoft’s .NET game framework
  • Moved to Unity when Microsoft announced XNA support ending

Dylan Wolf

@dylanwolf

3 of 32

How I thought it was going to be...

Step 1: You already know this:

Step 3: Profit:

Step 2: ???

4 of 32

How it actually works...

Already know how to do this:

Web, Desktop, or Service Development Patterns

With this:

Game Development Patterns

(not really useful in learning)

(helps with learning)

(used to write code for)

5 of 32

So what’s different?

  • Different set of patterns:
    • Game loop vs. request-response or long-running process
  • Different scale:
    • 1/60th-second frames vs. requests and transactions
  • Different ways of thinking about state:
    • Frame-to-frame changes become important
  • Different set of tooling:
    • GUI editors actually save a lot of effort
  • Different performance limitations:
    • For example, memory and garbage collection become important on certain platforms

6 of 32

The Game Loop

7 of 32

You’re probably used to this...

Web request

Web response

Database read and/orwrite

Render HTML, JSON, XML, etc.

Button click�(or other UI interaction)

Event handler

Model or UI change

Service begins processing task

Database or message queue reads

Database write

Processing

Client-side Javascript

More or less event-driven.

8 of 32

How games work...

Player presses a button

Sprite begins moving

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Player releases button

Sprite stops

9 of 32

A simple game loop

Draw

Update

(Happens 30-60 times a second)

Can have events, but not really event-driven.

10 of 32

A more complex game loop (Unity)

Awake()

Start()

FixedUpdate()

Trigger/Collision events

Mouse events

Update()

LateUpdate()

Render events

OnDestroy()

(physics updates)

(input updates)

(internal init)

(init with other components)

Game Loop

11 of 32

Events in Unity

They exist, but they’re not all created equal.

  • UI element interactions
  • Service connections
  • Mouse interactions and collisions
    • However, sometimes it’s better to do the raycast yourself.

12 of 32

1 frame = 1/60th of a second

13 of 32

Let’s go back to this example...

Anything that runs over a long process needs to be broken up into tiny pieces.

Player presses a button

Sprite begins moving

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Sprite moves a little farther

Player releases button

Sprite stops

14 of 32

Converting to frame scale (Unity)

15 of 32

Timers (Unity)

16 of 32

Coroutines (Unity)

17 of 32

Frame-to-frame state changes

Necessary for components to track this in order to make big-picture features work.

Examples:

  • Timers
  • Input code:

Button is up

Button was just pressed

Button is down

Button was released

Button is up

Touch began

No touches

Touch moved or paused

Touch ended

No touches

18 of 32

How I learned to stop worrying�and love GUI editors

19 of 32

I’ve been burned by GUIs before

  • HTML editing with Netscape Composer or Microsoft Frontpage
  • SharePoint workflows

20 of 32

But...

This is all XNA gives you by default.

Draw

Update

21 of 32

What do I get with a GUI like Unity?

  • Built-in, visual support for things like scaling, rotation, positioning, collisions
  • Easier to debug frame-by-frame when you can see details, not just numbers
  • Writing code for game mechanics, not plumbing

22 of 32

You can go even further...

Some tools have no-code workflows like PlayMaker for Unity

23 of 32

What do I lose with a GUI like Unity?

  • Control over individual scripts, but not the “big picture.”
    • Engine design patterns and careful use of lifecycle methods can help.
  • Quirks

24 of 32

Is it worth it?

Maybe.

I find I’m more productive with Unity than I ever was with XNA, but YMMV.

25 of 32

Performance

26 of 32

Performance probably doesn’t matter to you

At least, not at first.

  • Start with simple games that won’t tax systems.
  • Learn on PC, then deploy to mobile/console later.
  • Learn how to do it first, then worry about the most efficient way.
  • You’ll probably throw away your earliest attempts as you’re learning.

TL;DR: You can learn patterns for better performance as you go, as necessary.

27 of 32

Garbage collection

In managed code like .NET and Java, cleans up objects that are no longer referenced.

It’s the price you pay for not having to manage memory yourself.

28 of 32

Garbage collection on desktop or server

In this environment:

  • Memory is plentiful
  • Transactions are not constant
  • Garbage collection thresholds are fairly lax

So:

  1. You create a bunch of objects in a transaction.
  2. Garbage collection runs between events/requests, before it’s a problem.
  3. You never even think about garbage collection.

29 of 32

Garbage collection on XBox 360

In this environment:

  • Limited memory (512MB)
  • Your code is running 60 times a second
  • Garbage collector fires every 1MB of allocation

So:

  1. You potentially create object(s) 60 times a second, in a continuous loop.
  2. You hit 1MB of allocation in the middle of an action sequence.
  3. The garbage collector fires and you noticeably drop several frames.

30 of 32

So what’s the solution?

  • Be careful (maybe even paranoid) about “new”-ing up objects
  • Ensure you release unmanaged resources (in C#, using or Dispose())
  • Pool and re-use objects rather than recreating them

31 of 32

Profiling

32 of 32

Questions? Comments?

Blog: http://www.dylanwolf.com/

Twitter: @dylanwolf

Released games: http://apps.funcworks.com/