Skip to main content
gamedev.net
🔒 Locked

Problems with lua and classes

Started by ConorH 2 replies 450+ views
Original Post
ConorH
ConorH
When I try to make a class dealign with Lua, like so: class cScript { public: lua_State* Setup(); void DoFile(const char*); void Close(); private: lua_State* Lua; }; Passing the Lua var created in my setup function causes an execption. So to get round it, I have to have the Setup() func return the lua_state, so that the programmer can pass it on into each function they call, which is tedious and I'd prefer to have it self contained. I'm quite new so I've probably missed something fundamental out, but if anyone could tell me what Im doing wrong it would be great cheers
ApochPiQ
ApochPiQ
Post the code that has the problem - i.e. the code for Setup() as well as the code where you're passing the lua_State* into other functions. It sounds like there's a minor mistake in your code someplace, but there's no way we can find it without the code itself to look over.
ConorH
ConorH
Script.h
- - - - - -

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

class cScript
{
public:
void Setup();
void DoFile(const char*);
void Close();

private:
lua_State* Lua;
};
- - - - -


Script.cpp
- - - - -
#include "script.h"

void cScript::Setup()
{

//Create KittyKat
/* the Lua interpreter */
lua_State* Lua;

/* initialize Lua */
Lua = lua_open();

/* load Lua base libraries */
luaL_openlibs(Lua);

return L;
}

void cScript::DoFile(const char* file)
{
luaL_dofile(Lua,file);
}

void cScript::Close()
{
lua_close(Lua);
}
- - - - - -


Main.cpp
- - - - - -
#include "engine.h" //which includes script.h


int main( int argc, char* args[] )
{
//Start of program

cScript dog;
dog.Setup();
dog.DoFile("avg.lua");
dog.Close();
getchar();

return 1;
}
ApochPiQ
ApochPiQ
Your problem is in Setup(), here:

/* the Lua interpreter */lua_State* Lua;



This is creating a local variable called Lua inside of Setup(). When you call lua_open and save the lua_State*, you are storing it into this local variable. That local variable only exists (has a scope) inside of Setup().

However, when you access the Lua variable from, say, DoFile(), you are accessing the Lua member of the cScript class. This is not the same as the Lua variable that exists only inside of Setup().

What happens, then, is that the correct lua_State* data is created and returned, then kept in the local Lua inside of Setup(). As soon as Setup() returns, that variable goes out of scope, and the correct lua_State* is now lost. Instead, you have random garbage inside the cScript::Lua member, which is what will be used next time any of cScript's member functions are called, resulting in a crash.

This is commonly known as identifier shadowing and is generally something to avoid as much as possible, because it tends to create very nasty and subtle bugs like the one you've got here.


Remove the local Lua variable in Setup() and use the Lua member consistently in all of cScript, and you should be fine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.