## Registration code : 12 ##
It is currently Mon Nov 20, 2017 10:54 pm

All times are UTC + 1 hour [ DST ]





Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: GUI and Lua
PostPosted: Sat Sep 06, 2008 10:58 am 
Offline
Site Admin
User avatar
 WWW  Profile

Joined: Mon Jan 28, 2008 9:44 pm
Posts: 200
Location: France
I've started working on re-writing the GUI code for it to work with OGRE, and decided to completely change the system I used : now, I'm directly using GUI widgets in my code (UIObject, Frame, ...). It will require a bit more work, but it'll be much cleaner, will consume less memory and will speed up compilation time.

I've also added a few tools to help in Lua glues creation in C++ : let's say you have a Lua function that takes a string and a float plus an optional boolean. When you execute the function in C++, you have to make sure that the user has :
  • given enough arguments
  • ...with the proper value type
The Lua::Function class can do all that for you ! Let's see how :
Code:
int MyLuaFunction( lua_State* pLua )
{
    // Create the function definition (name, lua state, number of return value)
    Lua::Function mFunc("MyLuaFunction", pLua, 1);
    // The first argument (rank, name, type)
    mFunc.Add(0, "name of parameter 1", Lua::TYPE_STRING);
    // The first argument (rank, name, type)
    mFunc.Add(1, "name of parameter 2", Lua::TYPE_NUMBER);
    // The first argument (rank, name, type, optional)
    mFunc.Add(2, "name of parameter 3", Lua::TYPE_BOOL, true);

    // Check if arguments are correct, and retrieve them
    if (mFunc.Check())
    {
        // Everything went fine, we can safely use our arguments
        s_str sParam1 = mFunc.Get(0)->GetString();
        s_float fParam2 = mFunc.Get(1)->GetNumber();
        s_bool bParam3;
        // Parameter 3 is optional, so you should check if it has be provided or not
        if (mFunc.IsProvided(2))
        {
            bParam3 = mFunc.Get(2)->GetBool();
        }

        // We have collected all the required data from Lua,
        // we can now use them :
        s_int iResult = MyCFunction(sParam1, fParam2, bParam3);

        // The function has a return value, we can give it to Lua :
        mFunc.Push(iResult);
    }
    else
    {
        // The user has done something wrong.
        // Check() has printed the error(s) in the log file.
    }

    // Here, we just return the number of values this function has returned to Lua.
    return mFunc.Return();
}


Another thing one can do with this tool, is to allow an argument to have several possible types.
Let's say you have a function that either takes a number or a string as its only argument. Here is how to do it :
Code:
int MyLuaFunction2( lua_State* pLua )
{
    // Create the function definition
    Lua::Function mDef("MyLuaFunction", pLua);
    mFunc.Add(0, "first name of parameter 1", Lua::TYPE_STRING);
    // Notice the zero :
    mFunc.Add(0, "second name of parameter 1", Lua::TYPE_NUMBER);
    // Check if arguments are correct, and retrieve them
    if (mFunc.Check())
    {
        // Everything went fine, we can safely use our argument
        s_ptr<Lua::Argument> pArg = mFunc.Get(0);
        if (pArg->GetType() == VALUE_STRING)
        {
            // The user has provided a string
            s_str sValue = pArg->GetString();
            MyCFunction2(sValue);
        }
        else
        {
            // The user has provided a number
            s_float fValue = pArg.GetNumber();
            MyCFunction3(fValue);
        }
    }

    // We don't return anything, but let's say it anyway
    return mFunc.Return();
}


Believe me, it's a very handy tool (just try to do what it does with the standard Lua API, and you'll see how painful it is).

Note : it takes a bit more CPU and memory to use this tool instead of the Lua API. It may become a problem if you're calling your functions very often (I don't know, more than 1000 times per frame maybe). Everything has a price in this world ;)

Edit (16/06/2009) : I've adjusted the code snippets so they are compilable with the latest SVN release.

_________________
- Kal.


Top
 

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour [ DST ]



Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
SourceForge.net Logo
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group  
Design By Poker Bandits