Game Development
and Ruby
Saturday, November 3, 12
Slide 2
Slide 2 text
Andrew “Cad” Nordman
@cadwallion
cadwallion
Saturday, November 3, 12
Slide 3
Slide 3 text
DAY JERB
Saturday, November 3, 12
Slide 4
Slide 4 text
History
Saturday, November 3, 12
Slide 5
Slide 5 text
UnrealEd
• Level Design
• Event Trigger System
• Particle Effect Manager
• One-click Playtesting
Saturday, November 3, 12
Slide 6
Slide 6 text
UnrealScript
• Inspired by Java
• Rather Verbose
• Awkward OO Principles
• Lacking in Language Functionality
Saturday, November 3, 12
Slide 7
Slide 7 text
class MyPawn extends UDNPawn
dependson(UDNPawn);
simulated function Tick (float DeltaTime) {
local vector tmpLocation;
super.Tick(DeltaTime);
tmpLocation = Location;
tmpLocation.Y = 275;
SetLocation(tmpLocation);
}
DefaultProperties {
ControllerClass=class'MyGame.SuperBot'
MaxStepHeight=15.0
MaxJumpHeight=65
}
class MyGameInfo extends UTGame;
DefaultProperties
{
MapPrefixes(0)="MYG"
DefaultPawnClass=Class'MyGame.MyPawn'
BotClass=class'MyGame.SuperBot'
HUDType=class'MyGame.SimpleHUD'
bUseClassicHUD = true
DefaultInventory(0)=none
}
\Development\Src\MyGame\Classes\MyGameInfo.uc \Development\Src\MyGame\Classes\MyPawn.uc
Example UnrealScript Code
Saturday, November 3, 12
Slide 8
Slide 8 text
Unreal Development
Kit (UDK)
• UnrealEd 3.0
• Designed for Mods & Full-on games
• Extensive UnrealEd Improvements
• UnrealScript Mostly Unchanged
Saturday, November 3, 12
Slide 9
Slide 9 text
UnrealRb
• CoffeeScript-inspired language compiler
• Written in Ruby
• Ruby code in
• UnrealScript out
Saturday, November 3, 12
Slide 10
Slide 10 text
This was a terrible idea.
Saturday, November 3, 12
Slide 11
Slide 11 text
UnrealRb Problems
• Poor Performance
• Interfacing Bugs with UDK
• Still bound by UnrealScript structure
• Devolved into primitive DSL
Saturday, November 3, 12
Slide 12
Slide 12 text
However...
What if Ruby could be used in
Game Development more directly?
Saturday, November 3, 12
Slide 13
Slide 13 text
Three Paths in Game
Development
• Game Libraries
• Using Existing Engines
• Building a Game Engine
Saturday, November 3, 12
Slide 14
Slide 14 text
Game Libraries
• Abstracts Low-Level Game Internals
• Input Collection
• Rendering Interface
• Iteration Timing
• Minimum Level of Code to Start
Saturday, November 3, 12
Slide 15
Slide 15 text
Minimum Requirements
• Graphics Renderer
• Game Loop Handler
• Input Handler
Saturday, November 3, 12
Slide 16
Slide 16 text
Ruby Game Libraries
• Most Built on 1 of 2 Projects
• Rubygame
• Gosu
• Several Derivatives/Extensions
Saturday, November 3, 12
Slide 17
Slide 17 text
Rubygame
• http://github.com/rubygame/rubygame
• Pure Ruby Implementation
• Uses SDL for Rendering Interface
• Not Actively Maintained
Saturday, November 3, 12
Slide 18
Slide 18 text
Gosu
• https://github.com/jlnr/gosu/
• C++ Library
• Ruby wrapper uses SWIG to call C++
• Uses OpenGL for Rendering
• Actively Maintained
Saturday, November 3, 12
Slide 19
Slide 19 text
Anatomy of a Game
with Gosu
• Create subclass of Gosu::Window
• Create ONE instance of this subclass
• Subclass implements 3 Methods
Saturday, November 3, 12
Slide 20
Slide 20 text
Gosu Barebones
Saturday, November 3, 12
Slide 21
Slide 21 text
Blackjack
• https://github.com/cadwallion/blackjack
• Simple card game
• Built using Gosu
Saturday, November 3, 12
Slide 22
Slide 22 text
Blackjack - Initialize
Saturday, November 3, 12
Slide 23
Slide 23 text
Resource Manager
• Creation/Deletion of Resource Handles
• Removes binding to Game Loop
• Game State Manager Communication
Saturday, November 3, 12
Slide 24
Slide 24 text
Saturday, November 3, 12
Slide 25
Slide 25 text
Saturday, November 3, 12
Slide 26
Slide 26 text
Saturday, November 3, 12
Slide 27
Slide 27 text
Game State Manager
• Contains Game Logic
• Contains UI Elements
• Contains Events / Input Handler
• Relays Information to Game Loop
Saturday, November 3, 12
Slide 28
Slide 28 text
Resource Manager
Game State Manager
Primary Game Loop
Game State
Game Logic
Rendering
Input Handling
Game State
Game Logic
Rendering
Input Handling
Game State
Game Logic
Rendering
Input Handling
Saturday, November 3, 12
Slide 29
Slide 29 text
Entities
• Building Block of Game Objects
• Base class in traditional engines
• Any interaction player has is with Entities
Saturday, November 3, 12
Slide 30
Slide 30 text
Gamebox
• https://github.com/shawn42/gamebox/
• Built on top of Gosu
• Designed for Rapid Game Development
Saturday, November 3, 12
Slide 31
Slide 31 text
Gamebox Structure
• Gosu::Window becomes a Stage
• Entities are represented as Actor objects
• Game States are represented as Scenes
• StageManager coordinates Scenes
• Stagehands micromanage for Stage Manager
Saturday, November 3, 12
Game Libraries
• Great starting point for Developers
• Boilerplate is quickly due to complexity
• Requires not overly performant
Saturday, November 3, 12
Slide 34
Slide 34 text
Building Ruby Games
with Engines
Game Libraries are Game Engines with minimal
component implementation
Game Engines allow game developers to focus on
game-specific logic, not low-level internals
Saturday, November 3, 12
Slide 35
Slide 35 text
Slick2D
• Java Library -- Accessible via JRuby
• Mature, Active Development
• Easily package-able
Saturday, November 3, 12
Slide 36
Slide 36 text
Breakout
• https://github.com/cadwallion/breakout
• Classic Breakout game
• Extension of Slick2D Pong by @peterc
• http://www.rubyinside.com/video-game-
ruby-tutorial-5726.html
Saturday, November 3, 12
Slide 37
Slide 37 text
New Components
• Breakout has multiple levels
• Physics calculations
• More intense rendering through animation
Saturday, November 3, 12
Slide 38
Slide 38 text
Breakout Tech
• GameContainer vs Game
• Input Handler is part of GameContainer
• Direct Access to Renderer Object
• Direct Access to GameContainer
• Implicit Window context for Images
More Exposure to Engine Internals
Saturday, November 3, 12
Slide 39
Slide 39 text
jMonkeyEngine
• Popular 3D Game Engine
• Written in Java
• Closer to Traditional Game Engine
Saturday, November 3, 12
Building Game Engines
with Ruby
• Full-featured Game Component Library
• Handles non-game-specific work
• Provides hooks for game code
What is a Game Engine?
Saturday, November 3, 12
Slide 42
Slide 42 text
Components of a
Game Engine
Engine Setup
Game Loop
Game States
Input Handler
Resource Manager
Rendering
Physics
Networking Sound System
Scripting System
Entities Hardware
Saturday, November 3, 12
Slide 43
Slide 43 text
Ruby Engine Challenges
• Concurrency
• Cross-Platform Support
• Library Interfacing Support
• Memory Management / Performance
• Code Compilation / Obfuscation
Saturday, November 3, 12
Slide 44
Slide 44 text
Concurrency
Certain components need to be non-blocking.
Without a proper concurrency system, bad things
like the networking blocking the renderer, or the
renderer blocking input handling will occur.
Saturday, November 3, 12
Slide 45
Slide 45 text
ZOMGTHREADS?!
• Spin up some threads
• Offload Rendering to Thread
• GameLoop negotiates Interactions
Saturday, November 3, 12
Slide 46
Slide 46 text
GIL
• GIL = Bad News Bears for Games
• Ruby Implementations without a GIL
• JRuby
• Rubinius
• MacRuby
Saturday, November 3, 12
Slide 47
Slide 47 text
Cross-Platform Support
with Ruby
• Great Linux/OSX support
• Mediocre Windows Support
• Lots of gotchas spread across core/stdlib
• JRuby/JVM helps mitigate these issues
Saturday, November 3, 12
Slide 48
Slide 48 text
Cross Platform Idea
Could we pick our Ruby Implementation based on the
operating system the game is being developed on?
Saturday, November 3, 12
Slide 49
Slide 49 text
OS Input Interfacing
• Ruby + Mouse Events =
• C/C++ Extensions
• JRuby can interface
• MacRuby can too!
Saturday, November 3, 12
Slide 50
Slide 50 text
Memory Management
MRI 1.9 GC is not well optimized for
long-running processes and rapid object
creation/deletion
JRuby has great tools for performance
tuning the runtime and GC
Saturday, November 3, 12
Slide 51
Slide 51 text
Code Obfuscation
Fun Game + Visible Code = Free Game
Saturday, November 3, 12
Slide 52
Slide 52 text
Code Obfuscation
JRuby can MITIGATE this concern by
packaging via Warbler...
...but this does not ELIMINATE the potential.
Saturday, November 3, 12
Slide 53
Slide 53 text
Ruby as a Scripting
Language
Using Ruby as the high-level scripting language
provides the benefits of Ruby while allowing
other languages to handle the lower-level mechanics.
Saturday, November 3, 12
Slide 54
Slide 54 text
Benefits
• DSL Creation
• Ruby Features
• Easy for Game Modders
• Code Obfuscation no longer a concern
• Logic Mixins, Game State Injections, etc
Saturday, November 3, 12
Slide 55
Slide 55 text
Challenges
• Performance
• Memory Footprint
• Windows Support for Ruby still sucks
Saturday, November 3, 12
Slide 56
Slide 56 text
mruby
• Lightweight Footprint (~500K)
• Alternative to Lua
• Embeddable Implementation
• Subset of Ruby Implementation
• Easily bridged to C/C++
• No assumption of an OS
Saturday, November 3, 12
Slide 57
Slide 57 text
mruby is NOT a full Ruby
implementation
Saturday, November 3, 12