How to create a library?

I want to create a library and distribute it via GitHub. Should I follow any specific guidelines on how to do this so it can easily be added to a Defold project? How should I unit test the library? What’s the best practice for what to do when my library depends on other Lua libraries?

Thanks.

1 Like

Perhaps my question was not specific enough. I’m not asking about how to build/develop libraries in Lua. What I’d like to know is what (if anything) I need to consider with libraries I develop which depend on APIs in Defold.

For example, how would I unit test a library which relies on sys.get_connectivity()? How should I package my library so it’s easy for Defold users to include in their projects? How do I manage dependencies in the library project? Is it safe to simply use Luarocks?

1 Like

There’s a built in system for handling project dependencies using Defold library projects. You can read more about this system here. The library system works best for closed teams of developers sharing/reusing code. Everyone who has access to a library project has full read/write access to the project and can manage the project from the Defold Dashboard. Defold library project can be included in your project by adding them to your game .project file. You can depend on the latest version or on a specific commit (SHA1 or tag). You update the library projects using Fetch Libraries from the menu.

The thing with Defold library projects is that they don’t work well for public/open source project. In such projects you want a master project on for example GitHub from which users can either clone a read only version of the repo or fork it and modify and create pull requests. In the future you will be able to host Defold projects in external locations such as GitHub, and once that happens I’m guessing that you’ll be able to control access in a better way.

In your case it sounds like you want to use the GitHub approach. Including such a project in a Defold project would be a manual process or at least a process that has to take place outside of the Defold editor, for instance using some script to clone or update or by manually copying the project files/folder to the Defold project.

As for testing you can use any unit testing framework out there. Busted and Telescope seems to be the most popular. You can run your unit tests using a headless version of dmengine. This makes it suitable for use on CI systems such as Jenkins, TeamCity, BuildHive etc. All versions of dmengine (release, debug, headless) plus other build tools can be found here: http://d.defold.com/stable/. You can build the project from the command line using Bob: http://www.defold.com/doc/bob. Once you have built the project you run it by executing dmengine_headless from the root of your project.

2 Likes

This is awesome advice. Thank you @britzl :smile:

2 Likes

The first link led to an error


but here’s the libraries manual
Working with library projects in Defold