I learned C++ in a recent college course and to structure our code, each object(class) has it’s own file for it’s members and methods,(like module scripts) those files then get included into the main file where they are used in the main function. The main function serves as a kind of roadmap, utilizing class instances to execute the code.
In scripting languages, there’s no main function. How should I best structure my code?
Each script is started on its own Lua thread (Not a new logical thread or process on your CPU!), which has its own context but runs on the same Lua machine, and so has the same _G table.
Lua’s OOP is not C++ like; Instead, any table or userdata can have an associated metatable. All objects assigned the same metatable refer to that specific metable, it is not copied per object. In C++ this would be like if each area of memory had a .h / .hpp file associated to it at runtime, which describes what member methods it has. However, just like C++, this means that non-virtual member functions, do not have their code repeated for each object, instead they access their containing object through a reference to it (self in lua, this* in C++)