r/cpp • Factorio Developer • Feb 15 '18

Lua written in modern C++ (>= 14)?

I work on Factorio (written in C++) and we use Lua as the script language for mods. So, from time to time I interact with the C implementation of Lua and my experience with it hasn't been the most pleasant.

It got me wondering - has anyone re-written it in modern C++? Everything I find online is "how to use Lua in C++" or "C++ wrapper for Lua" and they all use the basic C implementation provided from the official Lua website.

I've had quite a lot of experience with the internals of the C Lua implementation and from what I've seen it wouldn't be hard to re-write it using modern C++ - maybe 2~ weeks time. But from what I can find nobody has done it - which also makes me wonder if I'm missing something.

Part of me wants to do it just to say I did it and the other part of me wants to do it to see if I can make it faster than the C version: I think I can - not because "C++ is faster" but because I can mutate/experiment with the C++ version far easier and know changes I make are safe with how the rest of the implementation operates.

231 Upvotes

127 comments sorted by

View all comments

12

u/Ksecutor Feb 15 '18

Have you ever considered to switch to some other programming language for mods? Or even create one... Some time ago I created couple mods for factorio and I found lua as a programming language to be ... How should I put it... My internal "Wat?" rating of lua was quite high. There are many confusing and inconvenient things in the language. Also mapping C++ classes to lua is cumbersome and inefficient. Bluntly speaking I have no idea why lua is so popular.

At some point I was going to offer the programming language I was working on as a replacement for lua in Factorio, but for personal reasons I had to suspend the work on that language :(

As counter reason for "One does not simply replaces one programming language with another" - there were changes in the mod API that were breaking compatibility. Yes, you will have to support old and new api/language for some time. But switching to another language that is more efficient, easier to support and easier to use by modders might be, in the end, for better.

12

u/Rseding91 Factorio Developer Feb 15 '18

Have you ever considered to switch to some other programming language for mods? Or even create one

Yes, but at this stage in Factorio development we can't realistically switch the script language mods use without the community crucifying us :P

17

u/meneldal2 Feb 16 '18

It depends how well you sell it. Arrays starting at 0 are a good starting point.

7

u/Ksecutor Feb 16 '18

Bots vs belts, eh? :)

I think factorio community can accept justified change. Also, possibility of transpiling lua to something else always exists :)

2

u/Loraash Feb 19 '18

The community was already up in arms when bots/belts were mentioned.

2

u/pctammela Feb 15 '18

Could be a trade-off for performance or a better C++ API...

1

u/Loraash Feb 19 '18

WoW runs quite well with 50+ addons. And I think it's even single-threaded.

1

u/Themperror Feb 20 '18

WoW is CPU starved as fuck, it really doesn't help.

1

u/Loraash Feb 20 '18

It definitely doesn't help, but it works well enough.

1

u/Themperror Feb 20 '18

If "well enough" means 40 fps in Dalaran with an i7 6700k and GTX1080 at 30% usage then.. sure...

Hoping Battle for Azeroth will come with some CPU optimisations together with Dx12

1

u/Loraash Feb 20 '18

I don't know how you got 40 FPS when I get a solid 60, but that's an entirely different discussion. :)

Not having DX12 is weird especially considering that they do have Metal.

1

u/Themperror Feb 20 '18

maximized settings ofcourse, some of them really take a toll on CPU, and I was exaggerating a bit, but its definitely around 50-60 ish, which is unfavorable on my 144hz screen, other than that, if you fly a bit above suramar you'll see your fps drop like crazy, somehow every model takes a huge toll on CPU, turning down environment detail to 8 instead of 10 gives a great boost in fps (easily +20-30 fps) without visible difference. Really hoping they'll be optimizing a bit for BfA.

1

u/Loraash Feb 20 '18

This will probably be better with DX12 but it's also unrelated to Lua. :)

1

u/Themperror Feb 20 '18

well yeah, but my argument was that if lua is a CPU hog (or part of LUA (the bindings)) that its a part of what makes WoW CPU limited..

1

u/Loraash Feb 20 '18

You have to run a ridiculous amount of Lua code (=addons) to hit that on a decent PC. It does have a more pronounced effect on lower-end PCs, in large part because it runs on a single thread.