$30 off During Our Annual Pro Sale. View Details »

Game Development and Ruby

Game Development and Ruby

A presentation on opportunities of Ruby in the Game Development industry, including building games in Ruby, building game engines with Ruby, and using Ruby as a Scripting Languages for non-ruby Game Engines. Presented at RubyConf 2012.

Andrew Nordman

November 03, 2012
Tweet

More Decks by Andrew Nordman

Other Decks in Programming

Transcript

  1. Game Development
    and Ruby
    Saturday, November 3, 12

    View Slide

  2. Andrew “Cad” Nordman
    @cadwallion
    cadwallion
    Saturday, November 3, 12

    View Slide

  3. DAY JERB
    Saturday, November 3, 12

    View Slide

  4. History
    Saturday, November 3, 12

    View Slide

  5. UnrealEd
    • Level Design
    • Event Trigger System
    • Particle Effect Manager
    • One-click Playtesting
    Saturday, November 3, 12

    View Slide

  6. UnrealScript
    • Inspired by Java
    • Rather Verbose
    • Awkward OO Principles
    • Lacking in Language Functionality
    Saturday, November 3, 12

    View Slide

  7. 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

    View Slide

  8. Unreal Development
    Kit (UDK)
    • UnrealEd 3.0
    • Designed for Mods & Full-on games
    • Extensive UnrealEd Improvements
    • UnrealScript Mostly Unchanged
    Saturday, November 3, 12

    View Slide

  9. UnrealRb
    • CoffeeScript-inspired language compiler
    • Written in Ruby
    • Ruby code in
    • UnrealScript out
    Saturday, November 3, 12

    View Slide

  10. This was a terrible idea.
    Saturday, November 3, 12

    View Slide

  11. UnrealRb Problems
    • Poor Performance
    • Interfacing Bugs with UDK
    • Still bound by UnrealScript structure
    • Devolved into primitive DSL
    Saturday, November 3, 12

    View Slide

  12. However...
    What if Ruby could be used in
    Game Development more directly?
    Saturday, November 3, 12

    View Slide

  13. Three Paths in Game
    Development
    • Game Libraries
    • Using Existing Engines
    • Building a Game Engine
    Saturday, November 3, 12

    View Slide

  14. Game Libraries
    • Abstracts Low-Level Game Internals
    • Input Collection
    • Rendering Interface
    • Iteration Timing
    • Minimum Level of Code to Start
    Saturday, November 3, 12

    View Slide

  15. Minimum Requirements
    • Graphics Renderer
    • Game Loop Handler
    • Input Handler
    Saturday, November 3, 12

    View Slide

  16. Ruby Game Libraries
    • Most Built on 1 of 2 Projects
    • Rubygame
    • Gosu
    • Several Derivatives/Extensions
    Saturday, November 3, 12

    View Slide

  17. Rubygame
    • http://github.com/rubygame/rubygame
    • Pure Ruby Implementation
    • Uses SDL for Rendering Interface
    • Not Actively Maintained
    Saturday, November 3, 12

    View Slide

  18. 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

    View Slide

  19. 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

    View Slide

  20. Gosu Barebones
    Saturday, November 3, 12

    View Slide

  21. Blackjack
    • https://github.com/cadwallion/blackjack
    • Simple card game
    • Built using Gosu
    Saturday, November 3, 12

    View Slide

  22. Blackjack - Initialize
    Saturday, November 3, 12

    View Slide

  23. Resource Manager
    • Creation/Deletion of Resource Handles
    • Removes binding to Game Loop
    • Game State Manager Communication
    Saturday, November 3, 12

    View Slide

  24. Saturday, November 3, 12

    View Slide

  25. Saturday, November 3, 12

    View Slide

  26. Saturday, November 3, 12

    View Slide

  27. Game State Manager
    • Contains Game Logic
    • Contains UI Elements
    • Contains Events / Input Handler
    • Relays Information to Game Loop
    Saturday, November 3, 12

    View Slide

  28. 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

    View Slide

  29. Entities
    • Building Block of Game Objects
    • Base class in traditional engines
    • Any interaction player has is with Entities
    Saturday, November 3, 12

    View Slide

  30. Gamebox
    • https://github.com/shawn42/gamebox/
    • Built on top of Gosu
    • Designed for Rapid Game Development
    Saturday, November 3, 12

    View Slide

  31. 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

    View Slide

  32. Gamebox Stagehands
    • Resource Manager
    • Input Manager
    • Physics Manager
    • Configuration Manager
    Saturday, November 3, 12

    View Slide

  33. Game Libraries
    • Great starting point for Developers
    • Boilerplate is quickly due to complexity
    • Requires not overly performant
    Saturday, November 3, 12

    View Slide

  34. 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

    View Slide

  35. Slick2D
    • Java Library -- Accessible via JRuby
    • Mature, Active Development
    • Easily package-able
    Saturday, November 3, 12

    View Slide

  36. 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

    View Slide

  37. New Components
    • Breakout has multiple levels
    • Physics calculations
    • More intense rendering through animation
    Saturday, November 3, 12

    View Slide

  38. 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

    View Slide

  39. jMonkeyEngine
    • Popular 3D Game Engine
    • Written in Java
    • Closer to Traditional Game Engine
    Saturday, November 3, 12

    View Slide

  40. jMonkeyEngine
    Component
    • SimpleApplication
    • simpleInit()
    • simpleUpdate()
    • simpleRender()
    • AppStates
    • Multithreading via Thread Pools
    Saturday, November 3, 12

    View Slide

  41. 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

    View Slide

  42. 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

    View Slide

  43. Ruby Engine Challenges
    • Concurrency
    • Cross-Platform Support
    • Library Interfacing Support
    • Memory Management / Performance
    • Code Compilation / Obfuscation
    Saturday, November 3, 12

    View Slide

  44. 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

    View Slide

  45. ZOMGTHREADS?!
    • Spin up some threads
    • Offload Rendering to Thread
    • GameLoop negotiates Interactions
    Saturday, November 3, 12

    View Slide

  46. GIL
    • GIL = Bad News Bears for Games
    • Ruby Implementations without a GIL
    • JRuby
    • Rubinius
    • MacRuby
    Saturday, November 3, 12

    View Slide

  47. 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

    View Slide

  48. Cross Platform Idea
    Could we pick our Ruby Implementation based on the
    operating system the game is being developed on?
    Saturday, November 3, 12

    View Slide

  49. OS Input Interfacing
    • Ruby + Mouse Events =
    • C/C++ Extensions
    • JRuby can interface
    • MacRuby can too!
    Saturday, November 3, 12

    View Slide

  50. 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

    View Slide

  51. Code Obfuscation
    Fun Game + Visible Code = Free Game
    Saturday, November 3, 12

    View Slide

  52. Code Obfuscation
    JRuby can MITIGATE this concern by
    packaging via Warbler...
    ...but this does not ELIMINATE the potential.
    Saturday, November 3, 12

    View Slide

  53. 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

    View Slide

  54. 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

    View Slide

  55. Challenges
    • Performance
    • Memory Footprint
    • Windows Support for Ruby still sucks
    Saturday, November 3, 12

    View Slide

  56. 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

    View Slide

  57. mruby is NOT a full Ruby
    implementation
    Saturday, November 3, 12

    View Slide

  58. https://github.com/iij/mruby
    • Adds Kernel#require
    • Adds IO
    • Adds Socket, TCPSocket, UDPSocket
    • Extends String
    Saturday, November 3, 12

    View Slide

  59. mrbgems
    • https://github.com/mruby/mruby/pull/479
    • Library manager for C/Ruby extensions to
    mruby
    • mrbgems != rubygems
    Saturday, November 3, 12

    View Slide

  60. Engine Scripting Examples
    Saturday, November 3, 12

    View Slide

  61. Ruby Engine Initiative
    • C++ Internals
    • OpenGL Rendering
    • mruby embedded engine
    • Development starts next week
    Saturday, November 3, 12

    View Slide

  62. Thank You
    Saturday, November 3, 12

    View Slide