Runnin Multiple Lua Scripts In C++

Each Lua script requires only one priming run in order to create its globals so its functions are visible to the C program. In order to fully pass an integer into callfuncscript.lua's square function and receive the function's return, the luapcall's nargs argument must be 1 (one argument passed in) and its nreturns argument must be 1 (1.

  1. Lua Scripts
  2. Runnin Multiple Lua Scripts In C++ 2017
  3. Lua Script Download

This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.

Programming in Lua
Part IV. The C APIChapter 25. Extending your Application

25.2 – Calling Lua Functions

A great strength of Lua is that a configuration file can definefunctions to be called by the application.For instance, you can write an application to plot the graphof a functionand use Lua to define the functions to be plotted.

The API protocol to call a function is simple:First, you push the function to be called;second, you push the arguments to the call;then you use lua_pcall to do the actual call;finally, you pop the results from the stack.

As an example, let us assume that our configuration filehas a function likeand you want to evaluate, in C,z = f(x, y) for given x and y.Assuming that you have already opened the Lua library and runthe configuration file,you can encapsulate this call in the following C function:

You call lua_pcall with the number of arguments you are passingand the number of results you want.The fourth argument indicates an error-handling function;we will discuss it in a moment.As in a Lua assignment,lua_pcall adjusts the actual number of results to whatyou have asked for, pushing nils or discarding extra values as needed.Before pushing the results, lua_pcall removes from the stackthe function and its arguments.If a function returns multiple results,the first result is pushed first;so, if there are n results,the first one will be at index -nand the last at index -1.

Lua Scripts

Runnin multiple lua scripts in c++ 2

If there is any error while lua_pcall is running,lua_pcall returns a value different from zero;moreover, it pushes the error message on the stack(but still pops the function and its arguments).Before pushing the message, however,lua_pcall calls the error handler function,if there is one.To specify an error handler function,we use the last argument of lua_pcall.A zero means no error handler function;that is, the final error message is the original message.Otherwise, that argument should be the index in the stack wherethe error handler function is located.Notice that, in such cases,the handler must be pushed in the stackbefore the function to be called and its arguments.

Runnin Multiple Lua Scripts In C++ 2017

For normal errors, lua_pcall returns theerror code LUA_ERRRUN.Two special kinds of errors deserve different codes,because they never run the error handler.The first kind is a memory allocation error.For such errors, lua_pcall always returns LUA_ERRMEM.The second kind is an error while Lua is running the error handler itself.In that case it is of little use to call the error handler again,so lua_pcall returns immediately with a code LUA_ERRERR.

Lua Script Download

Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.