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
--Graphics
ScreenWidth = 640;
ScreenHeight = 480;
ScreenDepth = 32;
Fullscreen = false;
--Debug
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();
luaopen_base(luaVm);
int error = luaL_dofile(luaVm, "./data/Initialization.lua");
if(error)
{
cout << lua_tostring(luaVm, -1);
lua_pop(luaVm, 1);
}
lua_getglobal(luaVm, "ScreenWidth");
lua_getglobal(luaVm, "ScreenHeight");
lua_getglobal(luaVm, "ScreenDepth");
lua_getglobal(luaVm, "Fullscreen");
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);
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.lua
InitializeGame("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 offset
playerStartPos = {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.
|