Cafu Engine
cf::UniScriptStateT Class Reference

This class represents the state of a script: the underlying Lua state, pending coroutines, metatables for C++ class hierarchies, etc. More...

#include "UniScriptState.hpp"

Public Member Functions

 UniScriptStateT ()
 The constructor. More...
 
 ~UniScriptStateT ()
 The destructor. More...
 
bool DoString (const char *s, const char *Signature="",...)
 Loads the given string as a Lua chunk, then runs it. More...
 
bool DoFile (const char *FileName, const char *Signature="",...)
 Loads the given file as a Lua chunk, then runs it. More...
 
bool Call (const char *FuncName, const char *Signature="",...)
 Calls the global script function with the given name. More...
 
template<class T >
bool CallMethod (T Object, const std::string &MethodName, const char *Signature="",...)
 Calls a method with the given name of the given object. More...
 
template<class T >
bool CallMethod_Impl (T Object, const std::string &MethodName, int NumExtraArgs, const char *Signature, va_list vl)
 Like CallMethod() above, but the arguments and results are passed via vl rather than "...", and if any extra arguments have been pushed on the stack, their number must be given. More...
 
void RunPendingCoroutines (float FrameTime)
 Runs the pending coroutines. More...
 
lua_State * GetLuaState ()
 Returns the Lua state that implements this script state. More...
 

Detailed Description

This class represents the state of a script: the underlying Lua state, pending coroutines, metatables for C++ class hierarchies, etc.

Its main features are:

  • easy calling of Lua chunks, functions and object methods from C++ (Run() and Call()),
  • easy to use support for coroutines/threads: Lua code can call "thread()" and "wait()",
  • easy creation of Lua instances for C++ objects and binding of C++-implemented methods.

Constructor & Destructor Documentation

UniScriptStateT::UniScriptStateT ( )

The constructor.

UniScriptStateT::~UniScriptStateT ( )

The destructor.

Member Function Documentation

bool UniScriptStateT::Call ( const char *  FuncName,
const char *  Signature = "",
  ... 
)

Calls the global script function with the given name.

Parameters
FuncNameThe name of the global script function to be called.
SignatureDescribes the arguments to and results from the Lua function. See below for more details.
...The arguments to the Lua function and the variables that receive its results as described by the Signature parameter.

The Signature parameter is a string of individual letters, where each letter represents a variable and its type. The letters 'b' for bool, 'i' for int, 'f' for float, 'd' for double and 's' for const char* (string) can be used. A '>' separates the arguments from the results, and is optional if the function returns no results. For the results, additionally to the other letters, 'S' can be used for (address of) std::string.

Returns
whether the function call was successful. Note that when a signature was provided that expects one or more return values and the called script code yields (calls coroutine.yield()), the returned values are undefined and thus the call is considered a failure and false is returned. Nonetheless, the related Lua thread is added to the list of pending coroutines for later resumption.
template<class T >
bool cf::UniScriptStateT::CallMethod ( Object,
const std::string &  MethodName,
const char *  Signature = "",
  ... 
)
inline

Calls a method with the given name of the given object.

Parameters
ObjectThe object whose script method is to be called.
MethodNameThe name of the method to be called.
SignatureDescribes the arguments to and results from the Lua method.
...The arguments to the Lua method and the variables that receive its results as described by the Signature parameter.

For more details about the parameters and return value, see Call().

Example: If the variable Obj is bound to Object, then CallMethod(Object, "OnTrigger", "f", 1.0); calls the script method Obj:OnTrigger(value) where value is a number with value 1.0.

template<class T >
bool cf::UniScriptStateT::CallMethod_Impl ( Object,
const std::string &  MethodName,
int  NumExtraArgs,
const char *  Signature,
va_list  vl 
)
inline

Like CallMethod() above, but the arguments and results are passed via vl rather than "...", and if any extra arguments have been pushed on the stack, their number must be given.

Note that the "extra arguments" are a work-around that was not necessary if we could use variadic templates for the implementation of CallMethod().

bool UniScriptStateT::DoFile ( const char *  FileName,
const char *  Signature = "",
  ... 
)

Loads the given file as a Lua chunk, then runs it.

bool UniScriptStateT::DoString ( const char *  s,
const char *  Signature = "",
  ... 
)

Loads the given string as a Lua chunk, then runs it.

(This acts very much like the stand-alone Lua interpreter.)

lua_State* cf::UniScriptStateT::GetLuaState ( )
inline

Returns the Lua state that implements this script state.

void UniScriptStateT::RunPendingCoroutines ( float  FrameTime)

Runs the pending coroutines.


The documentation for this class was generated from the following files: