Some background information to "set the scene" for learning Lua for use with OT.
1. Script Languages (like Lua) vs Compiled languages (like C++) (the short version)
Compiled languages can do more, and run much faster than scripts, and have many other more technical benefits as well. But they are a poor choice for dynamic modification of a server environment like OT.
Script languages are (almost) as good as compiled languages for logic, and can call functions (like the OT Lu fUnctions that are implemented in C++) to do things the script language isn't capable of. And unlike a compiled language, you don't need a lot of IT skills to work with scripts.
2. Why Lua?
Lua is very widely used, especially for scripting games. It's also open source, so it doesn't introduce an annoying "Intellectual Capital" issues.
If I'd designed OT I'd have gone with Lua too.
3. How does it work?
You'll have noticed that you don't need to compile Lua scripts. They are very convenient: just modify the .lua file, save it, and you can run it immediately.
It works this way because there's some Lua code (the Lua runtime) running inside your OT server (more or less - the low-level technical detals may vary a little, but aren't relevant anyway).
When told to run a script, OT's Lua code:
- Checks if the file is new or has been changed - if so, it loads a new copy of the script from the file
- Interprets the Lua instructions and function calls
- Does whatever the script tells it to do - most of this leads to calling the OT server runtime to affect things in the game
The Lua runtime can call the OT server because the server's C++ code has a Lua interface coded into it, and it has a bunch of functions that conform to Lua's API requiremeients.
So it's all "simple" (at least to the Lua coder
OT sets up the Lua runtime for you, and provides a whole bunch of OT-relevant functions that Lua scripts can call. Lua provides the ability to read script files, figure out what they mean "on the fly", and (if it's told to) interact with the OT server and its environment (map, players, items, etc) in real time.
4. What's Lua and what isn't (exactly) Lua
This is quite important, because the Lua language itself doesn't know anything about OT. Also, while there are C++ functions provided by OT that Lua can call for you, but there are also Lua functions that it can call, and they look the same in a Lua script.
Still, in principle the answer is simple:
- Lua provides the logic and the data structures
- OT provides a bunch of Lua-compatible functions (i.e. functions that can be called directly from a Lua script)
One thing to remember: Lua isn't the only helper component. At least one other runtime turns up in scripts: SQL, which is implemented not in Lua, but in your database server (e.g. MySQL). Lua can use SQL to interact with database tables vioa Lua functions (I'm not sure (yet) if these are OT functions or a Lua DB package, but it makes no practical difference to a scripter.
5. Comments on this background information
Some important implications:
- To use Lua effectively within OT you need to know and understand the range of Lua functions provided by OT
- Setting up some of the interactions between Lua and the OT server seems complex at first
... but ...
- The right place to start is still to learn the basic capabilities of the Lua language.
Of course it's possible to modify existing scripts without much Lua knowledge, but sooner or later (usually sooner) you'll run into a problem you can't deal with. It's better to learn enough Lua to read the Lua documentation and existing OT/Lua scripts before you try to make any significant modifications to existing scripts or write new ones.