Using Lua with C++ in practice. Part 2. Implementing the basics

Source code for this article

Hello, this is a second part of “Using Lua with C++ in Practice” series. I recommend to read the first part to know what’s going on and why using Lua with C++ is awesome! (You’ll see it in this arcticle too, of course).

This tutorial will implement a simple ECS model which I’ll improve and use in other parts of tutorials. Note that this is not the best way to implement ECS. I’m making the most basic ECS which can be used to show the main principles of how things work in my game and how you can use them in general. This is not a tutorial about C++, so I won’t spend too much time discussing C++ parts and I’ll mostly focus on Lua/C++ interaction and binding. Let’s start!

Continue reading

Using Lua with C++. Using Lua scripts with C++ classes.

Read the first part about LuaBridge here to see how to install it and use it for basic things.

You can find the complete source code of this article here

This part will show you how to register C++ classes in Lua, call member functions and how to apply scripts to something practical like putting object behaviour code in scripts.
I’m a game developer, so I’ve used examples related to gamedev but you can use Lua almost everywhere, so take a look even if you’re not interested in gamedev.
The code used in this article is as simple as possible while also remaining a complete example which you can run and compile.
I leave out the details which are irrelevant to focus on the main topic: scripts.

Continue reading

Using Lua with C++(Part 3)

Part 1. Why Lua is great and why you should use it. Writing simple wrapper/binding and using Lua for configuration files

Part 2. Getting arrays and calling Lua functions from C++

Part 2.5. Template getArray function and other modifications

Part 3. Calling C++ functions and creating C++ objects

I am very excited to write about this topic because it shows how great Lua is. While Lua is great for configuration files (as I’ve shown in my previous tutorials) it’s even greater when you use it to call C++ functions and you can even create C++ objects classes and modify them with Lua (I’ll cover this in part 4)
Let’s begin.

Note: if you don’t know about how Lua stack works, I recommend you to read this chapter of great “Programming in Lua” book : http://www.lua.org/pil/24.2.html

Continue reading

Using Lua with C++(Part 2.5)

Part 1. Why Lua is great and why you should use it. Writing simple wrapper/binding and using Lua for configuration files

Part 2. Getting arrays and calling Lua functions from C++

Part 2.5. Template getArray function and other modifications

Part 3. Calling C++ functions and creating C++ objects

This is part 2.5 of my Lua tutorial. Why 2.5 and not 3? Well, because this tutorial doesn’t introduce any new concepts. I’ll point out some bug fixes I made and what improvements can be done. It won’t be too big.

Continue reading

Using Lua with C++(Part 2)

Part 1. Why Lua is great and why you should use it. Writing simple wrapper/binding and using Lua for configuration files

Part 2. Getting arrays and calling Lua functions from C++

Part 2.5. Template getArray function and other modifications

Part 3. Calling C++ functions and creating C++ objects

And you can view source code here: https://github.com/EliasD/unnamed_lua_binder

Continue reading

Using Lua with C++ (Part 1)

You can view full source code of this article here

This is VERY outdated, please refer to this version as the latest and up-to-date guide:

https://edw.is/using-lua-with-cpp/

If you want to see Lua in real world practice, check out Using Lua with C++ in practice series.

So, why use LUA?

I’ve seen game devs using different data formats. Some of them use JSON, some use XML, some use plain .txt files etc.. As for me, I use Lua for data, because:

  • It is easy to use without any additional libraries(well, except of lua libraries, of course…)
  • You can use different formulas in your files, for example: some_variable = math.sqrt(2) * 2
  • It’s extremely lightweight and fast
  • It’s under MIT license, so you can use it in any way you want. Just download it and use it
  • It’s ported almost everywhere, because it’s written in C and compiles with almost any C compiler
  • You can use tables to categorize your data which is easy to edit and read

Let’s look at example Lua file:

player = {
    pos = {
         X = 20,
         Y = 30,
    },
    filename = "res/images/player.png",
    HP = 20,
-- you can also have comments
}

With a little class(implementation is below) you can get data in this way:

LuaScript script("player.lua");
std::string filename = script.get("player.filename");
int posX = script.get("player.pos.X");

Pretty neat.

Bindings

You can find lots of bindings here.
But I wanted to write my own, so here it is.

Note: my code is not perfect, but it works. I appreciate your suggestions on how I can improve my code. E-mail me if you find some errors or just to say thanks: eliasdaler@yandex.ru

Continue reading