How to use/import lua libraries?

Hi !

Everything's in the title. I searched the forum for it but could not find what I needed (sorry If I missed something)
I'm trying to use this vector 2 library : https://github.com/vrld/hump
But using the import keyword, it seems I can't use the library like it should be used.
Is there another way to use them or do I have to make this library a class ?

Thanks !

That repo seems to be made for the Löve engine so things may not work directly on Playdate. If you were using Löve you would use luarocks to install this lib.

In your case, and that might already be what you are doing, you would copy the Lua files into your source folder and import them into your project.

1 Like

Thanks for your reply Didier.
You're right that's what I did to import it.
But when I try to instantiate a vector the way it is described in the docs : https://github.com/vrld/hump/blob/master/docs/vector.rst I have an error "global vector is not callable".
I wonder if there is something else to do ?

I did a quick test and it seems to work:

local gfx = playdate.graphics

local vector = import "vector"
local player = {}
player.velocity = vector(0, 0)

function playdate.update()
    gfx.clear()
    player.velocity = player.velocity + vector(1, 2)
    gfx.drawText("player.velocity x, y: " .. player.velocity.x .. ", " .. player.velocity.y, 10, 10)
end

That said, using the SDK's inbuilt playdate.geometry.vector2D is probably advisable for performance.

1 Like

Thanks ! I was actually just importing it without affecting the module to a local vector variable.
Also thanks for the vector2d of playdate, you're right it might be better to use this instead.
Problem solved, thank you !

1 Like