Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Calling Lua functions from C

The method for calling Lua functions from C has already been covered in the Loading a Lua file section of this chapter—it's lua_pcall. This time around, we will be using the second and third arguments of the function. As a reminder, the second argument is the number of arguments on the stack for Lua to consume, and the third argument is the number of values we expect Lua to leave on the stack for us.

Let's make an example function in Lua that takes two numbers and returns a linear index into a matrix. Only the Lua code will know the width of the matrix. The Lua code for finding this linear index will look something like this:

num_columns = 7function GetIndex(row, col)    return row * num_columns + colend

The preceding ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content