Difficulties including lua libraries manually

I’m trying to include a pure lua pathfinding library which works fine in lua, but inside defold it has build errors. Does defold use some breaking version of lua? I can’t figure out why these errors are happening.

This is the lua library I downloaded, and put into a jumper folder inside my main folder for the defold project, and then used require() in main.script to get the script to load, using this example script directly from the github.

This library is a pathfinding library. I know there’s already one made for defold but it’s not as good, for one thing the pathing in the defold one is robotic/overly simplistic, in this one the paths generated are ones humans would take which cost the same - because it understands more than one heuristic for tile cost.

-- Set up a collision map
local map = {
	{0,1,0,1,0},
	{0,1,0,1,0},
	{0,1,1,1,0},
	{0,0,0,0,0},
}
-- Value for walkable tiles
local walkable = 0

-- Library setup
-- Calls the grid class
local Grid = require ("main.jumper.grid")
-- Calls the pathfinder class
local Pathfinder = require ("main.jumper.pathfinder")

-- Creates a grid object
local grid = Grid(map)

-- Creates a pathfinder object using Jump Point Search algorithm
local myFinder = Pathfinder(grid, 'JPS', walkable)

-- Define start and goal locations coordinates
local startx, starty = 1,1
local endx, endy = 5,1

-- Calculates the path, and its length
local path = myFinder:getPath(startx, starty, endx, endy)

-- Pretty-printing the results
if path then
	print(('Path found! Length: %.2f'):format(path:getLength()))
	for node, count in path:nodes() do
		print(('Step: %d - x: %d - y: %d'):format(count, node:getX(), node:getY()))
	end
end

Got it to get past that by commenting that out, not sure why that syntax doesn’t break the project outside of defold.

encountered a new problem where defold lua can’t understand this type of pathing:

  local _PATH = (...):gsub('%.pathfinder$','')
local Utils     = require (_PATH .. '.core.utils')

Instead you have to go in there and create manual pathing for every require, such as

local Utils = require ('main.jumper.core.utils')
1 Like

The library works now after that latest fix ^

Are you using lua 5.3 or feature extended from lua 5.1? Defold is based on luajit (mostly) and this is a 5.1 /5.2 compatible lua. I suspect this is the prob.

1 Like

This is because Defold only includes those files into the bundled game that it knows are actually used. For game objects and atlases, it can just check out which ones are included in the main collection. For Lua modules, it checks all calls of require in the included script files. If it contains a variable, it is not clear which Lua file it will load, so the file is not bundled, and can’t be loaded when running the game. So each require must explicitly use the full path.

7 Likes

Looking at your build errors, it seems the cause is at the first line in your module. It should be local function addNode(...)

1 Like

Another note. I would recommend not putting search paths in to a require.
There is a better mechanism (already part of lua) for that.
Have a look at the env paths commonly used for this - the package.cpath and package.path
cpath is used for finding dlls/shared libs and path is used for finding lua scripts and libs.
You can extend it like so:
package.path = package.path..";my_folder_with_lua_libs/?.lua"
See lua docs for more info.