#need example on pathfinding

1 messages · Page 1 of 1 (latest)

random notch
#

Here's what I got, but doesn't find a path to the target:
⁨⁨```control.lua
---@param goal MapPosition
local function findPathToTarget(goal)
local player = game.players[1]
if not player.character then return end

player.surface.request_path {
bounding_box = player.character.prototype.collision_box,
collision_mask = player.character.prototype.collision_mask,
start = player.position,
goal = goal,
force = player.force,
radius = 4,
pathfind_flags = {
cache = false,
low_priority = false,
allow_failed_to_start = true,
allow_failed_to_finish = true
},
id = player.index
}
end


It always returns the ⁨`event`⁩ without a ⁨`path`⁩ property.
dusty dome
#

does it error or not return anything

random notch
#

it returns the event without erroring, but the event has no ⁨path⁩ indicating it didn't found a path.

#

eg: ⁨{"name":134,"tick":3603,"id":1,"try_again_later":false}

dusty dome
#

do you have a listener for ⁨on_script_path_request_finished⁩⁩ yet?

random notch
#

yep, that is where I'm getting the event without the path

dusty dome
#

it returns nil if pathfinding failed according to the API

random notch
#

how can I debug this?

#

I mean, there is no further details on why it failed

#

The scenario I'm trying is:

  • load the game
  • get player position
  • get nearest iron ore position
  • check if iron ore was found
  • request a path from player to iron ore
dusty dome
#

what does your control.lua file look like

random notch
random notch
#

I've also tried to find a path to a known unblocked point ⁨{ x = 10, y = 10 }⁩ because I thought collision would be the problem, but the event still comes without a ⁨path⁩ and so collision is not the thing I guess...

random notch
#

I think I got it.
I believe I need only to call ⁨⁨player.surface.find_non_colliding_position⁩⁩ for the positions passed to ⁨⁨player.surface.request_path⁩⁩...

But I don't fully understand why, since testing without any obstacles the path is still not calculated.

dusty dome
#

not sure, feels a little weird