Can you import lua libraries from a script? (SOLVED)

Hi!

I’m trying to use the require function to import an easing library into my script (link to the library).

I downloaded that file into my Aseprite’s script folder, and at the beginning of my script I typed:

require("easing.lua"); 

But when I try to load my script in Aseprite, I only got this error:


Am I doing something wrong? Is there any way to load other lua files while using Aseprite scrpts or maybe I should just copy & paste the easing functions into my script? (this works fine, but I think that if I can organize things like that then the scripts would be cleaner).

Thanks in advance! :slight_smile:

The require() function is not yet implemented (as it might need to be able to import modules from other sites/locations). Anyway you can use the dofile("./otherfile.lua") function instead.

1 Like

That’s great, thank you! :smiley:

After some tries (lots of them to be honest XD) I got it to work! Yeeah! :smiley:

What I wanted is to have a library of easing functions, so I did the following for the library file (code is abridged for the sake of simplicity):

-- easing.lua 

function linear(t, b, c, d) 
  return c * t / d + b
end

function inQuad(t, b, c, d)
  t = t / d
  return c * t^2 + b
end

And the following for my script file:

-- myScript.lua 

dofile("./easing.lua")

local sprite = app.activeSprite

local startingFrames = 1
local totalFrames = 10 -- Total number of frames for the animation
local newFrames = totalFrames-startingFrames
local startingPosPix = 0
local distancePix = 20

-- Duplicate the actual cell to get all the needed frames
for i=startingFrames, newFrames do 
app.command.NewFrame()
end
local easingChoiceForX = "linear"

-- Go over all the cells and move them
for i, layer in ipairs(sprite.layers) do
    for j, cel in ipairs(layer.cels) do
	  local xIncrement = 0
	  
	  if easingChoiceForX == "linear" then xIncrement = linear(j-1, startingPosPix, distancePix, newFrames)
	  elseif easingChoiceForX == "inQuad" then xIncrement = inQuad(j-1, startingPosPix, distancePix, newFrames)
	  else print("ERROR!!!!! EasingChoice for X Unknown!!")
	  end 
	  
	  cel.position = { x = cel.position.x + xIncrement, y = cel.position.y }
    end
end

I’ll be writing some scripts that I had in mind for the past months, and I’ll be posting them here, hopefully soon :slight_smile:

Also, just in case I’ll leave here a warning for novice coders like myself: If you want to call functions from another file, you shouldn’t declare them local (which makes sense, now that I think about it XD) or you will get an error like the following one:

3 Likes