Designing a Component-based Architecture in Lua for Game Apps

app-forum-taiwan_01

Hi there, I’m Wei Ciao. This is the talk I presented in 2012 Game Game App Developers Conference Taipei in Taiwan. I’d like to share my passion for Lua and talk about some techniques we used in Jade Ninja and Bonnie’s Brunch 2.

app-forum-taiwan_02

Before becoming an iOS developer, I was a game programmer working for a company making MMO genre games. My primary skills and fondness in programming languages are C++ and Lua. As a game developer, I’ve dug deeply in Lua and developed a game framework based on it.

Here is the combination of programming languages utilized in my game apps:

  • C++: General abstract framework can be used in cross-platforms.
  • Objective-C: Handling iOS platform-specific details and OpenGL ES implementation.
  • Lua: Data description. User interfaces. Game logic. Anything else.

app-forum-taiwan_03

With Lua, I can easily describe a game object simply based on components. In the example above, the game object, Player, is composed of 3 components, which are:

  • Transform: In charge of translation, rotation and scaling in game world.
  • Quad: A simple quad drawn in 2D plane to represent the object.
  • BoundingBox: Collision detecting in box-shaped.

app-forum-taiwan_04

One object can comprise several components, however, not every component needs to be updated and/or rendered. Unlike traditional object-oriented architecture, in component-based architecture we can update and render the game objects accordingly.

app-forum-taiwan_05

What we called a game object, actually is nothing more than a container. Game object doesn’t need to be involved in how specific game components function. It merely contains and manages them in an abstract way and all tasks are assigned to its components. Powers are delegated to the game components.

app-forum-taiwan_06

For example, when a game object consists of Interpolator game component, we can interpolate a variable between the starting value and the ending value, and then apply it to image alpha or scaling. Interpolator will make your objects and interfaces look bouncing and vivid if used properly.

app-forum-taiwan_07

As for memory management, Lua takes no responsibility. All dynamically memory-allocated objects are created in C++ and passed-by-reference for Lua to utilize. Lua only gets pointers. Lua could perform complex memory management tasks, but a clear separation between C++ and Lua helps making memory management less error-prone.

Lua is lightweight, robust and cross-platform. Programming in Lua can save you a lot of compile-time for source files, and the coding process has a quick turnover rate. The higher turnover rate is, the more iterations you can get. In the process of game development, more iterations help increasing work efficiency and improving game quality.

app-forum-taiwan_08

If you are wondering how it works in game apps, you may check out my latest game Bonnie’s Brunch 2 and upcoming Jade Ninja.

Live well, happy coding and make great games! 😀

Download full slideshttps://speakerdeck.com/halflucifer/designing-a-component-based-architecture-in-lua-for-game-apps

Tutorial: Introduction to Component Based Architecture in Games

2 Replies to “Designing a Component-based Architecture in Lua for Game Apps”

  1. Very good article, i’ve read it on Gamasutra and i liked very much.

    I have few question about your Memory Menagement beetween Lua and C++.
    From your Memory Management slide i’ve assumed that in your system the GameObject is only in the Lua side, and you use it like “aggregator” beetween components, but i haven’t understand if your components are defined in C++ code (and controlled by Lua using a pointer to it) or are directly defined in Lua and the memory allocated in the C++ side.

    Again, great work!!

    1. Hi CBS,

      I built up all components in Lua while C++ provided only the OS-level and low-level functionalities such as texture mapping, screen rendering and so on. However, all graphic objects are allocated and deallocated in C++. Game logic in Lua should never access memory directly.

Leave a Reply

Your email address will not be published. Required fields are marked *