Enemy AI working strangely help

Hello! I'm making a game, and have decided to start making an enemy. I don't know how to do this, but that's fine, due to the SquidGod tutorial about enemy AI. I've used it for multiple other projects, and it's always worked fine. But for some reason, this time it's been kinda funky. I don't know what it is, but the enemy just won't move. Does anybody have any ideas why this might be, or have an alternate enemy ai? Thanks so much!

Hard to tell without the code in question - do you have the code that's supposed to fire their movement? Does everything else work as expected?

Oh, okay, wait. Sorry, I pretty much just figured it out. Sorry to bother you!

1 Like

No worries! I have a habit of asking things in public and then figuring them out straight after :sweat_smile:

1 Like

Thanks! Actually, I ran into a new problem. I modified the original enemy code from the SquidGod video, so that the player takes damage when the enemy is on a tile NEXT TO the player, instead of in the same tile, and therefore it makes it confusing when the enemies are able to still go into the same position as the player. How might I be able to keep the enemies from going into the same tile as the player, but still chase the player?

If you are using squidgod's code, you can do something like this where you swap the enemy tile:

if NewEnemyX != player.px then
   if NewEnemyY != player.py then
      tell NewEnemyX,NewEnemyY to
         swap "{enemy_tile}"
      end
   end
end

What this essentially does is, checking if the new place the enemy tries to go to is where the player is. if it isn't, the enemy can move to the new location. Hope this helps!

2 Likes

This almost works for me, but if the enemy is in the same X or Y as the player, it disappears, and I'm not entirely sure how to fix this. The game is turn based, so every time you move twice, the enemies go. You can see below that on an enemy's turn, if the enemy goes into the same x or y as the player, it disappears. How might I be able to fix this?
image

Ohhh I know what's happening. Ok so- the way that the code works (if I remember correctly), is that it first erases the monster tile then puts it back in the new location. You'd want to put the "check" after the game changes the x/y location, undoing the change if it happens to be the player tile. I fear my explanation isn't clear enough, so here's how I imagined it:

	newEnemyX = event.x
	newEnemyY = event.y
	if event.px<newEnemyX then
		newEnemyX--
        //now we check the location
        if newEnemyX == event.px then
           if newEnemyY  ==  event.py then
              //its the same as the player coordinates, so we change it back
              newEnemyX++
           end
        end

	elseif event.px>newEnemyX then
		newEnemyX++
        //now we check the location
        if newEnemyX == event.px then
           if newEnemyY  ==  event.py then
              //its the same as the player coordinates, so we change it back
              newEnemyX--
           end
        end
	end
	
	tileAtNewPos = solid newEnemyX,newEnemyY
	tileAtNewPos = name newEnemyX,newEnemyY
	if tileAtNewPos!="white" then
		newEnemyX = event.x
	end
	
	if event.py<newEnemyY then
		newEnemyY--
        //now we check the location
        if newEnemyX == event.px then
           if newEnemyY  ==  event.py then
              //its the same as the player coordinates, so we change it back
              newEnemyY++
           end
        end

	elseif event.py>newEnemyY then
		newEnemyY++
        //now we check the location
        if newEnemyX == event.px then
           if newEnemyY  ==  event.py then
              //its the same as the player coordinates, so we change it back
              newEnemyY--
           end
	end
	
	tileAtNewPos = solid newEnemyX,newEnemyY
	tileAtNewPos = name newEnemyX,newEnemyY
	if tileAtNewPos!="white" then
		newEnemyY = event.y
	end
	
	swap "white"
	tell newEnemyX,newEnemyY to
		swap "enemy"
	end

Its be a bit tedious, so better suggestions are welcome. I hope this fixes it, and sorry for the previous mistake! The game looks super cool btw

So sorry to keep bugging you, but I've found one more thing I haven't been able to fix! This works, but strangely, the enemy only moves if the player is below it, and will therefore only move downwards. Do you know how I might be able to fix this?

Uhhhh I'm not really sure. Can you maybe send the code, so I could take a look?

Yes, here it is for the enemy:

on enemyMoveUpdate do
	newEnemyX = event.x
	newEnemyY = event.y
	if event.px<newEnemyX then
		newEnemyX--
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyX++
			end
		end
		
	elseif event.px>newEnemyX then
		newEnemyX++
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyX--
			end
		end
	end
	
	tileAtNewPos = solid newEnemyX,newEnemyY
	tileAtNewPos = name newEnemyX,newEnemyY
	if tileAtNewPos!="floor" then
		newEnemyX = event.x
	end
	
	if event.py<newEnemyY then
		newEnemyY--
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyY++
			end
		end
		
	elseif event.py>newEnemyY then
		newEnemyY++
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyY--
			end
		end
		
		tileAtNewPos = solid newEnemyX,newEnemyY
		tileAtNewPos = name newEnemyX,newEnemyY
		if tileAtNewPos!="floor" then
			newEnemyY = event.y
		end
		
		swap "floor"
		tell newEnemyX,newEnemyY to
			swap "enemy"
		end
	end
end

I'm pretty sure that this is the same as the code that you came up with before?

And enemyMoveUpdate is called every second player movement.

Thanks for all of the help!

Took some testing, but this should work (there was a misplaced end there on my part)-

on enemyMoveUpdate do
	newEnemyX = event.x
	newEnemyY = event.y
	
	if event.px<newEnemyX then
		newEnemyX--
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyX = event.x
			end
		end
	end
	
	if event.px>newEnemyX then
		newEnemyX++
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyX = event.x
			end
		end
	end
	
	tileAtNewPos = solid newEnemyX,newEnemyY
	tileAtNewPos = name newEnemyX,newEnemyY
	if tileAtNewPos!="floor" then
		newEnemyX = event.x
	end
	
	if event.py<newEnemyY then
		newEnemyY--
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyY = event.y
			end
		end
	end
	
	if event.py>newEnemyY then
		newEnemyY++
		// now we check the location
		if newEnemyX==event.px then
			if newEnemyY==event.py then
				// its the same as the player coordinates, so we change it back
				newEnemyY = event.y
			end
		end
	end
	
	tileAtNewPos = solid newEnemyX,newEnemyY
	tileAtNewPos = name newEnemyX,newEnemyY
	if tileAtNewPos!="floor" then
		newEnemyY = event.y
	end
	
	swap "floor"
	tell newEnemyX,newEnemyY to
		swap "enemy"
	end
end
1 Like

Thank you so much for all of your help! This works perfectly. Thank you so much!

1 Like