Game Engines
Planning & Design
(please email me at pdiffenderfer@[Link] with any criticisms)
How to Plan & Design a Game Engine
Game Engines are complicated and like all complicated projects it's best we
understand the problem space, the decisions we need to make, and the different
solutions available.
We must choose which components of a game engine we want, which features we want
per component, and which designs we want to undertake to build the components.
Core Components of a Game Engine
* = components often required to create a game engine
● AI (objects that have behavior) ● Math* (vectors, matrices, & shapes)
● Animation (anything that moves) ● Multithreading
● Asset Management* ● Networking
● Audio* ● Particle Effects
● Core* (game state, memory, data structures) ● Physics
● Entity* (sprites, characters, meshes) ● Rendering* (graphics, scenes, cameras, windows)
● Entity Management* ● Screens* (menu, loading, gameplay)
● Event System ● Scripting System
● Files & Streams (parsing & writing data places) ● System Integration
● Game Loop* ● User Interface
● Input* (keys, mouse, controllers, sensors)
Secondary Components of a Game Engine
** = components broken out into their own because multiple components depend on them
● Debugging Utilities
● Easing** (animation velocity)
● Game Patching & DLC
● Gaming Services (score boards, authentication servers, profile saving, match-making services)
● Localization (multilanguage support)
● Maps (tiles, hex, heightmap, platforms, converting between screen points and map locations)
● Paths** (lines & curves)
● Profiles (game state, achievements, preferences)
● Spatial Database (index and query entities in space)
● Tasks (break up processing into pieces that can be ran asynchronously)
● Tools
● Types (expose game objects to debugging, networking, file system, and tool components)
Example Organization
Implementation & Design Considerations
● Which Language [& Framework]?
● Which Graphics Library?
○ Graphics library agnostic?
● Which Dimensions? (2D, 3D, isometric)
○ Dimension agnostic, all 3D, or separate classes for 2D & 3D?
● Which Components?
● Any 3rd Party Libraries?
○ Behind a facade to swap out implementations?
● Cross-platform?
○ Components should operate on an interface to abstract the platform specific code
■ Input, Audio, Multithreading, Files & Streams, Asset Management, Networking, Memory
Structure of Presentation
● What do we want to build?
○ For each core component
■ Define basic responsibilities
■ List possible features / implementations
● Pros & Cons
● What designs / solutions are available?
○ For each component
■ For each feature
● Discuss solutions
○ Pros & Cons
● Our Game Engine Design
What do we want to build?
AI
Responsibilities Sub-Components
● Add behavior and intelligence to actors. ● State Machine
○ Managing state & transitions of an actor. ○ Finite
● Helping actors traverse their environment. ○ Fuzzy
○ Finding a path ● Path Finding
○ Following a path ● Steering Behaviors
○ Avoiding collisions
○ Group movement
● Control how much processing time is spent
per frame by queueing tasks (see Tasks
component)
AI > State Machines > Finite
Responsibilities Features
● Manage the state of an actor ● Conditions
● Handle transitions between states ○ Evaluates to true or false
● Handle multiple transitions (which one to ○ Compound (and, or, not, xor)
○ Require N true/falses in a row before true
apply)
● Transitions
○ Prioritized
○ If condition is true, go to state
○ Random Selection
● Prioritized transitions
● Random transitions
● Transition from given state or any to a
specific state
● Periodic condition evaluation
AI > State Machines > Fuzzy
Responsibilities Features
● Manage the states of an actor ● Conditions
● Each state can be active to some degree ○ Evaluates between 0.0 and 1.0
○ Compound (min, max, avg)
○ Running average
● States
○ State is active based on condition
● Periodic condition evaluation
AI > Path Finding
Responsibilities Features
● Given a "map" and two "points" on the map, ● Nodes & Edges
return a path between the points ○ Have a constant or dynamic cost
○ Map and points don't need to ○ Can only be traversed by certain actors
represent physical values ● Heuristics
○ Estimates cost from current node to target
● The path returned should be the best
● Path simplification & optimization
available for the given actor
● Path caching
● Control how much processing time is spent
● Dynamic path evaluation & recalculation
per frame by queueing tasks (see Tasks
○ Node traversability changes
component)
● Partial & incremental path calculation
● Hierarchical pathfinding (a tree of graphs)
AI > Steering Behaviors
Responsibilities Features
● Add realistic movement to actors in their ● Target an object, position, future position,
environment offset to another target
● Adjusts movement behavior based on the ● Filter which targets are seen based on
state of the actor proximity, field of view, & object type
● Constrain actor movement (believability)
● Steering behavior
○ To, Away, Arrive, Wander, Follow, Path,
○ Cohesion, Separation, Alignment
● Compound behaviors
○ Prioritized
○ State Machine uses actor state
○ Context uses concept of good and bad forces
Animation
Responsibilities Features
● Change one or more properties of an entity ● Generic Animation System
from one set of values to another set of ● Play, Pause, Resume, Queue, Seek, Reverse
values through a path of points ● Partial Animations
○ Sprite animations ● Transitioning
○ Skeletal animations ● Animation Blending
○ Camera zooming, movement, shaking, etc
● Additive Animations
○ Color, scaling, rotation, alpha, etc
● Sequential Animations
● Constraints
● Spring & Physical Forces
● Linear, Curved, Disjoint, and Combo paths
● Keyframe Animations
● Animation Events
● Multi-input Animations
Asset Management
Responsibilities Features
● Loading an asset (texture, animation, model, ● Live asset reloading
font, etc) from any source ● Custom sources
○ Embedded, file system, http, zip file, database ● Custom format
● Converting assets from raw format into ● Caching
game friendly structures ● Asynchronous loading
● Control how much processing time is spent ● Bundles
per frame by queueing tasks (see Tasks ● Context
component) ○ Ensures only assets the game currently needs
are loaded - releases unneeded assets
○ One or more contexts can be active at a time
Audio
Responsibilities Features
● Loads and streams audio files ● Play, pause, resume, mute, stop, repeat, fade
● Handles system limitations by ensuring most ● Stereo playback based on position of sound
appropriate sounds are playing ● Volume adjustment based on obstacles
● Manage the state and volume of sounds ● Sound effects based on environment surface
based on game state ○ Uses a state Machine
● Prioritized sounds when the system is
limited by concurrent sound instances
○ By type, distance, volume, etc
● Maximum instances per sound
● Sound banks which choose a sound to play
based on probability
● Mass actions on sound instances based on
flags (IN_WORLD, UI, AMBIENCE, etc)
Core
Responsibilities Features
● Basic structures and utilities ● Game State
○ Timing and drawing data
● Memory
○ Pooling / caching
● Pointers / Handles
● Data Structures
○ Lists, Stacks, Queues, Trees
○ Primitives & Objects
● Interfaces
○ Clone, Factory, Bufferable
● Bitset Utilities
Debugging Utilities
Responsibilities Features
● Assist developer in finding and fixing bugs ● Live Graphs
○ Based on 1 or more round-robin databases that track
and poor performance data points
● Handles the "real-time" requirements a game ○ Memory, networking, profiling data
has with the requirements to be able to ● Profiling
○ Nested profiling is tracked
inspect code & values
● Console
○ Displays game activity and accepts commands
○ Verbosity setting
○ Channels
● Multithreading concurrent access detection
○ Trying to write and read a shared resource at the same
time
● Snapshots
○ Take singular or multiple frame snapshots of the state
of the game and display or log them
○ Multiple snapshots can be aggregated
Easing
Responsibilities Features
● Provide functions for controlling the velocity ● Easing
of an animation ○ Single argument function which takes a delta
and returns a new delta
○ Ex: linear, elastic, ease, cubic, quartic, sine
● Easing Type
○ Double argument factory which takes a delta
and an easing function and generates a new
easing function
○ Ex: in, out, reverse, ping-pong, back, in-out
● Easing Scale
○ Double argument factory which takes a
scalar value and easing function and
generates a new easing function
● Parses easing expression strings into Easings
Entity
Responsibilities Features
● Provides interfaces for game objects which ● Templates
can be updated, drawn, hidden, disabled, ○ Describe an entities shared values (available
expires, etc animations, effects, etc)
● Provides a base set of game objects ● Sprite
○ Basic, Rotated, Animated, Tiled, Physics
● Character
○ Skeletal Animations
● Mesh
○ Static, Dynamic, Animated
Entity Management
Responsibilities Implementations
● Efficiently update and render the entities in ● Simple data structures
the game ○ Managed arrays, auto-pruning lists, linked
○ Only update entities that need it lists, trees, layers of lists
○ Only render entities that are visible ● Entity Component System
● Store entities in some traversable structure ○ Components of an entity are stored
● Handle entity removal/expiration and coherently in memory for quick updating
○ Systems iterate over all entities with
insertion/addition efficiently
components X, Y, and Z
○ Time efficiency
○ Entities don't need to really exist as an object,
○ Memory efficiency (pooling)
only as an identifier
● Scene Graph
○ Tree of nodes where a node and its
descendants could be invisible to the user
and efficiently ignored
● Mixed System
Event System
Responsibilities Features
● Simple and decoupled communication ● Immediate or delayed event triggering
between different components in a game ○ Some events could interfere with update
● Can handle any number of subscribers logic and should be queued and batch ran
listening for an event ● Subscribers can have priority and can ignore
● Allows events to pass metadata to events temporarily or forever
subscribers ● Event propagation can be stopped by a
● Events can be binded to other components subscriber
○ UI can trigger events
○ Events can be sent to a server via networking
Files & Streams
Responsibilities Features
● Provide functionality for serializing and ● Format-agnostic
deserializing game objects to and from one ○ The same code could read and write to
or more formats multiple formats (JSON, XML, binary, etc)
● Offers compression and decompression ● Reflection based translation
○ Some languages offer automatic translation
functionality
● Compress/decompress binary data
○ Useful for networking
● Map between discriminators and translators
○ Discriminator is a string like "vector" which is
stored in data to know what to create
● Map between data types and translators
Game Loop
Responsibilities Implementations
● Determines when updating and rendering ● Variable
need to occur ○ Every iteration performs an update and
● Provides timing information to update logic render, no maximum rate (fps)
on time elapsed since last update ● Interpolated
○ Updates at a fixed rate but draws every
● Provides interpolation data to rendering
iteration and sends interpolation data to
logic when the game is set to render more rendering for frames between updates
than update (i.e. fixed updates per second) ● Fixed
○ Updates and renders have their own rate, any
extra time is spent "sleeping" the thread
Game Patches & DLC
Responsibilities Features
● Contacts the server and determines which ● Resource registry with timestamp or hashes
files are out of date and handles ● Client and resource server both have
downloading and installing registries for game and each DLC
● Contacts the server about new files available ● Registries have their own hash and
and downloads and installs them timestamp for quick patch checking
● Separate servers store files with their
timestamps or hashes and can be securely
downloaded
Gaming Services
Responsibilities Services
● Allows a game to connect to servers which ● Scoreboards / Leaderboards
add social and competitive value ● Social Platform Integration
○ Facebook, Google+
● User Authentication & Management
● Profile Sync
○ See Profiles component
Input
Responsibilities Features
● Convert raw input data from devices (mouse, ● One or more contexts can be active
keyboard, controllers, accelerometer, etc) ● A context has mappings between input and
into a platform independent API actions
● Provide input state access or input state ● Actions can be called on key up, key press,
change listeners (or both) key down, or the entire duration of a state
change
● Actions can be binded to an event system
● Inputs can provide a value on a range
(mouse, joysticks, accelerometer)
● Inputs have triggering ranges and filtering
● Input combinations (Ctrl + A)
● Gesture Detection (U U D D L R L R B A)
Localization
Responsibilities Features
● Provides support for toggling between ● Localizations are stored in files containing
different localizations: ○ Map of key to text
○ Multi-language ○ Date formats
○ Date formats ○ Number formats
○ Number/currency formats ○ Primary units of measurement
○ Units of measurement ○ Algorithm information
○ Algorithms ● Formatted text which contains functions for
■ Different locales could implement translating dates, numbers, and between
game logic differently units
Maps
Responsibilities Implementations
● Render a section of a map based on the ● Heightmap
camera ● Tilemap
● Provide information about the map to other ○ Used in top-down and platformers
components ● Hexmap
○ Traversability (AI > Path Finding) ● Isometric
○ Objects to add to Physics ● Sectioned
● Convert between screen coordinates and ○ Maps broken up into pieces for minimizing
map coordinates memory usage and the number of entities to
○ Trigger map events based on mouse update & simulate physics
● Provide invisible map objects that the player
can interact with
○ Trigger areas could trigger events
Math
Responsibilities Features
● Provides data structures and algorithms ● Dimension agnostic structures and
necessary for 2D and 3D games algorithms, separate 2D and 3D, or just a
○ Primitives singular dimension
○ Geometries ● Primitives
○ Springs ○ Vector, Point, Matrix, Color, Quaternion,
○ Plotting / Raytracing Ranges
○ Distance Calculators ● Geometry
● Provides common non-spatial mathematical ○ Bounds, Rectangle, Sphere, Line, Plane,
functions and utilities Segment, Polygon
○ Random Number Generation ● Springs
○ Noise ○ Linear & distance (Euclidean and
○ Probability Map Manhattan)
Multithreading
Responsibilities Features
● Allow the game code to be broken up into ● Thread Manager
individual tasks that can be executed in ○ Sleep time between tasks
multiple threads ○ Target tasks per second/minute
○ Max number of threads
● Provide utilities for ensuring threads are
○ Successful tasks ready to be finished
NOT reading and writing to a resource at the ○ Extra threads can be created for large tasks
same time
● Provide information about how much time it
takes from task queue, to start, to end, to
finishing on the main thread. This may help
determine where multithreading doesn't
make sense.
Networking
Responsibilities Features
● Provide a nearly seamless experience playing ● Connect to any number of servers
the game with other people ● Setup multiple channels to control congestion
○ Handles slow connections ○ A channel has a priority, whether or not
● Converts messages or RPC into bytes to be the messages need to be ordered, and
written to packets whether or not messages need to be
● Allow a client to download resources async reliably sent
● Messages can be cancelled, have their own
● Provide functionality for creating user
priority, have a time they need to be sent by, and
servers, matchmaking servers, and game
a number of times we can try to send it again
servers
● A packet has a maximum size so lower priority
messages are queued for later
● Each endpoint has a send rate in FPS
Networking > Client
Responsibilities Features
● Communicates with one or more servers ● State-based validation of messages sent from
○ Authentication server
○ Updates ● Command oriented (authoritative server)
○ Matchmaking ○ Client collects commands (inputs) and sends
○ Gameplay
them to server, server responds with game
● Can act as a dummy terminal sending state updates
commands to the server and predicting the ○ Client-side prediction simulates what the
outcome OR acts authoritatively and sends server might send and the client handles
game state to server OR cooperates with corrections if any are required
other clients in a deterministic simulation ● State oriented (authoritative client)
○ Client sends the game state and the server
(common for RTS)
can validate it to a degree - then passes it
● Manages actor events sent from server along to the other clients
(create, update, delete)
Networking > Gameplay Server
Responsibilities Features
● Receives and validates messages sent by ● State-based validation of messages sent from
clients client
○ Messages can represent commands, actor ● Validate commands / states sent by client
state, variables, functions (RPC) ○ Logical and based on game state history
● Behaves as the authority of the game state or ● Command oriented (authoritative server)
a passive validator ○ Server processes inputs and updates local
● Keeps track of the last X game states to game state
validate past messages ● State oriented (authoritative client)
● Manages all actors that exist in game and ○ Server can validate the state sent and
broadcast it to clients
need to be synced with the clients
● Each client has a list of relevant actors
● Determines which messages need to be
○ Based on vision, hearing, recent
passed to each client to minimize the
● LOD actor state communication
amount of data sent out ○ Full, partial, sounds, none
Particle Effects
Responsibilities Features
● Spawns and animates particles ● Entity Component Facade
● Determines which particle systems should be ● Deterministic Behavior
rendered ● Burst particle system
● Efficiently handles the rapid creation and ○ Burst size, frequency, particle life, etc
destruction of particles and how that affects ● Trail effects
memory usage and fragmentation ● Emitter listener defines initial state and final
states
● Emitter modifier actively animates the
particles
● Particle system AABB for occlusion tests
● Particle ordering for 3D systems
Paths
Responsibilities Implementations
● Using one or more points, generates a point ● Point
○ Single point
with a delta value between 0.0 and 1.0
● Linear
○ Points can be anything (vector, angle, etc) ○ Interpolates between a set of points (tween,
● Used for motion, sprite animation (frames), linear, keyframe)
skeletal animation, animation transitioning, ● Curved
○ A path that goes through the starting and ending
particle animation, etc
points and maybe points in between (cubic,
quadratic, spline, catmull-rom, etc)
● Disjoint
○ Multiple points without interpolation
● Combo
○ Multiple paths
● Sub
○ Small section of another path
Physics
Responsibilities Features
● Simulate ● Shapes
○ Rigidly body collisions & resolution ○ Single Geometry
○ External forces ○ Compound
○ Motors ● Motors
○ Constraints ● Constraints
● Triggers events ● Events
○ Collisions
● Collision Data
○ Motor or constraint sensors
● Generic Facade
● Keep track of collision data (contact points
● Deterministic
and normals)
Profiles
Responsibilities Features
● Provides a way to save player preferences, ● One or more profiles
saved game states, and any achievements. ● Profiles synced with user gaming service
● Profile has:
○ Preferences
○ Achievements / trophies
○ Game state saving & loading
○ Custom data
Rendering
Responsibilities Features
● Creates windows, scenes, and cameras ● Scene Graph
● Determines which entities are visible and ● Decoupled Rendering System
need to be rendered ○ Each entity has a view which maps to a
● Handles interpolation between entity states rendering implementation loaded at startup
when there are more rendering calls than ● Graphics library agnostic rendering
○ Shaders, Vertex Buffers, Fullscreen Post
updates
Processing Effects, Textures, Surfaces,
● Decouples the graphics library from the Meshes, Sprites, Immediate Mode Rendering
game logic (Graphics), Fonts
○ Useful for multi platform support and ● One or more monitors, one or more
networked games
windows, one or more views per window
○ A view renders a scene from a camera's
perspective
Screens
Responsibilities Features
● Manage screens and transitions ● Screen Manager
● Screens handle their own input ○ Handles current screen and all subscreens
● Screens require certain assets to be loaded ○ Handles asset loading
before rendering ● Triggers events around screen transitioning
● Screens draw their own scene and update ● Commonly used screens
○ Loading (Assets)
their own entities
○ Video
● Events can trigger screen transitioning ○ Animation (Logos, Credits)
○ UI can trigger events
Scripting System
Responsibilities Features
● Allows a secondary uncompiled language to ● Script Schedules
handle game logic ○ Determines if and when one or more scripts
● Loads and executes the scripts on command are ran during gameplay
or on schedule while passing the necessary ● Script Groups
inputs ● Live Script Reloading
● Scripts have access to variables and functions ● Easier Game Logic updating with patches
in game ● Tiered Variable Scopes
○ Global
● Enables a game to be moddable
○ Group
○ Function
○ Parameter
● Memoized Scripts
● Save & Restore Scopes (save/load game)
Spatial Database
Responsibilities Features
● Indexes objects in space based on their ● Querying
position and geometry ○ Intersection
● Can be queried for all objects ○ Containment
○ K-nearest-neighbor
intersecting/contained in a geometry
○ Ray tracing
● Handles spatial entity position updates
● Continuous Querying
○ Notify of changes in query result
● Collision Callbacks
● Implementations
○ Grid
○ Binary Tree
○ Quadtree / Octree
○ Sweep and Prune
○ Kd-tree
System Integration
Responsibilities Features
● Provides system information to the ● Operating system information
developer ● Screenshots & GIFs
○ Memory, number of CPUs, OS
● Record videos and screenshots
● Provides system-specific support for the
following components:
○ Rendering
○ Input
○ Files & Streams
○ Profiles
Tasks
Responsibilities Features
● Allow the game code to be broken up into ● Tasks
individual tasks and be executed based on ○ Priority
priority ○ Time constraints
○ Status (pending, running, canceled, success,
● Run tasks within a time frame to ensure a
failure, finished)
consistent frame rate / user experience ○ Submitted, starting, & completion time
● Task chains and groups
● Task Manager
○ Runs tasks within time frame
○ Keeps tasks ordered based on priority
Tools
Responsibilities Tools
● Assist the developer in creating, converting, ● Script editor
and organizing assets ● Particle system editor
○ Assets in the most appropriate format results ● UI editor
in quicker load time ● Map editor
● Provide some basic functionality so the ● Animation editor
developer can create their own tools that ● Character editor
integrate with their code base ● Asset converter
● World editor
○ Combination of previous tools
○ Import and place assets in a map
○ Organize entities in scene graph
○ Set entity properties
○ Test out game
User Interface
Responsibilities Controls
● Provides controls that can be utilized with ● Inputs
the mouse, keys, or a controller ○ Text, dropdowns, checkboxes, radios, etc
● Provides skin-able controls and a default skin ● Actions
so tools can easily be created ○ Button, toggle button
● Controls can be skinned based on state ● Containers
○ Lists, menus, toolbars, dialogs, sidebar
(default, focused, selected, hover, etc)
● Text
● Loads controls from an asset or build them
○ Labels, scrollable text, tooltips
programmatically
● Layouts
● Handles ordering, what controls are focused, ○ Absolute positioning, responsive, stacking,
where input is triggered, how input events grids, tables
propagate or bubble
Types
Responsibilities Features
● Expose game object properties and types and ● Get properties
associated metadata ○ primitive types
● Provides an avenue to inspect and record the ○ collection types that can be iterated
○ some properties are read-only
state of any number of objects in a way the
○ some properties are virtual
developer can extend.
● Given a root value & type and a path get or
● Can be used by editors and other tools to
the value at the end of the path
modify game state or setup game state live.
● Can be used by serialization/deserialization
to convert objects into formats like XML &
JSON
● Can be used by networking system to encode
types into smallest number of bits as
possible.
What designs / solutions are available?