Adding LuaScriptModule to Sample FirstWindow

From CEGUI Wiki - Crazy Eddie's Gui System for Games (Open Source)

Jump to: navigation, search

Written for CEGUI 0.3


Works with versions 0.3.x (obsolete)

Obsolete warning: This article has been written for an old version of CEGUI and likely doesn't apply to current stable!

This applies to the CEGUI 0.3.0 Release.

Contents

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

   #include "CEGUI.h"
 + #include "../ScriptingModules/CEGUILua/LuaScriptModule/include/LuaScriptModule.h"

In Sample_FirstWindow.cpp

 bool FirstWindowSample::initialiseSample()
 {
     using namespace CEGUI;
 
 +   // create a lua_state by using [[User:Lindquist|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.

Personal tools
Namespaces
Variants
Actions
Main Menu
Community
Documentation
Toolbox