Page 1 of 1

Subscribing Lua functions to buttons

Posted: Thu Oct 14, 2010 17:37
by aaron1a12
Is there a way of subscribing Lua functions to events for button not created in the LUA script but rather in the actual UI .layout?

You see I tried running the following code (from the wiki) in the LUA script but I kept getting Runtime errors:

Code: Select all

CEGUI::PushButton* pb = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button","lua_powered_button");
pb->setSize(CEGUI::Size(0.1f,0.1f));
pb->setPosition(CEGUI::Point(0.1f,0.1f));
pb->subscribeScriptedEvent("Clicked","luabtn_clicked");
CEGUI::System::getSingleton().getGUISheet()->addChildWindow(pb);

Re: Subscribing Lua functions to buttons

Posted: Sat Oct 16, 2010 23:43
by aaron1a12
Anyone? :?:

Re: Subscribing Lua functions to buttons

Posted: Sun Oct 17, 2010 20:51
by Turtle
Hi,

Did you do an executeScriptFile above this to load your script into memory, and do you know for sure that it loaded?

Also, when in doubt, I strip my event function down to the point that it's empty and then start adding the code back to it once I'm sure it's running.

Cheers.

Re: Subscribing Lua functions to buttons

Posted: Mon Oct 18, 2010 19:50
by Jamarr
You described your problem well, and did post relevant code so that is helpful; what wiki article are you referencing? Also, you should always post the error message (if it's an exception, catch and post the exception message) as well as the associated debug callstack. Basically, the more information you provide up front, the easier it will be for others to help you and the quicker your question will be answered. In case have not already seen this post, you will be doing yourself a favor by reading this ;)

Unfortunately I am not familiar enough with the CEGUI's use of Lua, and without the error message I cannot even begin to look into the causes of it. All I can say, with the information you've given, is that the code you posted looks fine. And I believe that you can subscribe Lua functions to events for windows created in layouts - assuming the layout is loaded before the script executes that code.

Re: Subscribing Lua functions to buttons

Posted: Mon Oct 18, 2010 23:34
by aaron1a12
Okay, I tried catching an exception and I got something:
CEGUI::ScriptExecption in file
..\..\..\..\cegui\src\ScriptingModules\LuaScriptModule\CEGUILua.cpp(579) :
Unable to execute Lua script file: 'Scripts/cegui/vita.lua'

[string "Scripts/cegui/vita.lua"]: 12:'<name>' expected near ':'


I was simply following the tutorial on this page:
http://www.cegui.org.uk/wiki/index.php/Handling_Events_from_Lua

This is my Lua:

Code: Select all

-----------------------------------------
-- Main Menu Script
-----------------------------------------
local logger = CEGUI.Logger:getSingleton()
local system    = CEGUI.System:getSingleton()
local fontman   = CEGUI.FontManager:getSingleton()
local schememan = CEGUI.SchemeManager:getSingleton()

logger:logEvent( "UI initiated with LUA" );


CEGUI::PushButton* pb = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button","lua_powered_button");
pb->setSize(CEGUI::Size(0.1f,0.1f));
pb->setPosition(CEGUI::Point(0.1f,0.1f));
pb->subscribeScriptedEvent("Clicked","luabtn_clicked");
CEGUI::System::getSingleton().getGUISheet()->addChildWindow(pb);

function luabtn_clicked(e)
  local we = CEGUI.toWindowEventArgs(e)
  logger:logEvent( "Button Clicked" );
end

Re: Subscribing Lua functions to buttons

Posted: Tue Oct 19, 2010 10:24
by Turtle
Ahh, I see. The code from the Wiki is C++ code for creating Lua events and can't go into a Lua file as is - it would need to be modified to the Lua equivalent.

Offhand (ie: I haven't tested this), you might be able to do this:

Code: Select all

local pb = CEGUI.WindowManager:getSingleton().createWindow("TaharezLook/Button","lua_powered_button")
pb:setSize(CEGUI.Size(0.1,0.1))
pb:setPosition(CEGUI.Point(0.1,0.1))
pb:subscribeEvent("Clicked","luabtn_clicked")
CEGUI.System:getSingleton().getGUISheet()->addChildWindow(pb)


You might want to have a look at the demo8.lua file as an example and this page in the wiki http://gpwiki.org/index.php/Crazy_Eddie ... sing_CEGUI (under the section titled "Lua and CEGUI") has some examples further down.

Re: Subscribing Lua functions to buttons

Posted: Tue Oct 19, 2010 15:31
by aaron1a12
OMG! So I was using C++ in a Lua script? :oops: crazy

Re: Subscribing Lua functions to buttons

Posted: Tue Oct 19, 2010 16:49
by Jamarr
I should have realized that when you said:

I tried running the following code...in the LUA script


heh...completely skimmed over that ;)