Correct way to extend Lua standard libraries

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Tabaqui
Prole
Posts: 34
Joined: Tue Mar 24, 2020 2:47 pm
Location: Italy

Correct way to extend Lua standard libraries

Post by Tabaqui »

Hi all!

I'm working on a large Love2D project using a library i've written to handle OOP, and i've decided to divide it into multiple files (one for each class plus main.lua and conf.lua). Whenever i need to instantiate or extend a class i'm simply requiring it with require "ClassName" and it just works fine.

While the project grows i've noticed that i'm repeating a lot of code for simple tasks like finding items inside tables and stuff like that, so i thought i could write some simple table extension functions, but i would like to use them in pure lua syntax without the need to require some specific library each time (ex: if table.contains(t, element) then etc...).

My first thought was to declare such extension methods in my main.lua, but i don't like this approach very much because i'd like to keep the file as clean as possible.

What would be the best way to extend Lua libraries (possibly by declaring my custom functions in a separate file) and "patch" them once in my main.lua so i can use them from everywhere without worrying to require them?

Thank you in advance
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Correct way to extend Lua standard libraries

Post by darkfrei »

Tabaqui wrote: Fri Feb 03, 2023 4:29 pm Hi all!

I'm working on a large Love2D project using a library i've written to handle OOP, and i've decided to divide it into multiple files (one for each class plus main.lua and conf.lua).
http://kiki.to/blog/2014/03/30/a-guide- ... a-modules/
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests