How to make a 'class' in Lua - Lua modules (no libraries)

preview_player
Показать описание
Some people coming from Java or C++ or C# that are used to work with classes may wonder how to make a class in Lua, however classes don't exist in Lua. But don't get discouraged yet, because there is something similar called a module.
Modules are not exactly like classes and there is no rule in how to make them, which gives the opportunity to customize them as needed and also to write them wrong and wondering why they don't work. A subtle problem with them is overwriting. When you make more objects with the same module, they can get to overwrite. The solution to this is adding a parameter in the main module's function called ```self```. It must be the first parameter. After this, you have to put the object you are trying to make in the first parameter. Luckily Lua has a shortcut to this:
-with shortcut: object:new(initial_parameters)

The love.* functions are from a graphics library that I use for graphics. I also use it for debugging because Microsoft decided that the print command is useless and therefore does nothing in VSCode anymore even though shotgun debugging is the only way to debug Lua programs. I know, I know… I said "no libraries", but this library has nothing to do with the modules themselves.

More about modules in the video, hope you learnt something!
Рекомендации по теме
join shbcf.ru