Jump to content


Photo

Grid Based Enemy AI Movement


  • Please log in to reply
8 replies to this topic

#1 12toe

12toe

    GMC Member

  • GMC Member
  • 191 posts

Posted 29 February 2012 - 10:11 PM

So I'm starting up GameMaker again with a new game this time. It's a dungeon crawling type game. I've got the player movement all set, it's just that, I want the enemies to move in a random direction every time I press a key. The matter is, I have no clue how to do this, so if anyone is willing to help me, it would be very appreciated. I could probably figure out how to make it so that the more advanced enemies move toward the player, but right now, I need random movement. The grid is also 16x16. Thanks.
  • 0

#2 Sirosky

Sirosky

    GMC Member

  • GMC Member
  • 1332 posts
  • Version:GM8

Posted 29 February 2012 - 10:31 PM

This is the best example I've seen so far. Follows a grid perfectly and has advanced movement. Complicated but if you just need an engine, this is perfect: Posted Image

http://gmc.yoyogames...howtopic=286571

Edited by Sirosky, 29 February 2012 - 10:36 PM.

  • 0

#3 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 29 February 2012 - 10:32 PM

if keyboard_check_pressed(vk_left) || keyboard_check_pressed(vk_up) || keyboard_check_pressed(vk_right) || keyboard_check_pressed(vk_down)
{
direct = random_range(1,8)
if direct = 1 //up-left
{
//code to make the enemy move to the up-left grid
}
if direct = 2 //up
{
//code to make the enemy move to the upgrid
}
if direct = 3 //up-right
{
//code to make the enemy move to the up-right grid
}
if direct = 4 //up-left
{
//code to make the enemy move to the right grid
}
//comtinue doing that until you get all 8 directions.
}

To make an enemy move left ata a higher rate than right, or not move diagonal, edit the numbers/range.
to not move diagonal, make it so that
1/2 = up
3/4 = right
5/6 = down
7/8 = left


The "moving code" is kinda what your player does to move in that direction, I can't make it because I dont know how your player moves.

Edited by Ventwig, 29 February 2012 - 10:33 PM.

  • 1

#4 12toe

12toe

    GMC Member

  • GMC Member
  • 191 posts

Posted 01 March 2012 - 02:03 AM

Alright, so, I inserted your code Ventwig. I edited it to make it move. Yet, when I go to test it, nothing happens when I press the keys. Here's the code.
if keyboard_check_pressed(vk_left) || keyboard_check_pressed(vk_up) || keyboard_check_pressed(vk_right) || keyboard_check_pressed(vk_down)
{
direct = random_range(1,8)

if direct = 1 //up-left
{
y -= 16
}

if direct = 2 //up
{
y -= 16
}

if direct = 3 //up-right
{
x += 16
}

if direct = 4 //up-left
{
x += 16
}

if direct = 5 //up-left
{
y += 16
}

if direct = 6 //up-left
{
y += 16
}

if direct = 7 //up-left
{
x -= 16
}

if direct = 8 //up-left
{
x -= 16
}
}

The people are only supposed to be able to move ONE square per PRESS of an Arrow Key.

Edited by 12toe, 01 March 2012 - 02:03 AM.

  • 0

#5 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 01 March 2012 - 09:37 PM

Did you put it in the enemy's step event?
Also, did the syntax say that keyboard_pressed_check()
doesn't work?
I don't know if the name is EXACT, but it should be around that.
  • 0

#6 12toe

12toe

    GMC Member

  • GMC Member
  • 191 posts

Posted 01 March 2012 - 10:04 PM

Did you put it in the enemy's step event?
Also, did the syntax say that keyboard_pressed_check()
doesn't work?
I don't know if the name is EXACT, but it should be around that.


Everything's where it needs to be, I also ran it in debug mode, no errors there. I have no clue why it's not working. Should I send you the editable file?
  • 0

#7 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 02 March 2012 - 01:21 AM

Okay, I tested this out and foun the problem.
Replace this:
direct = random_range(1,8)

with
direct = random_range(0.5,8.4)  //This is an edited version of that line.
direct = round(direct)

Edited by Ventwig, 02 March 2012 - 01:21 AM.

  • 0

#8 12toe

12toe

    GMC Member

  • GMC Member
  • 191 posts

Posted 02 March 2012 - 02:16 AM

Okay, I tested this out and foun the problem.
Replace this:

direct = random_range(1,8)

with
direct = random_range(0.5,8.4)  //This is an edited version of that line.
direct = round(direct)


Thank you so much man! Does thou want a mention in the credits? You kind of just created the main AI for my small game!
  • 0

#9 Ventwig

Ventwig

    GMC Member

  • New Member
  • 279 posts

Posted 02 March 2012 - 11:46 PM

Well, I don't really care about that, but if you really want, a small "thanks" in the credits would be nice :)

But it's not like I'll hunt you down if you forget.
Also, if you want some more help with advanced parts of that AI, I'd gladly help
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users