Advertisement Jump to content
Sign in to follow this  

Using Lua with C++

This topic is 4502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've chosen Lua as the scripting language to use for my game engine. I've basically figured out how to interface C++ with Lua using Luabind, but I've not seen any tutorials about actually USING them together in games programming. Most tutorials barely get past "hello world". I've got a pretty good handle on Lua's syntax, but I could use a tutorial, or at least an example, of what and how Lua is used for in game programming. Can anyone point me in the right direction? Thanks in advance.

Share this post

Link to post
Share on other sites
I don't know if you've seen it already, or will find it useful, but a friend of mine up north wrote this...

Share this post

Link to post
Share on other sites
That depends on how much of the game you want to script.

I'm working on an RPG now, and here is an example of what I'm doing with Lua.

It's very easy to just use Lua to parse configuration files that you want to load, here's an example of one:

--This file defines settings important to the initialization of the application

ScreenWidth = 640;
ScreenHeight = 480;
ScreenDepth = 32;
Fullscreen = false;

EnableConsole = true;

and here would be how I use it(heavily simplified):

#include "SDL.h"

#include "Lua.hpp"

#include "iostream"

using namespace std;

int main(int, char**)
lua_State* luaVm = lua_open();


int error = luaL_dofile(luaVm, "./data/Initialization.lua");

cout << lua_tostring(luaVm, -1); // the error is on top of the stack
lua_pop(luaVm, 1);

//load graphical configuration properties
lua_getglobal(luaVm, "ScreenWidth");
lua_getglobal(luaVm, "ScreenHeight");
lua_getglobal(luaVm, "ScreenDepth");
lua_getglobal(luaVm, "Fullscreen");

//get them off the stack
int ScreenWidth = (int)lua_tonumber(luaVm, -4);
int ScreenHeight = (int)lua_tonumber(luaVm, -3);
int ScreenDepth = (int)lua_tonumber(luaVm, -2);
bool Fullscreen = lua_toboolean(luaVm, -1);


//set up our screen

SDL_Surface* screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight, ScreenDepth,



That's a very simple use of Lua in your app. But what if you wanted to create a basic game engine in C++, but you wanted people to be able to script the behavior of game objects and such. Here is what the final script will probably look like (for my game anyway):



UsingResource("Graphics.res", "Graphics")
UsingResource("Maps.res", "Maps")
UsingResource("NpcData.res", "Npcs")

LoadMap("PlayersHouse", "Maps")

--the first to numbers are the tile position, the last two are the pixel offset
playerStartPos = {tileX = 7, tileY = 10, offsetX = 0, offsetY = 0}
SpawnActor("Jim", "Npcs", playerStartPos)


If you haven't learned how to make functions like "LoadMap()" visible to Lua, I recommend this book.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!