Subscribing Lua functions to buttons

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

User avatar
aaron1a12
Not too shy to talk
Not too shy to talk
Posts: 29
Joined: Wed Sep 29, 2010 21:46
Location: Miami, Florida, USA

Subscribing Lua functions to buttons

Postby aaron1a12 » Thu Oct 14, 2010 17:37

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);
I hate BBCode pls enable <b>HTML</b> ;)

User avatar
aaron1a12
Not too shy to talk
Not too shy to talk
Posts: 29
Joined: Wed Sep 29, 2010 21:46
Location: Miami, Florida, USA

Re: Subscribing Lua functions to buttons

Postby aaron1a12 » Sat Oct 16, 2010 23:43

Anyone? :?:
I hate BBCode pls enable <b>HTML</b> ;)

User avatar
Turtle
Not too shy to talk
Not too shy to talk
Posts: 24
Joined: Tue Nov 08, 2005 22:36

Re: Subscribing Lua functions to buttons

Postby Turtle » Sun Oct 17, 2010 20:51

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.

Jamarr
CEGUI MVP
CEGUI MVP
Posts: 812
Joined: Tue Jun 03, 2008 23:59
Location: USA

Re: Subscribing Lua functions to buttons

Postby Jamarr » Mon Oct 18, 2010 19:50

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.
If somebody helps you by replying to your thread, upvote him/her as a thanks! Make sure to include your CEGUI.log and everything you tried when posting! And remember that we are not magicians!

User avatar
aaron1a12
Not too shy to talk
Not too shy to talk
Posts: 29
Joined: Wed Sep 29, 2010 21:46
Location: Miami, Florida, USA

Re: Subscribing Lua functions to buttons

Postby aaron1a12 » Mon Oct 18, 2010 23:34

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
I hate BBCode pls enable <b>HTML</b> ;)

User avatar
Turtle
Not too shy to talk
Not too shy to talk
Posts: 24
Joined: Tue Nov 08, 2005 22:36

Re: Subscribing Lua functions to buttons

Postby Turtle » Tue Oct 19, 2010 10:24

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.

User avatar
aaron1a12
Not too shy to talk
Not too shy to talk
Posts: 29
Joined: Wed Sep 29, 2010 21:46
Location: Miami, Florida, USA

Re: Subscribing Lua functions to buttons

Postby aaron1a12 » Tue Oct 19, 2010 15:31

OMG! So I was using C++ in a Lua script? :oops: crazy
I hate BBCode pls enable <b>HTML</b> ;)

Jamarr
CEGUI MVP
CEGUI MVP
Posts: 812
Joined: Tue Jun 03, 2008 23:59
Location: USA

Re: Subscribing Lua functions to buttons

Postby Jamarr » Tue Oct 19, 2010 16:49

I should have realized that when you said:

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


heh...completely skimmed over that ;)
If somebody helps you by replying to your thread, upvote him/her as a thanks! Make sure to include your CEGUI.log and everything you tried when posting! And remember that we are not magicians!


Return to “Help”

Who is online

Users browsing this forum: Google [Bot] and 17 guests