Home » Community » Forums » Scripting Languages and Game Mods » Using Lua with C++
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Using Lua with C++
Post New Topic  Post Reply 
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.

 User Rating: 1065   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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 Nutter
Technical Designer
Snowblind Studios

 User Rating: 1029   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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); // 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.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.

 User Rating: 1334   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: