I'm trying to make an A* pathfinding algorithm but the AI isn't chasing the player and I get this error thats listed at the bottom

-- Define enemy speed
local enemySpeed = 150

-- Initialize enemy position within the update function
local enemyPosition

-- Function to calculate distance between two points
local function distance_squared(p1, p2)
local dx = p1.x - p2.x
local dy = p1.y - p2.y
return dx * dx + dy * dy
end

-- Function to calculate path using A* algorithm
local function find_path(start, goal)
-- Placeholder implementation of A* algorithm
-- Replace this with your A* implementation
return {start, goal}


function update(self, dt)
-- Initialize enemy position within the update function
if not enemyPosition then
	enemyPosition = go.get_position()
end

-- Get player position
local playerPosition = go.get_position("player")

-- Calculate path to player using A* algorithm
local path = find_path(enemyPosition, playerPosition)

-- If path is empty, return
if #path <= 1 then
	return
end

-- Get next waypoint from the path
local nextWaypoint = path[2]

-- Calculate direction vector towards the next waypoint
local direction = vmath.normalize(nextWaypoint - enemyPosition)

-- Update enemy position towards the next waypoint
enemyPosition = enemyPosition + direction * enemySpeed * dt

-- Set enemy position
go.set_position(enemyPosition)
end

ERROR:SCRIPT: enemy/enemy.script:28: Instance player not found
stack traceback:
[C]:-1: in function get_position
enemy/enemy.script:28: in function <enemy/enemy.script:21>

Answer is in the error: ERROR:SCRIPT: enemy/enemy.script:28: Instance player not found.
Line 28:

local playerPosition = go.get_position("player")

Check out go.get_position documentation, and be sure the ID is on the collection(or it is in correct format -string, hash, url).

2 Likes

I did that but I still get the error


I also changed it to ‘local pos = go.get_position(“player”)’ and still get the same error

What is this screen? It isn’t contain enemy or even your enemy/enemy.script (which you have error)? So are they on different collections or what?

If you are using collection factory for player collection than you can’t just use ‘player’ as address to get its position.

that was my player.collection
This is my enemy.collection

This is my main.collection
image

I appreciate the help, do you not think that the error lies somewhere in the enemy.script
Edit: Thanks man I’m not getting that error anymore however the enemy isn’t chasing my player which is a bit confusing

Yeah it always does. Good luck :rofl:
You can always use ‘print’, ‘pprint’ or even Debug to figure out what is wrong.

Where could I use that in the enemy.script
– Define enemy speed
local enemySpeed = 150

-- Initialize enemy position within the update function
local enemyPosition

-- Function to calculate distance between two points
local function distance_squared(p1, p2)
local dx = p1.x - p2.x
local dy = p1.y - p2.y
return dx * dx + dy * dy
end

-- Function to calculate path using A* algorithm
local function find_path(start, goal)
-- Placeholder implementation of A* algorithm
-- Replace this with your A* implementation
return {start, goal}
end

function update(self, dt)
	-- Initialize enemy position within the update function
	if not enemyPosition then
		local p = go.get_position()
	end

	-- Get player position
	local pos = go.get_position("/player#collectionfactory")

	-- Calculate path to player using A* algorithm
	local path = find_path(enemyPosition, playerPosition)

	-- If path is empty, return
	if #path <= 1 then
		return
	end

	-- Get next waypoint from the path
	local nextWaypoint = path[2]

	-- Calculate direction vector towards the next waypoint
	local direction = vmath.normalize(nextWaypoint - enemyPosition)

	-- Update enemy position towards the next waypoint
	enemyPosition = enemyPosition + direction * enemySpeed * dt

	-- Set enemy position
	go.set_position(enemyPosition)
end

What do you mean?

Are you sure you got the player position right? Look like you are not:

local pos = go.get_position("/player#collectionfactory") -- You are trying to get the position of the 'collection factory', which is pointless.
pprint(pos) -- this will print the position(if you can get it correctly)

Please read the documentations which I share with you before.

What is your main.script? How could you use 2 main.script in a collection?

One thing to notice, when you use go.get_position() the param should be an url of a game object, not collection name

1 Like

This is my main script
image

I installed this A* Path Finding
Do you know how to implement this into the script because I think that was the issue

There is pretty good documentation in the asset itself:

There’s also an example project:

2 Likes

I don’t think so, you should ensure you got the position correctly before considering using A* Path Finding.
Base on your project structure, I think your player position could be gotten by local playerPosition = go.get_position("player:/player")

1- Please do not waste our time to fix this ‘stupid AI’ generated code. Start by learning the basics of Defold.
2- Since you can’t even get the position from collection or even write this code by yourself, it is almost impossible for you to implement and use this A* lib. Start by learning the basics of Defold.
3- You don’t even know the A* is a TILE based solution. I can’t see a tile map on your collection. DO you have one, do you know how to place and get tiles and data? I highly recommend you to go and learn about A* first.

After you had the knowledge, as @britzl mentioned, there are simple examples in the A* lib go ahead and check them out.

1 Like