Scripting Engine Implementation Tutorial
Does anyone know of any good tutorials for beginners for implementing a scripting language such as python or lua into a game using C++? My first choice would be lua, since that's the one I'm most familiar with.
There are actually a bunch of articles right here on Gamedev.net. Some of them are a bit dated, however most of them still contain relevant information.
For instance:
An Introduction to Lua
Also, see:
Scripting Languages
For instance:
An Introduction to Lua
Also, see:
Scripting Languages
After I posted this, I found this tutorial. It seems pretty recent, but I'm having some problems already. So far I have this code:
It compiles fine, but when I try to run it, I get the error "PANIC: Unprotected error in call to lua API (no calling environment)"
After a quick google search, I found this page, with someone who was having the same problem, and someone stated that "The luaopen_* functions (to open libraries) cannot be called directly, like a regular C function. They must be called through Lua, like a Lua function." I think this may be the solution to my problem, unfortunately, I have no idea how to call it through lua. Does this simply mean to call the function in my Lua script instead of in my C++ code?
//luainc.h#ifndef __LUA_INC_H__#define __LUA_INC_H__extern "C"{ #include "lualib/lua.h" #include "lualib/lauxlib.h" #include "lualib/lualib.h"}#endif // __LUA_INC_H__
//main.cpp#include "luainc.h"int main() { int iErr = 0; lua_State *lua = lua_open (); // Open Lua luaopen_io (lua); // Load io library if ((iErr = luaL_loadfile (lua, "test.lua")) == 0) { // Call main... if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0) { // Push the function name onto the stack lua_pushstring (lua, "helloWorld"); // Function is located in the Global Table lua_gettable (lua, LUA_GLOBALSINDEX); lua_pcall (lua, 0, 0, 0); } } lua_close (lua);}
-- Lua Hello World (test.lua)function helloWorld () io.write ("hello World")end
It compiles fine, but when I try to run it, I get the error "PANIC: Unprotected error in call to lua API (no calling environment)"
After a quick google search, I found this page, with someone who was having the same problem, and someone stated that "The luaopen_* functions (to open libraries) cannot be called directly, like a regular C function. They must be called through Lua, like a Lua function." I think this may be the solution to my problem, unfortunately, I have no idea how to call it through lua. Does this simply mean to call the function in my Lua script instead of in my C++ code?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement