How to turn my code onto a library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Winum
Prole
Posts: 2
Joined: Sun Jul 31, 2022 11:43 pm

How to turn my code onto a library

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to turn my code onto a library

Post 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.
obey
Winum
Prole
Posts: 2
Joined: Sun Jul 31, 2022 11:43 pm

Re: How to turn my code onto a library

Post by Winum »

Got it, thank you
Rigachupe
Party member
Posts: 100
Joined: Fri Jun 18, 2021 11:21 am

Re: How to turn my code onto a library

Post 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?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to turn my code onto a library

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to turn my code onto a library

Post 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.
User avatar
kbmonkey
Party member
Posts: 139
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: How to turn my code onto a library

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests