Sign in to follow this  

Scripting Engine Implementation Tutorial

This topic is 3597 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

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.

Share this post


Link to post
Share on other sites
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:


//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?

Share this post


Link to post
Share on other sites

This topic is 3597 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this