gameplay programming: when to use lua and when to use c++

Started by
8 comments, last by Alberth 7 years, 10 months ago

I need some things cleared out as i cant seem to find anything that explains this to me,

Anyway Say i was making a game, this hypothetical game was about getting to the finish line before the npcs,

the player can walk run and jump and so can the npcs. and the point is to finish first. When making this game what parts would i code(c++) and what parts would i script(lua),

no answers involving things like graphics physics or anything done by the game engine as those would obviously be written in a low level language.

Advertisement
It depends on what you want.

The answer is anywhere on a spectrum from:
* All gameplay code is Lua,
to
* Lua is just used to configure a few tweakable settings.

It's used both ways by different people (and ways in between).

On some games, even low level features such as the main loop or the graphics pipeline submission could be in Lua.

It just depends on how much you like the language :)
Lua scripts are easier to treat a resource. You can have a file represent an enemy type or another dynamic object in your game. This file can include the name of a script to use as the object's behavior.
This makes extending the game easy to do, and you really begin to see the benefit when your game has many different types of dynamic objects. You do pay the cost of having to create a binding layer between lua and c++. So the benefit may not be worth it if you aren't going to have many types of objects in your game.
My current game project Platform RPG
You can use any style. Legend of Grimrock 2 was almost entirely programed in lua.

With Lua being faster and easier to write, I'd try to push as much as possible into Lua. What's the point of a scripting language if you don't use it when you can?

This then leaves the "lua is too slow for this" parts in C++ (as well as the lua glue to the C++ libraries).

However, that part could be a lot smaller than you'd think at first sight.

With Lua being faster and easier to write, I'd try to push as much as possible into Lua. What's the point of a scripting language if you don't use it when you can?

This then leaves the "lua is too slow for this" parts in C++ (as well as the lua glue to the C++ libraries).

However, that part could be a lot smaller than you'd think at first sight.

So basically lua it as much as possible until you need c++ for performance reasons,

However, that part could be a lot smaller than you'd think at first sight.

Especially if you use LuaJIT :D Damn, it's fast.

So basically lua it as much as possible until you need c++ for performance reasons,

If you enjoy programming in the language and find it more productive than writing C++ code, sure!
I've worked on half a dozen console games (this-gen and prev-gen) that use Lua as their main language, and C++ for engine / high-perf stuff. The reason for that is that it's much faster to write a game in Lua than it is to write one in C++ (i.e. productivity).

Some people don't like 'scripting languages' (e.g. Epic/Unreal voted on which scripting language to use in UE4, and C++ won... :o), so in some games you'd only see it used as a DDL for entities, triggers, etc...

However, that part could be a lot smaller than you'd think at first sight.

Especially if you use LuaJIT :D Damn, it's fast.


Unfortunately, LuaJIT decided to stick with the Lua 5.1 language, and not upgrade to newer versions.
I wonder if anyone would answer but here goes, for core gameplay like shooting, jumping and movement. Would you use c++ or lua.

"core" or "non-core" isn't that relevant, more relevant is how often a thing must be done.

In that sense, it depends what "shooting" means. If you mean user pulling the trigger, that happens 5 times a second or so, max? (Billy the Kid player)

If "shooting" means "moving a bullet", that happens once a frame, say 50Hz?

If "shooting" means 1000 enemies all moving bullets, that's say 50KHz?

My guess is that anything less or equal to once a frame is pretty feasible. A game like CorsixTH does pretty much everything in Lua, and that works even without LuaJIT.

This topic is closed to new replies.

Advertisement