Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Lua Quick Start Guide

You're reading from  Lua Quick Start Guide

Product type Book
Published in Jul 2018
Publisher Packt
ISBN-13 9781789343229
Pages 202 pages
Edition 1st Edition
Languages
Author (1):
Gabor Szauer Gabor Szauer
Profile icon Gabor Szauer

Calling C functions from Lua

All of the code that we have written so far has been about exposing C to Lua using Lua Bridge. Any C function exposed through Lua Bridge can be called from Lua. If a function is in a namespace and not a class, it is called with the dot syntax: Math.Sqrt(16). But, if a function is in a class, it needs to be called with the colon syntax: vector:Normalize(). The following code shows how to expose a C function to Lua and how to call it from Lua.

The C code needs to declare the appropriate vector 3 class, a Normalize member function, and a global dot product function. Next, the Register function exposes all of these functions to Lua, using Lua Bridge:

class Vec3 {
public:
float x, y, z;

float Normalize() {
float dot = x * x + y * y + z * z;
if (dot == 0) {
return 0;
}
return sqrt(dot);
}
}

float Dot(Vec3...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime}