1
\$\begingroup\$

I'm starting work with the Allegro C++ library to get some lower level game development experience after working mostly with Unity. I've read about how Lua is a popular scripting language choice for game developers, but I have never used it before. I've been trying to work through how Lua might be integrated with a game engine in my head, and I've ran into some conceptual problems.

I imagine the simple scenario where I have a block that I want to move to the left at a certain speed for 2 seconds. I imagine that something like the Draw() method would sit nicely in the game engine in C++, where the actual command to move the block, being a fairly specific one, would originate in Lua.

I have trouble imagining where the transition between the languages is. I've read that calling to Lua every frame of the Game Loop isn't very performant, so that means that unless Lua can keep track of the movement timer asynchronously and interrupt C++ execution when the timer runs out, I would have to track the timer in the C++ engine. I would need a method like MoveForSeconds(direction, time) implemented in C++. At that point, Lua calling my MoveForSeconds() method is little more than a passthrough, and seems to defeat the purpose of using a scripting language.

What am I misunderstanding here? Or is my example too myopic, and the uses of Lua become more evident on a larger scale?

Thanks

\$\endgroup\$
  • \$\begingroup\$ I haven't done much scripting but I would imagine that on the c++ side you have all your scriptable commands defined and then LUA would just call said commands, proly need some kind of sate machine on a thread to run your scripts so as not to lock the main game thread. My understanding is that you would use scripts so that to change how an object moves you just rewrite the script and not the controller(C++) that way the controller just runs the script and affects your engine. Just some ideas again as i haven't done much scripting work on my engine yet as I'm the only dev and have no need LUA \$\endgroup\$ – Justin William Stanley Bryant Jan 9 '17 at 6:38
  • \$\begingroup\$ There's actually a few different approaches to how scripting languages interact with the engine code (and where the "transition" occurs). Describing all of them would probably be too broad, but I'd recommend checking out "Game Engine Architecture" (2nd Edition) by Jason Gregory - it has a short discussion of the possibilities in the chapter on Gameplay Systems. \$\endgroup\$ – UnholySheep Jan 9 '17 at 7:21
  • 1
    \$\begingroup\$ Also you can check out "Game Coding Complete" by Mike McShaffry and David Graham for an example of how Lua can be integrated into a C++ game engine - including source code and explanations what is integrated how and where (and why). \$\endgroup\$ – UnholySheep Jan 9 '17 at 7:25
  • 1
    \$\begingroup\$ Why do you need to use a scripting language? Just write the game logic in c++ \$\endgroup\$ – Bálint Jan 9 '17 at 7:50
  • 1
    \$\begingroup\$ @Daniel Every game is moddable, a bad and weak scripting integration is worse than when you need to deckmpile it \$\endgroup\$ – Bálint Jan 9 '17 at 9:05
1
\$\begingroup\$

A scriptable engine has 3 parts, the code, an interface and the script.

Let's say you want to be able to write scripts for entities. In code you create an entity component system to easily add scripts as components.

The interface is a simple lua interpreter with custom functions/objects to modify the entity (things like a way to get other components and to modify them).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.