πLibraries
Libraries allows you to organize functions that perform similar actions.
What Are Libraries?
Libraries are simply tables containing helper functions that perform similar actions. These functions can then be used throughout your addon. Libraries are a really good way to organize your code.
Libraries should not interact with services nor the game itself! Libraries can interact with each other though.
If your library needs to interact with the game or with services, you may want to consider making your library a service instead. See this page.
Built-In Libraries
Just like with services, Noir comes with built-in libraries. They can be found under Noir.Libraries
.
A notable library is Noir.Libraries.Events
.
Events
Creating A Library
Unlike services-ish, libraries are very simple to setup. Libraries are also fully intellisense supported as long as you add a simple ---@class
annotation.
First, give your library a name. For this example, we'll call your library Matrix
, and we'll have the library provide helper methods relating to SW matrices.
Define the library like so:
And simply add methods to it like so:
You can then use the libary methods like so:
Uh oh! myMatrix
also got changed from the :Offset
method. We only want it to provide a new matrix to prevent issues. We could just use matrix.translation
, but that would get rid of the rotational values. Instead, we can use a built-in library to copy the contents of pos
(parameter of :Offset()
) into a new table, and modify that new table instead. We can use a built-in library for this!
Matrices in Stormworks are actually tables.
Problem solved!
Retrieving A Library
Libraries you create aren't stored in Noir. You have to store them yourselves by either placing them under Noir.Libraries
, or just by placing them into _ENV
like so:
_ENV is a table containing globals.
Adding Credit
If you create a library and you would like to credit yourself or others, you can provide extra parameters to Noir.Libraries:Create()
.
Note that everything after "Name"
is optional. See Libraries.
Last updated