Lua Scripting Engine

I skim read this thread and there are good arguments either way. Personally I used Lua within ArcEmu, and this is what I found.

Creating a dungeon was super quick and easy due to everything being loosely typed and being able to prototype in real time. I know you can use ‘auto x = std::listboost::any{ y, 1, 5, { 1, 2, 3 }, {{3, 2}, {1}}, nullptr, “hello” };’ with modern C++ but I was still more comfortable with the simple syntax of Lua.

Here is an example dungeon with my code style: https://github.com/stoneharry/Misc-WoW-Stuff/blob/master/Scripts/Instances/CoT-TheHunt.lua

I don’t know if there is a better structure but it is the way I implemented it working with the ArcEmu API.

This was just incredibly quick to put together and have working. In C++ if I changed a header file, suddenly the entire project needed to be recompiled which takes > 2mins for TrinityCore (way less on ArcEmu) on my Intel I7-4770 CPU. How often does one need to do that? Quite a lot it turns out when I am designing and implementing. Furthermore I could just link Dropbox to my server and develop entirely on localhost, saving a script and going in game to have the updated script already reloaded and ready to test.

To sum it up, from my experience:

Advantages:

[ul][li]Incredibly fast to develop with, much faster than C++[/li][li]Easy and loose syntax and types[/li][/ul]
Disadvantages:

[ul][li]Slower processing speed (negligible? Is this even a factor on modern hardware? WoW never seems to use many resources, especially compared to other game servers with the same number of players. Also JIT compilation)[/li][li]Attracts bad code (ArcEmu still has horrendous code marked as ‘stable’ under their Git repository and even on their SVN repository despite this being pointed out. Some of it doesn’t even run and I don’t think was even tested. And I’m sure we all remember the scripts being released back in TBC era.)[/li][/ul]
I think the main issue is would anyone even take advantage of a Lua engine? The only people who I imagine could code well in Lua are those well versed in programming anyway, in which case would they use it over C++ if they are used to the C++ way now?

Also it would fragment code in that some content would be done in Lua and some content would be done in C++. This is not ideal at all - it gets confusing fast and scripts may conflict.

One could just refuse all community content in the form of Lua until reviewed by somebody, and refuse all support for coding with Lua. But this then becomes something that needs to be managed and code styles need to be addressed.

I am not really in favour of having a Lua engine unless quality is maintained somehow. At the same time, we don’t want elitist and overcomplicated guidelines to scare off those who can program but are not familiar with certain terminology.

Also I hate the idea of wait(x). That’s what event based programming is for - raise an event that recalls the function or a new one in a set time, reuse functions by marking variables of what event should be handled.

Food for thought, I guess. Just trying to raise some more discussions than the current ones going in circles.

This is just my two cents as I’d like to throw in on this coming from someone who doesn’t know anything about C++ but knows a moderate amount of Lua. Lua is used in many things WoW Based… Addons, Many programs such as PQR, or WoW+. Macros(?), and the infamous Arcemu. I think alot of people know atleast the basics of Lua. Which will bring more contributors. But I do not know alot of people that know C++. It was VERY easy for me to learn the basics of Lua, and if I ever need to learn other things about it its easy to learn. C++ on the other hand, I lack motivation in learning because its hard for me to understand. I mean I could probably learn it if I really set my mind to it but Lua you can learn very fast, and it can be used for advanced stuff also. This being said, I vouch for the implementation of a Lua engine into Trinity Core.

If you code Lua without knowing any C++, it is likely your code is very bad.

Such a sweeping statement, which is purely opinion.

Not really. Prove me wrong?

How do you know what the language is doing? How it is handling functionality and data types? How do you know what is efficient and what is not? How strong is your general algorithm knowledge?

Lua is embedded C. You should have an understanding of what is going on behind that and how your data is being handled. If you do not and find it “too hard” then you are probably just another kid trying to program and giving the language a bad name.

Similar situations can be found in the Java world - the vast majority of programmers are terrible at it because it is what they have taught primarily at universities for many years as an introductory language. Yet if you actually do some research and use the language properly it is quite effective and you will see why it is still one of the most widely used languages in enterprise.

I don’t really want to argue with people who find it “too hard”.

I don’t want a Lua engine for people who find programming ‘too hard’ in other languages. Lua can be a useful tool but that does not mean it can be relied upon. The same can be said about any scripting language for any base.

I don’t think it’s for me to prove you wrong. You’re the one that made the initial statement.

What I will say is that by your reckoning, we shouldn’t trust someone to do C++ unless they have a good understanding of ASM on the architecture(s) they’re working on and a full understanding of the underlying hardware.

You choose to conveniently stop at C++ (for whatever reason) in this comparison. The truth is, whatever level you’re working on, there’s another one deeper down you could have an understanding of.

Yes, I do think you should have an understanding of ASM to some extent. No, it is not directly relevant to programming due to the data types and entire logic flow being completely different to modern languages. Much more importantly, you should have an understanding of how compilers work.

Hence why ASM and computer architecture is taught at universities.

I would only agree in so far to say it’s useful in diagnostics. Understanding why those odd problems might be happening. Otherwise, writing someone off because they don’t know a particular language is somewhat elitist to the extreme.

Anyway, it’s taken the topic quite off-topic. So that’ll be my last comment on the matter.

It’s not really off topic at all. Those going “we need Lua because we can’t program C++” should not be catered for, primarily because they likely won’t be producing anything worthwhile and will instead be a hindrance due to the points I have highlighted in the previous few posts.

The reason for the discussion isn’t off topic. Us arguing about the finer points of skills in software development are. /emoticons/default_smile.png

My personal opinion is that there’s no need for Lua, but for an entirely different reason. That being that an interpreted scripting language if used extensively as the defacto scripts for encounters and so-on could well start causing real performance issues. That’s just my thinking. No idea what the processing overhead of Lua is. But, I suspect it’s more than compiled C++ code.

As I understand it, there’s an extension to add Lua scripting if people want to (quite sure I saw a current project supporting up to TC 4.x branch). That way, the Lua option and the scripts that come with it can be an option.

If the standard scripts for dungeons, raids, BGs and so on all become Lua, I am just wondering how the performance of a busy server would be affected.

I don’t run a server, merely have an interest in fixing/improving things within the project. So I don’t know where the bottlenecks appear on high population. Is it RAM use, CPU, Network saturation or general IO choke points?

If it’s CPU that causes the bottleneck then Lua will likely be bad. If the bottleneck is elsewhere it might not be a problem.

If your point holds true, that people not knowing C++ are bad programmers in general then maybe it could actually help. They would submit scripts, their coding would be torn apart in the style that is common in open source communities and maybe they’d start to learn good practices…

I don’t know.

I agree with both.

Lua is a scripting language and meant as an extention for C(++) in the way developing new would be fast and simple.
That way one can develop code faster without compiling and make less lower level mistakes ensuring better result with the speed.
The cost would be the slowness on runtime and startup and some scattering around to multiple languages, requiring some standards and more maintaining.

Base functionalities should be done in C++ (obviously) and lua would only then use the functions to create logic for AI scripts and events and similar. Generic scripts would be in C++ likely.
Some more complex scripts and parts that require or would be good to be speedy and well thought or require C++ would then be done in C++.

It comes down to if lua would be useful for the devs or community to help devs to create the scripts and logic
rather than if its good for customization or will the code be as good when made by ppl who dont know X language.
If its not useful for the developement goals of the actual project, then I would say there is no benefit in adding such an implementation and it should stay as a 3rd party addition, a patch or some optional addition to the project that one could choose to have. (probably mainly for customization purposes then or branching off)

Its not required to know X language to know how to code good lua, but it can help and then you know more about how you should go around doing things rather than just rely on … say lua.
From experience as I first learned lua and then more C++, I can say that it was way easier to learn more C++ after having the basics already from lua.
Then from fiddling around with lua engine I now understand better how it all works and how things should go, what is good to do and what is bad.
All these experiences should most likely be added to the implementation documentation saying what one should generally do and what not.
From that it will help others who dont have the language experiences to better have grasp on what they should or shouldnt do do and what is effective.
The scripts added to the official repos are likely at least skimmed over by someone else than just the creator.
Same applies to C++. I doubt all ppl releasing C++ scripts or patches and making improvement suggestions know a lot of C++ or ASM. Most likely all TC devs dont know about everything related to the part they are doing or the lower levels of it.

Lets say that in C++ a lot of times there is repetitive usage of player->GetGUID() inside a function and so on.
However in lua as that would be called each time from C++ and it would be (quite) slow to switch from lua to C++ each time, it is faster to just do
local guid = player:GetGUID() and then use the guid variable where needed.
This may not be obvious to one coding lua and might not be that relevant to coding pure lua as its then less of a problem.
The C->lua and lua->C calls are said to be the slowest part. However, luajit is said to have no overhead for pcall (C->lua, unsure about this) and lua->C calls should be that of a calling function through pointer if using FFI.
It might also be faster in C++ to use a variable as well though.

I think the best of both worlds would be better achieved by implementing loadable library support, which would allow c++ scripts to be compiled into (.dll on windows, and .so in Linux) and loaded for testing without recompiling and restarting the entire core, as well as a generic scripting API that would allow users to use whatever scripting language they want by implementing the engine themselves, in a way that would not be broken every time something is changed in the core…

I don’t know why, but TrinityCore’s “scripting” doesn’t really feel like scripting. Something about it being deeply embedded in the worldserver executable and compiled with the rest of the server.

I believe there is need for a scripting engine, but I don’t know if Lua is the way. V8 performance completely smashes that of Lua, but then (I would guess) less people know javascript (Might be wrong, given it’s the most widely used language for client-sided web scripting) than Lua.

That depends on the implementation and if its plain lua or luajit for example. V8 seems to be clearly faster than plain lua, but not faster than luajit.

Luajit /can/ be even faster than C++ in places. However everything is relative to what code and how/when it is executed. Luajit can optimize loops and similar tests really good thus distorting simple benchmarking and C calls are faster with FFI.

V8 does all of the runtime and ahead-of-time performance improvements out of the box. In a lot of scenarios, the difference between V8 and C++ can’t even be measured (you can calculate the theoretical performance drop, but there’s no measurable time).

I don’t see how anything can be faster than the thing interpreting it? If the code was written efficiently in the host language, then the host will always be faster or equal to the guest.

Either way, we’re talking about differences which can’t even be measured in milliseconds. No nanosecond performance boost could counter the convenience a scripting environment provides.

V8 is a beast but i rather wait for asm.js to be done.

AOT optimizations will still kill JIT imho. Just because first AOT would be done at loading, where it doesnt matter. It would only be a hindrance when hotpatching.

Asm.js is a project set out to accelerate the performance of bytecode applications converted to Javascript, and it’s already practically done (definitely usable). V8 is a javascript engine. Code written for asm.js will simply not be readable by humans at all.

Asm.js has no support for strings yet. that’s what i wait for /emoticons/default_wink.png

I might say that despite everything a JS scripting engine would be really interesting.

I must disagree with that statement. I’m more of an assembly coder than I am a C++ coder. In fact, I’m more of a C coder than anything. Have I tinkered with C++? Sure, but my code is far from perfect. Does my lack of C++ mean I can’t program anything decent at all? A resounding NO! It is extremely possible to program very well in one language and known NOTHING of another language. Coding Lua without knowing C++ means nothing about the quality of the Lua code. The languages aren’t even remotely structured the same.

A couple questions based on your point: Can you code AVR/PIC/6502/x86 assembly? If not, how could you POSSIBLY code any other language with any quality to your code?

EDIT: How many WoW addons were written by people that had the absolute inner workings of the WoW server and/or client? Were any addons written by actual Blizzard devs? Are you saying that every addon for WoW is “very bad code” by default due to having no “underlying” knowledge on which they were built?