-- 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] in function get_position
enemy/enemy.script:28: in function <enemy/enemy.script:21>
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
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
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.
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.