-
Notifications
You must be signed in to change notification settings - Fork 5
libraries
An assortment of libraries can be found in LemonymousMods/scripts/libs/.
Each library can be plugged into a mod to provide it with additional functionality.
I've provided a short tutorial on how to create and use libraries here.
When coding, we often see ourselves coding the same thing over and over. Instead of typing out the same thing again, we can use copy-paste to speed up the process. This works fine until there is an error in that piece of code. If that code has been copied several times, the error must be fixed several places. This can be an arduous task when the code is spread out in many places.
One solution to this problem is to identify when a piece of code can be output into a generalized function, and output to a library file. Fixing an error can still take some effort if the library file has been copied many times, but library files are at least searchable.
Here's an example library file with a simple function. You can place the file anywhere you like, but in this example I have placed it in the mod's /scripts/ folder.
local example = {}
function example.Hello()
LOG("Hello from a library")
end
return example
To use the library, you can use the function require(path). This function will search for the file in the path you specify. It will then do one of two things:
- If it is the first time the file has been opened, it will run the file as if it was a function, and then it will cache the result.
- If the file has been opened, run and cached, it will not run the file again, but instead return the cached result.
Here's an example init.lua file for a mod using the library. The library is used inside the mod:init function. scriptPath is a value that is added to your mod table by the mod loader before it calls your mod:init function. This is the full path to your mod's .../scripts/ folder, which we concatenate with "example" to locate the example library.
local mod = {
id = "library_example_mod",
name = "Library Example Mod",
description = "An example for how to use libraries"
}
function mod:init()
-- fetch library
local exampleLibrary = require(self.scriptPath .. "example")
-- call a function in the library
exampleLibrary.Hello()
end
function mod:load(options, version)
end
return mod
require is not just used for libraries though. It is useful to separate pieces of our program into logical parts, and then execute the files from our init.lua file.
Here's an example file for weapon definitions:
ExampleWeapon = Skill:new{
Name = "Example Weapon"
}
Now, let's say we wanted to use a library from this file instead of init.lua. We don't have access to mod.scriptPath here. To handle that, require passes the path of the file as a parameter to the file it runs, which can be accessed as ...
The mod loader has a function GetParentPath, which strips the path of the filename itself (it essentially goes one step back).
So if we wanted to require other files in our weapon.lua file, it could look something like this:
-- get the current path
local currentPath = GetParentPath(...)
-- fetch library
local example = require(currentPath .. "example")
-- call a function in the library
example.Hello()
ExampleWeapon = Skill:new{
Name = "Example Weapon"
}Now the library's functions can be called in this file as well, by using the local variable example.
Since require caches the result of the library, it is completely fine to require the same library from multiple files. Just remember that whatever the library returns will be shared among all of them.