Jump to content
  • Advertisement
Sign in to follow this  
Eddy999999

Scripting Engine Implementation Tutorial

This topic is 3873 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
Advertisement
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
Nevermind, I suppose I should have looked at the comments on the tutorial before asking here. I had to change luaopen_io (lua); to luaL_openlibs (lua);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!