Jump to content
  • Advertisement
Sign in to follow this  
sp00n

luabind with singletons

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

After _2 DAYS_ of tearing my hair out, trying to get luabind to compile and integrate with my engine, I've finally got it compiling and running. One thing I'm not sure how to do though, I understand how to expose a class to luabind so that a c++ class can be created from lua, but how do i inform lua of classes created already by the engine itself? For instance, I have a singleton GFX class, called cGFX, which has a method called ToggleFullScreen(). I have made luabind aware of this using this code:
void cScriptEngine::BindClasses()
{

	luabind::module(luaVM)
	[
		class_<cGFX>("GFX")
			.def("toggle_fs",	&cGFX::ToggleFullScreen)
			.def("set_mode",	&cGFX::SetMode)
			.def("width",		&cGFX::Width)
			.def("height",		&cGFX::Height),

		class_<cGUI>("GUI")
			.def("print",		&cGUI::ConsolePrint),

		class_<cApp>("App")
			.def("quit",		&cApp::Kill)
	];
}
This works fine, but how do i then point lua to the instance of cGFX? Or is it possible to export class methods from cpp to luabind as functions, so that from lua I can just call "toggle_fs()"? Thanks sp00n

Share this post


Link to post
Share on other sites
Advertisement
Sorted it (thanks to the luabind_usersmail list! thanks guys!)

the solution, if anyone is interested, is as follows:

Export static functions returning pointers to the singletons:

uabind::module(luaVM)
[
class_<cGFX>("GFX")
.def("toggle_fs", &cGFX::ToggleFullScreen)
.def("set_mode", &cGFX::SetMode)
.def("width", &cGFX::Width)
.def("height", &cGFX::Height),
def("getGFX", &cGFX::GetSingletonPtr),

class_<cGUI>("GUI")
.def("print", &cGUI::ConsolePrint),
def("getGUI", &cGUI::GetSingletonPtr),

class_<cKernel>("Kernel")
.def("quit", &cKernel::KillAllTasks),
def("getKernel", &cKernel::GetSingletonPtr)
];


then, the functions are accessible from lua like this, for example:

getGFX():toggle_fs();

one more question though, the function decribed above, cGUI::ConsolePrint, outputs a line of text to my in-game console. If I execute the following from lua, it works:

getGUI():print("hello world!");

but what is the Lua code to re-assign lua's print() function so that my console print function is called instead?

I expected this would work:

print = getGUI():print;

but no such luck. I'm sure its obvious but I cant figure it out :)

Any Ideas?

Thanks, sp00n

Share this post


Link to post
Share on other sites
ok slightly less obvious than it first seemed :)

i thought this would work:

function print(...)
local out=tostring(arg[1])
for i=2,table.getn(arg) do
out=out.."\t"
out=out..tostring(arg)
end
getGUI():print(out)
end




but no. Help!

[Edited by - sp00n on October 10, 2005 11:30:30 AM]

Share this post


Link to post
Share on other sites
you could try doing

function newprint(...)
local out=tostring(arg[1])
for i=2,table.getn(arg) do
out=out.."\t"
out=out..tostring(arg)
end
getGUI():print(out)
end

_G["print"] = newprint;


or try

-- or replace 'print' with _G["print"]
print = function (...)
local out=tostring(arg[1])
for i=2,table.getn(arg) do
out=out.."\t"
out=out..tostring(arg)
end
getGUI():print(out)
end




(I admit, this could do the same as the print = newprint line you had earlier, but its worth a go *shrugs*)

Share this post


Link to post
Share on other sites
fixed it. :)

changing

for i=2,table.getn(arg) do

to

for i=2,arg["n"] do

fixed it. obviously I hadnt loaded the table library :)
changing the engine to load the table library fixed the original version :)

thanks for the help anyway!

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!