Calling lua functions from C++ with luabind?

Started by
12 comments, last by CodaKiller 14 years, 11 months ago
I've been trying to call a lua function from C++ so I can run event functions but I can't get it to work. I have to send a reference to a window to the function so that it will know which window is being closed. Here is what I tried:

luabind::call_function<E_WINDOW*>(g_Lua, "On_WindowClose", g_Windows);







E_WINDOW is my window class and it's been added to luabind, g_Lua is the global lua state, "On_WindowClose" is the name of the event function I'm trying to call and g_Windows is a vector of all windows that have been created. Here is the lua code:

// Create window.
	WindowDesc = Window_Desc()
	WindowDesc.name = "Ether"
	WindowDesc.icon = "Ether.ico"
	WindowDesc.left = 0
	WindowDesc.top = 0
	WindowDesc.width = 1024
	WindowDesc.height = 768
	WindowDesc.close = true
	WindowDesc.maximize = false
	WindowDesc.minimize = true
	WindowDesc.sizeable = false
	GameWindow = Window( WindowDesc )

//Close window event function.

function On_WindowClose( window )
	if (window == GameWindow) then
		Exit()
	end
end







Here is the error I get:
Quote: First-chance exception at 0x764cf328 in Ether.exe: Microsoft C++ exception: luabind::error at memory location 0x0017f6b0.. The program '[7520] Ether.exe: Native' has exited with code 0 (0x0).
And lastly this is the function it crashes on in the luabind header:

					// pops the return values from the function call
					stack_pop pop(L, lua_gettop(L) - top + m_params);





My guess as to whats going wrong is that I can't send pointers to classes like that or something. [Edited by - CodaKiller on May 16, 2009 11:09:06 AM]
Remember Codeka is my alternate account, just remember that!
Advertisement
This guy had the same problem but nobody answered his question.
Remember Codeka is my alternate account, just remember that!
Whats the error on the lua stack when you execute this code?

Try this:
try {    luabind::call_function<E_WINDOW*>(g_Lua, "On_WindowClose", g_Windows);} catch (luabind::error& e){    std::string error = lua_tostring( e.state(), -1 );    std::cout << error << "\n";}


and tell us what it prints.

[size=1]Visit my website, rawrrawr.com

Quote:Original post by bobofjoe
Whats the error on the lua stack when you execute this code?

Try this:
try {    luabind::call_function<E_WINDOW*>(g_Lua, "On_WindowClose", g_Windows);} catch (luabind::error& e){    std::string error = lua_tostring( e.state(), -1 );    std::cout << error << "\n";}


and tell us what it prints.


It does not catch the error and crashes at the same line as it did.

[Edited by - CodaKiller on May 17, 2009 10:31:29 AM]
Remember Codeka is my alternate account, just remember that!
Hit "continue" to allow the first chance exception to propagate to your exception handler.

[size=1]Visit my website, rawrrawr.com

Quote:Original post by bobofjoe
Hit "continue" to allow the first chance exception to propagate to your exception handler.


Alright the error returned is "No such operator defined" but I know I defined the class. Here is the source for the class:

class E_WINDOW{public:	E_WINDOW( E_WINDOW_DESC desc )	{	    WNDCLASSEX wcex;		ZeroMemory( &wcex, sizeof( wcex ) );	    wcex.cbSize = sizeof( WNDCLASSEX );	    wcex.lpfnWndProc = WndProc;		wcex.hIcon = (HICON)LoadImage( NULL, desc.icon.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE );	    wcex.hCursor = LoadCursor( NULL, IDC_ARROW );	    wcex.lpszClassName = desc.name.c_str();	    RegisterClassEx( &wcex );		DWORD style = (WS_SYSMENU * (desc.minimize | desc.maximize | desc.close)) | (WS_MINIMIZEBOX * desc.minimize) | (WS_MAXIMIZEBOX * desc.maximize) | (WS_SIZEBOX * desc.sizeable) | WS_EX_NOPARENTNOTIFY;	    hWnd = CreateWindow( desc.name.c_str(), desc.name.c_str(), style, desc.left, desc.top, desc.width, desc.height, NULL, NULL, NULL, NULL );		UnregisterClass( desc.name.c_str(), NULL );	    ShowWindow( hWnd, SW_SHOW );		SwapChain = NULL;		RenderTargetView = NULL;		DepthStencilView = NULL;		RECT WindowRect;		GetWindowRect( hWnd, &WindowRect );		DXGI_SWAP_CHAIN_DESC			sd;		ZeroMemory( &sd, sizeof( sd ) );		sd.BufferCount = 1;		sd.BufferDesc.Width = WindowRect.right - WindowRect.left;		sd.BufferDesc.Height = WindowRect.bottom - WindowRect.top;		sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;		sd.BufferDesc.RefreshRate.Numerator = 60;		sd.BufferDesc.RefreshRate.Denominator = 1;		sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;		sd.OutputWindow = hWnd;		sd.SampleDesc.Count = 1;		sd.SampleDesc.Quality = 0;		sd.Windowed = true;		sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;		IDXGIFactory * pFactory;		CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory) );		pFactory->CreateSwapChain( g_d3dDevice, &sd, &SwapChain);		pFactory->Release();		ID3D10Texture2D* pBackBuffer;		SwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), ( LPVOID* )&pBackBuffer );		g_d3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &RenderTargetView );		pBackBuffer->Release();		ID3D10Texture2D* DepthStencil;		D3D10_TEXTURE2D_DESC DepthStencilDesc;		DepthStencilDesc.Width = WindowRect.right - WindowRect.left;		DepthStencilDesc.Height = WindowRect.bottom - WindowRect.top;		DepthStencilDesc.MipLevels = 1;		DepthStencilDesc.ArraySize = 1;		DepthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT;		DepthStencilDesc.SampleDesc.Count = 1;		DepthStencilDesc.SampleDesc.Quality = 0;		DepthStencilDesc.Usage = D3D10_USAGE_DEFAULT;		DepthStencilDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;		DepthStencilDesc.CPUAccessFlags = 0;		DepthStencilDesc.MiscFlags = 0;		g_d3dDevice->CreateTexture2D( &DepthStencilDesc, NULL, &DepthStencil );		g_d3dDevice->CreateDepthStencilView( DepthStencil, NULL, &DepthStencilView );		DepthStencil->Release();		g_Windows.push_back( this );	}	void Release()	{		SwapChain->Release();		RenderTargetView->Release();		DepthStencilView->Release();	}	HWND hWnd;	IDXGISwapChain*						SwapChain;	ID3D10RenderTargetView*				RenderTargetView;	ID3D10DepthStencilView*				DepthStencilView;};


And here is where I define it in luabind:
	luabind::module(g_Lua)	[	    luabind::class_<E_WINDOW>("Window")	        .def(luabind::constructor<E_WINDOW_DESC>())	];
Remember Codeka is my alternate account, just remember that!
Your window does not define operator==.

See: Here

[size=1]Visit my website, rawrrawr.com

Quote:Original post by bobofjoe
Your window does not define operator==.

See: Here


OK that makes sense but I don't understand how to get it to work.

I added this to my class:

bool luabind::operator==(E_WINDOW);


and this is how I define it:

	luabind::module(g_Lua)	[	    luabind::class_<E_WINDOW>("Window")	        .def(luabind::constructor<E_WINDOW_DESC>())		.def(luabind::self == luabind::other<E_WINDOW>())	];


But it's not working and I receive this error:

Quote:
1>.\Main.cpp(970) : error C2838: '==' : illegal qualified name in member declaration


I don't really understand how it works but I know what it's for, can you give me an idea how to use it?

[Edited by - CodaKiller on May 17, 2009 5:38:03 PM]
Remember Codeka is my alternate account, just remember that!
You're not declaring the operator correctly in your class. Its not luabind::operator==, its just operator==.

[size=1]Visit my website, rawrrawr.com

Quote:Original post by bobofjoe
You're not declaring the operator correctly in your class. Its not luabind::operator==, its just operator==.


When I do this:

bool operator==(E_WINDOW);


I get this error:

Quote:
1>Main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall E_WINDOW::operator==(class E_WINDOW)" (??8E_WINDOW@@QAE_NV0@@Z) referenced in function "public: static void __cdecl luabind::operators::eq::apply<class E_WINDOW &,class E_WINDOW,struct luabind::detail::null_type>::execute(struct lua_State *,class E_WINDOW &,class E_WINDOW)" (?execute@?$apply@AAVE_WINDOW@@V1@Unull_type@detail@luabind@@@eq@operators@luabind@@SAXPAUlua_State@@AAVE_WINDOW@@V6@@Z)
Remember Codeka is my alternate account, just remember that!

This topic is closed to new replies.

Advertisement