Page 1 of 1

How to turn my code onto a library

Posted: Sun Jul 31, 2022 11:47 pm
by Winum
Hello,
I created a small lua code to create a board (just a matrix with objects that can move). I want to use this code to create both a game of chess and a game of checkers. Is there a way of turning this code onto a library I can just import and call my buildBoard() function? I know that such a small code should probably just be written onto my main.lua, but I did this exactly as a way of learning more about libraries.
Thx in advance.

Re: How to turn my code onto a library

Posted: Mon Aug 01, 2022 1:35 am
by BrotSagtMist
For yourself or for others?
For yourself you can just outsource code and require makes it behave as if its still in the main file, nothing special there.
If this should be used by others as a general lib the structure is

Code: Select all

local lib={}
local stuff
lib.start= function()
 otherstuff
end
return lib
So require returns the table lib with everything in it there is to access.
Libs should further never create globals or mess with them.

Re: How to turn my code onto a library

Posted: Mon Aug 01, 2022 6:20 pm
by Winum
Got it, thank you

Re: How to turn my code onto a library

Posted: Tue Aug 02, 2022 4:38 am
by Rigachupe
Is there a way to compile lua into .so lib? And then import this in a project? Maybe he is asking something like this because he does not want to share the source code with other or does not want to copy the same lib files to multiple projects he is working at?

Re: How to turn my code onto a library

Posted: Tue Aug 02, 2022 5:21 am
by zorg
Rigachupe wrote: Tue Aug 02, 2022 4:38 am Is there a way to compile lua into .so lib? And then import this in a project? Maybe he is asking something like this because he does not want to share the source code with other or does not want to copy the same lib files to multiple projects he is working at?
you can compile lua code with luajit if you're really hard pressed to obfuscate it, but it's worthless and detrimental in so many ways.
just use a license file and threaten lawsuits like a sane person :3
as for not copying the same files to multiple projects, symlinks exist.

Re: How to turn my code onto a library

Posted: Wed Aug 03, 2022 1:24 am
by pgimeno
I saw a Lua to C compiler, https://github.com/davidm/lua2c but in my testing the resulting executable was slower than Lua, and of course much slower than LuaJIT.

Re: How to turn my code onto a library

Posted: Thu Aug 04, 2022 9:28 am
by kbmonkey
Winum wrote: Sun Jul 31, 2022 11:47 pm I know that such a small code should probably just be written onto my main.lua, but I did this exactly as a way of learning more about libraries.
For future reference, check the modules tutorial on Lua Users Wiki. There are plenty other good articles on there for all things Lua. Check it out!