Writing a Library
Let's write a library for the Ichigo Template. Libraries are not protected by the ichi environment, but rather extend it via the table passed to them. They are automatically loaded before all plugins and main.lua. Libraries can also return Actors that will be added to the Actors table. Let's create a simple plugin that will allow us to draw an arbitrary quad.
Because libraries are not protected by the ichi environment, there is potential behavior that can be unsafe for the template to occur with improper code. Please be aware of existing template variables on the API and more page.
Navigate to the /lib folder and create a new file called drawing.lua. Inside, write the following:
local ichi = ... -- get environment
local quadNext, let's write a function that will draw the quad. (Don't worry about quad being blank for now, we will create this later.)
ichi.drawQuad = function(x, y, sizex, sizey, rot, col)
rot = rot or 0
col = col or color("#FFFFFF")
quad
:visible(true)
:xy(x, y)
:SetSize(sizex, sizey)
:rotationz(rot)
:diffuse(col)
:Draw()
:visible(false)
endNow we can create and return our Quad Actor.
return Def.ActorFrame {
Def.Quad {
InitCommand = function(self)
quad = self
self:visible(false)
end
}
}The entirety of the code should look like this:
-- drawing.lua
-- written by Sudospective
local ichi = ...
local quad
ichi.drawQuad = function(x, y, sizex, sizey, rot, col)
rot = rot or 0
col = col or color("#FFFFFF")
quad
:visible(true)
:xy(x, y)
:SetSize(sizex, sizey)
:rotationz(rot)
:diffuse(col)
:Draw()
:visible(false)
end
return Def.ActorFrame {
Def.Quad {
InitCommand = function(self)
quad = self
self:visible(false)
end
}
}Congrats! Your first library is now created. You can extend on this library by adding more elements to draw if you wish. We can use this library in a function specifically for drawing. Add this to main.lua:
function draw()
drawQuad(SCX, SCY, 64, 64) -- default rotation and color
endNow you should see a small white Quad in the middle of the screen.
Last updated
Was this helpful?