morrowind-modding / morrowind-modding.github.io

The Morrowind Modding Wiki
https://morrowind-modding.github.io/
MIT License
9 stars 4 forks source link

Create General-purpose Lua Guide #42

Open herbertgithubaccount opened 3 months ago

herbertgithubaccount commented 3 months ago

The goal of these wiki pages would be to provide a general Lua guide, with the goal of only covering pure-Lua topics. This would make it useful for both openmw and MWSE lua mod authors.

I've outlined a few things that may be worth covering, but please feel free to suggest any other topics that should be discussed, and to offer suggestions on how things might be changed/improved.

The official Programming in Lua book can be very terse at times, and there could be benefit in creating a page that explains some core Lua concepts. It also uses Lua 5.0 syntax, which sometimes clashes with the changes made in Lua 5.1.

Here are some things that may be worth expanding upon in the MMW:

A list of useful references for further reading:

The documentation for Lua is pretty scattered, so it might be useful to gather them all in one place. This could include links to https://effective-lua.dreamweave-mp.com/ the Programming in Lua Book, and some parts of the openMW/MWSE wiki pages. Maybe even some Roblox stuff too.

Suggestions on how to set up an IDE environment

This is covered in the

Common Lua syntax/pitfalls

This would include things like: the intricacies of the and and or statements. Stuff like:

(nil and false) ~= (false and nil)
(nil or false) ~= (false or nil)
(nil and false) == (false or nil)

It could also include problems with table-packing, unpacking, e.g.:

string.format("%s %s %s", nil, nil, nil) --> "nil nil nil"
string.format("%s %s %s", table.unpack{nil, nil, nil}) --> ERROR: bad argument #2 to 'format' (no value)

Mentioning the difference between local and global variables would also probably be nice. I've noticed that quite a few mods in the Lua mod repo use global variables when they are probably intending to use local variables.

Metatables

Metatables are pretty complicated, and I haven't seen that many nice guides on them. Having a dedicated section for metatables would probably be very helpful to some people.

OOP programming

The official guide suggests using the following syntax for OOP:

function Account:new (o)
    o = o or {}   -- create object if user does not provide one
    setmetatable(o, self)
    self.__index = self
    return o
end

I've seen this design paradigm lead to many bugs that are difficult to track, and it's sweeping a lot of the intricacies of metatables under the rug. And this is just my opinion, but I think that making an object its own metatable is usually not the best approach, and can lead to a lot of ambiguity that is difficult to untangle.

This approach also blurs the lines between objects and classes: making a new object is the exact same thing as making a sub-class. It might be nice to propose a different design-paradigm in the wiki.

Explanation of for loops

The under-the-hood implementation of for-loops is a bit complex. And some more detail would probably go a long way. The official Lua documentation has a dedicated chapter on this topic, but it may not be the most accessible.

Explanation of coroutine

The coroutine documentation is a bit backwards in my opinion: it starts out by explaining the most abstract, general-purpose use-case, and only then does it mention the least abstract, most-common use-case: calling coroutine.wrap. I think it would be useful to have an explanation of the usefulness of coroutine.wrap, and then link to the official Lua documentation if people want to know more about the general-purpose use-case.

magicaldave commented 3 months ago

https://effective-lua.dreamweave-mp.com

There's also this guide from the homie Yvan Cwyan put together for dreamweave, mostly meant as general lua guidelines.

herbertgithubaccount commented 3 months ago

oh that would be a good thing to mention as well. i'll add that to the list of points