Using Lua with C++ in practice. Part 1. Intro to ECS and basic principles

There are many reasons to use Lua with C++. One of them is that you can put some of the logic from C++ code into scripts, so you can easily change them without the need or recompilation. You can also write some good interfaces, so the scripts are easy enough for even non-coders to write them. Lua is free, Lua is fast, Lua is used in game development quite often.

While there are plenty of good articles about using Lua with C++, I think there are not enough articles about how to use Lua in real projects.

This article is one of the many articles I plan to write. Here are some topics which my articles will cover:

  •  Entity creation and other basic stuff (you’re reading this now)
  •  How to implement entity creation
  •  Managing Lua state and cleaning up
  •  Scriptable state machines
  •  Events and callbacks

The stuff described in the articles was mostly discovered during the development of my game called Re:creation.
I don’t think the methods here are perfect, but they’re good enough, fast and work well for me. So, your feedback is welcome. Feel free to leave comments and write e-mails to me about the stuff I can do better. I’m interested in hearing about your Lua/C++ usage!

While this is a C++ article, I think you can implement most of the things mentioned here in your favourite language. I’ll use Lua C API and LuaBridge for examples, so I recommend to read the following articles if you’re not familiar with them:

Continue reading

Advertisements

Re:creation dev log. April-May 2015. Recreation mechanic, archers, event system and more!

Last two months were pretty awesome. I had lots of free time and was able to implement lots of new stuff!

Recreation mechanic

Let’s start with a gameplay gif:

It’s another example of recreation mechanic.
For those who don’t know, recreation mechanic is the main mechanic in my game. When undead hero kills people, he can leave his body and control dead people with his ghost. He gains their abilities to progress through the game and solve various puzzles. But he can’t leave his body behind for a long time because he can’t carry the hammer while controlling other people. The hammer can do some stuff which normal weapons can’t. (break some floors, for example)
This hammer is very heavy so he can’t carry other weapons. So, for example, in order to shoot arrows, you need to kill archers and control them with your ghost.
He can also use his ghost to reach inaccessible areas. But this won’t be very useful if there are no corpses lying there because he won’t be able to interact with the world this way. But it has another use: you can look around and see what you have to deal with next. This will be very helpful when solving complex puzzles.

Continue reading

Re:creation dev log. March 2015. Graphics improvements, entity inheritance, improved state machines and more!

This month was great. I finally got in the flow of development and managed to get lots of things done. I worked hard to make game look better and here’s the result. Compare old screens with new screens:

October 2014

 

March 2015

Continue reading

Re:creation dev log. December 2014 – February 2015. Bigger levels, boss and heavy attack

A long time has passed since I’ve written the last dev log! Is Re:creation dead? Is it stagnating? No, not at all. It’s more alive than ever now!
I haven’t written a new part of dev log because I had to study a lot in December and January. I had some time to develop some stuff during these month and had a lot of stuff done in February.
I’ve decided to write the dev log in two forms

  • First one will be about the features I’ve implemented recently and it will have a lot of pretty screenshots and gifs. (You’re reading this part right now).
  • Second one will be more specific and I’ll focus more on technical parts of the game and implementation details of some interesting features. This part is a lot harder to write so it’ll be less frequent than the first one.
    If you’re wondering about how I’ve implemented one or another feature of the game or its engine, feel free to write an email and ask about it! I’m always glad to answer.

Well, let’s start.

Continue reading

Re:creation dev log. November 2014. Core game mechanic explained and more.

Re:creation info/press kit | Previous dev log

 

I did a lot of stuff this month and I’ll start with the most important thing.
I’m ready to present the core game mechanic in the game!

Recreation

I call this mechanic “recreation“. Here’s how it works.

First, you kill someone

Then you can become a ghost and travel around the screen without taking any damage and colliding with anything

You can’t get too far away from your body though!

When you are in the ghost form you can become the enemy you just killed

It gets revived in the zombie form. You don’t have many health in this form so you can’t complete an entire level like that, you’re most likely to die. When you die, you return to your original body.

This mechanic is used to solve puzzles and progress forward. Each enemy will have unique skills which will help you on your way.
Here’s an example of solving a simple puzzle.

I have some other puzzles but I can’t show them right now because they need to be polished! I also don’t want to spoil too much for people who read my dev logs.
There are lots of puzzles which can be invented with this mechanic and I’m really excited to work on puzzles involved it!

And now I’ll tell you about other things I’ve done

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