How To: Make your own custom Lunalua library

In this tutorial you will learn how to make your own custom library. Please note that LunaLua v0.3 is required to use this function. In this example we will do a framecounter. This framecounter will count the frames in the game.

First of all we need to create a new file located in: "{Game Main Path}\LuaScriptsLib\framecounter.lua". Now open your newly created file with your editor of your choice. We want to start with the basic structor of a package: All your library-functions will be stored in a table. When require (API.load for SMBX2 Beta 3 or older, loadAPI and loadSharedAPI for LunaLua pre 0.7.3) is called, this table will be returned by your library, containing all exposed functions and variables you define. In addition to defining your own variables, you are able to hook into lunalua events from your library.

Now let's do the count function which is counts the frames. In addition we need a variable to store the passed frames:

To be able to count the frames we have to hook in the onTick-Event. To do so we have to register the event for our library. The best time to do it is when our libraryis getting loaded. There is an event that lets us do this, called onInitAPI:

The last thing we need is a function that is used by the host file. In this example we return the passed frames: We finished our library!

Let's test it with a sample host file:

Congratulations! You made your first library!