Using lambda expressions with CEGUI

C++ lambdas is just another name for anonymous methods, which can be defined within another method. It’s particularly useful for callback methods.

With CEGUI, it’s easy to define a callback for a pushbutton like this:

// Exit button
	CEGUI::PushButton* button = static_cast<CEGUI::PushButton*>(m_WindowManager->createWindow("TaharezLook/Button" ));
	button->setPosition(CEGUI::UVector2(cegui_reldim(0.40f), cegui_reldim( 0.50f)));
	button->setSize( buttonSize );

	auto handleExitClicked = [&] ( const CEGUI::EventArgs & args ) -> bool
		// Send message

		return true;

	button->subscribeEvent( button->EventMouseClick, handleExitClicked );


This is just a simple example on how to use lambdas with CEGUI.

About Kent Fagerjord

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.
This entry was posted in coding and tagged , , . Bookmark the permalink.