Adding LuaScriptModule to Sample FirstWindow

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Revision as of 15:38, 2 July 2005 by Acrion (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This applies to the CEGUI 0.3.0 Release.

Enable logging

Lua scripts get a lot of bugs. Without logging, you will never know why your application simply quits without stepping through every line of code.

In CEGuiSample.cpp

bool CEGuiSample::initialise()
{
     . . .
     default:
         // TODO: Throw exception or something!
+        return false;
-        break;
     }
+    CEGUI::Logger *logger = &CEGUI::Logger::getSingleton();
+    logger->setLoggingLevel( CEGUI::Informative );
+#if defined( __WIN32__ ) || defined( _WIN32 )
+    logger->setLogFilename( "C:\\Sample_FirstWindow.log", true );
+#elif defined(linux)
+    logger->setLogFilename( "~/Sample_FirstWindow.log", true );
+#else
+    logger->setLogFilename( "Sample_FirstWindow.log", true );
+#endif
     // execute the base application (which sets up the demo via 'this' and runs it.
    if (d_sampleApp->execute(this))

Create a Lua State

In Sample_FirstWindow.cpp

bool FirstWindowSample::initialiseSample()
{
    using namespace CEGUI;
    
+   // create a lua_state by using Lindquist's awesome LuaScriptModule
+   LuaScriptModule* lua = new LuaScriptModule();
+   // create the CEGUI namespace in the Lua environment
+   lua->createBindings();
    . . .
    // DefaultWindow which we will be using as the root of the displayed gui.
    root->addChildWindow(wnd);     
+   // call a script from our the datafiles
+   lua->executeScriptFile("../datafiles/scripts/sample.lua", "test");

Create a Lua Script

In the Samples\datafiles directory Create a scripts directory

Create sample.lua

-- get the GUI sheet
local sheet = CEGUI.WindowManager:getSingleton():getWindow("Root");

-- create a FrameWindow
local fw = CEGUI.WindowManager:getSingleton():createWindow ("TaharezLook/FrameWindow","framewnd");
-- add it to the sheet
sheet:addChildWindow(fw)

-- set its size and position
local sz = CEGUI.Size:new_local(0.5,0.5)
local pos = CEGUI.Point:new_local(0.2,0.1)
fw:setSize(sz)
fw:setPosition(pos)
fw:setText("Lua says Hello World!")
-- disable user sizing
fw:setProperty("SizingEnabled","False")

Running

Now just compile and run.