Jump to content
  • entries
    10
  • comments
    31
  • views
    7,484

LUA CPP "how to" 6/7


Charrua

2,067 views

 Share

5) How to receive returned parameters from LUA functions

(yes one or more)

come on, every time i edit the post, code /code get bad formatted and have to manually (again) correct it (really don't like to see un-indented code).

 

 

So, with an example will see how to receive parameters from LUA function scripts.

 

The example is basically the same as the one exposed in the previous tut (5/7) the only difference is that the function called has a Return sentence so is sending us something, and following the Invoke method, we have to catch that value.

It's good to test if the kind of value received is of the correct type and if so, then we have to convert it from LUA type to Cpp type, here an example:

 

LUA function declared on a script attached to the entity used next

 

function Script:average(intA, intB, intC)
  System:Print("executing average")
  return (intA + intB + intC)/3
end
 

 

 

float call_average(Entity* e, string funcName, int a, int b, int c)
{

  if (e->component == NULL) return false;
  bool success = false;

  //Get the component table
  int stacksize = Interpreter::GetStackSize();

  //Get the global error handler function
  int errorfunctionindex = 0;
#ifdef DEBUG
  Interpreter::GetGlobal("LuaErrorHandler");
  errorfunctionindex = Interpreter::GetStackSize();
#endif

  e->Push();
  Interpreter::GetField("script");
  if (Interpreter::IsTable())
  {
    System::Print(" entity has script");
    Interpreter::GetField(funcName);
    if (Interpreter::IsFunction())
    {
      System::Print(" " + funcName + " defined");
      Interpreter::PushValue(-2);//Push script table onto stack
      Interpreter::PushInt(a);
      Interpreter::PushInt(b);
      Interpreter::PushInt(c);
#ifdef DEBUG
  errorfunctionindex = -(Interpreter::GetStackSize() - errorfunctionindex + 1);
#endif
      success = Interpreter::Invoke(4, 1, errorfunctionindex); //in=4, out=1
      //
      //---------------------------- here the receiving part ******NEW******
      //
      if (success)
      {
        if (Interpreter::IsNumber())
        {
          float retValue = Interpreter::ToNumber();
          System::Print(retValue);
          return retValue;
        }
      }
      /*
      //how to retrieve more than one returned parameter:
      //if function returns two or more parameters, they are poped in inverse order
      //so be careful to ask for them in reverse order (from right to left)
      Interpreter::Pop(); //pop it
      if (Interpreter::IsNumber())
      {
        float retValue = Interpreter::ToNumber();
        System::Print(retValue);
      }
      */
    }
  }
  Interpreter::SetStackSize(stacksize);
}
 

 

Types that can be tested:

Interpreter::isBool, isFunction, isBreakpoint, isNumber, isConnected, isString, isObject, isTable

 

Types that can be converted to:

Interpreter::toNumber, toString, toBool o toObject

 

not so easy but with a recipe all things are made easy, no?

 

just in case you missed it on the code...

if you like you may return more than one value from LUA

LUA admit Return to give a list of comma separated values

 

Return value1, value1, ...

 

they are pushed and when popped, came in reverse order

 

you have to use

Interpreter::pop()

or retrieving each value and then testing it's type and converting back from lua type to c++ type

 

next tut is more long and perhaps i'll post later... smile.png

 

Next, Home, Previous

 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...