Using Lua with C++

Started by
1 comment, last by Drakkcon 17 years, 6 months ago
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Advertisement
I don't know if you've seen it already, or will find it useful, but a friend of mine up north wrote this...

http://www.gamedev.net/reference/articles/article1932.asp
---Josie NutterTechnical DesignerSnowblind Studios
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--GraphicsScreenWidth = 640;ScreenHeight = 480;ScreenDepth = 32;Fullscreen = false;--DebugEnableConsole = 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();        luaopen_base(luaVm);        int error = luaL_dofile(luaVm, "./data/Initialization.lua");        if(error)    {        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);        lua_close(luaVm);        //set up our screen    SDL_Init(SDL_INIT_VIDEO);        SDL_Surface* screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight,                   ScreenDepth,                                            SDL_HWSURFACE | SDL_DOUBLEBUF);        SDL_Delay(2000);        SDL_Quit();                             }




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):

--StartupScript.luaInitializeGame("Initialization.lua")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 offsetplayerStartPos = {tileX = 7, tileY = 10, offsetX = 0, offsetY = 0}SpawnActor("Jim", "Npcs", playerStartPos)SetPcControl("Jim")BeginUpdateLoop()--eof


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

This topic is closed to new replies.

Advertisement