Skip to main content
gamedev.net
🔒 Locked

Calling Lua Functions in Classes (LuaPlus)

Started by Ludi83 Apr 6, 2005 at 5:33 PM 0 replies 2.4k views
Original Post
Ludi83
Ludi83
Good evening to all game programmers out there, I am currently trying to get in touch with LuaPlus. As I am german, and the rest of the world just only speaks english, my first try of learning Lua is to write a language wrapper. I know, I could even use something like XML, perhaps this makes more sense then the use of Lua, but I don't want [grin] So, this is my Lua source:

-- 
-- English Language File
-- 
function language( code )

	if ( code ==  "error_net" ) then 
	return "Network Error" 

	elseif ( code == "player_health" ) then 
	return "Health"

	end
end

This is the way to call LuaPlus functions in C++, as explained in the documentation, works fine. [totally]

	LuaPlus::LuaState *luaState = LuaPlus::LuaState::Create();
	luaState->DoFile("english.lua");
	LuaPlus::LuaFunction<const char*> Lang = luaState->GetGlobal("language");

	printf("%s", Lang("error-net"));

	LuaPlus::LuaState::Destroy(luaState);

Here is my little class I've written. It only has a constructor, a desturctor and a method called Get, which returns a string. The (private) method Language should be accessable by all the class methods, but I have no clue how to do that, the code I have written produces a compiler error [tears]. I even thougt to bind the function everytime in the Get method, but if the class should grow, Language has to be bind in every method, that uses it.

class C_Language {
	
	public:
		
		std::string Get(const char *pcCode);
		C_Language(std::string cFile);
		~C_Language();

	private:

		LuaPlus::LuaState *luaState;
		LuaPlus::LuaObject *luaObject;
		LuaPlus::LuaFunction<const char*> *Language;
};

C_Language::C_Language(std::string cFile) {

	LuaPlus::LuaState *luaState = LuaPlus::LuaState::Create();
	luaState->DoFile(cFile.c_str());
	LuaPlus::LuaFunction<const char*> Language = luaState->GetGlobal("language");
}

C_Language::~C_Language() {

	LuaPlus::LuaState::Destroy(luaState);
}

std::string C_Language::Get(const char *pcCode) {

	std::string cValue;
	return cValue.append(Language(pcCode));
}

Has anyone an idea how to solve that? Greetings
Ludi83
Ludi83
Has nobody any ideas? Or is it to C++ specific?

Topic Locked

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

Sign in to reply to this topic.