Jump to content


Photo
- - - - -

Basic And Advanced Mp_grid Use By Nocturne


  • Please log in to reply
23 replies to this topic

#1 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 04 December 2008 - 01:25 PM

  • Title: Basic and Advanced MP_Grid Use
  • Description: Four files (Very Basic, Basic, Advanced and Other) that use mp_grid and path-finding
  • GM Version: These were done on GM7. If anyone wishes to convert them to GM6 then feel free to PM me and I will include a link in this post.
  • Registered: Yes
  • File Type: 4 .gmk files
  • File Size: each one approx 0.04mb
  • File Link (1): Posted Image


  • File Link (2): Posted Image


  • File Link (3): Posted Image


  • File Link (4): Posted Image

Additional Info

After having helped a few people with AI and seen more than a few tutorials on the theme, I decided to add my grain of sand... Here I have three different files, all related to the GM mp_grid function and demonstrating different ways of using and controlling the paths this can generate... It must be noted that to fully understand these examples (except the Very Basic) an intermediate knowledge of GML is needed!!!

File One... Very Basic MP_Grid Example.
This example has one object that travels from a start point to a finish point and is ideal for Tower Defense style games. The path is created once at the start, but if you press the right mouse button a new wall object is created and the path is adapted to avoid it. This example is recommended for beginners as it is very simple.

All the code in this example is heavily commented!!!


File Two... Basic MP_Grid Example.
This example has a basic AI object with two "states", Patrol and Alert. In patrol it follows a node based path around a room. The path is only generated when the object comes in contact with a patrol node. The AI object and patrol nodes have code in their room editor creation event, and to see it go to the room editor and "control+right_click" the object (For more information on this, see This Post by Chronic). In the example, if you press the left mouse button the AI changes to alert mode and starts moving randomly between alert nodes, again using mp_grid to calculate the paths. There is also the option to place walls dynamically in the way of the AI with the right mouse button, and the AI will automatically recalculate the path. Pressing enter resets the room...

All the code in this example is heavily commented!!!


File Three... Advanced MP_Grid Example.
This is the same as the above, but now has two AI objects, each with it´s own patrol route and four "states"... Patrol, Alert, Spotted, and Follow. There is also a player object and again, if you press the left mouse button the AI changes to alert mode, the right mouse button places and obstacle and enter resets the room. There is also a Player object controlled by the cursor keys.

There are NO comments in this example and it is recommended that you check the first one out and make sure you understand it before downloading this!!!!


File Four... Other MP_Grid Example.
This is an example based off of a file I helped someone with and is used to demonstrate how to dynamically create paths on a step by step basis. Move the mouse around to change the path and see how the 'ship' reacts. When there is no path the 'ship' calculates the nearest point to where the mouse is without a blocking object and moves there. (See post #10 for an edit to stop the ship "bouncing" when there is no path...)

This example has the path finding content commented and is fairly basic and easy to follow.


I hope that these files can be of help to anyone wishing to use and understand the mp_grip and path functions provided by GM. They are by no means perfect, (but then they are not meant to be!) and the code cannot be directly "cut and pasted" into your work. I have provided them as I have been working extensively on this for my game and feel that they may be of interest and learned from...

Please comment, and if you do use anything here, no credit is necessary (but apreciated!).
  • 1

#2 Chhaaiinnssaaww

Chhaaiinnssaaww

    GMC Member

  • GMC Member
  • 584 posts

Posted 04 December 2008 - 06:54 PM

yeah this is pretty cool and an excellent example in terms of teaching.

However it's pretty glitchy for example (on the "other" file) if you put your mouse in a wall, the player goes skits.

I suggest you put the speed of the AI objects down in all your examples.

Overall though its great. A lot of people want good patrolling guard AI for their top down games. Good job :P
  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 04 December 2008 - 07:30 PM

Thanks very much!!

As I said, they are far from perfect, but I intended these as examples to be disected and learned from. And that includes errors and mistakes... For example, I still haven´t worked out en efficient collision code for the Ai objects with one another in the Advanced example. It works, but it´s glitchy... If anyone works out a better way, feel free to post and I will happily change the file!!!

I´m also not happy about using the room editor´s object creation code, as it means that with three or more AI it´s difficult to keep track of the nodes and variables... But I couldn´t find another way to do it, as with lists it seemed impossible to assign specific nodes in a specific order to specific AI...

Anyway, thanks for downloading and the feedback...
  • 0

#4 True Valhalla

True Valhalla

    ಠ_ಠ

  • Retired Staff
  • 4880 posts
  • Version:GM:Studio

Posted 05 December 2008 - 12:09 AM

I'm going to look into all of these, because I think they will be useful with my current RTS, which already uses grid planning. Might help me fix one of my current errors too. Anyway, I'll DL and give some feedback soon :P

edit:OK, tried them all. While I won't make use of the Basic/ADvanced yet (maybe in a future TDS?) I will be looking inot using the Other one for the mouse commands in my RTS.

Very nice, btw :P

Edited by True Valhalla, 05 December 2008 - 03:21 AM.

  • 0

#5 lasttea999

lasttea999

    GMC Member

  • GMC Member
  • 290 posts

Posted 05 December 2008 - 03:13 AM

This is cool! I wonder how you'd make an object move along the grid (like Fire Emblem), though?
  • 0

#6 Mnementh

Mnementh

    15151

  • Retired Staff
  • 6261 posts
  • Version:GM:Studio

Posted 05 December 2008 - 03:14 AM

I have no idea.

EDIT: I suppose that doesn't count... oh well.

Edited by Mnementh, 05 December 2008 - 03:14 AM.

  • 0

#7 True Valhalla

True Valhalla

    ಠ_ಠ

  • Retired Staff
  • 4880 posts
  • Version:GM:Studio

Posted 05 December 2008 - 05:05 AM

I found this example has the same problem I have with my RTS. If you create an enclosed area and motion the thing to go into the closed area, it bugs up and flies off the screen :medieval:

This is in the 'Other' example, btw.

Any idea on how to fix this?
  • 0

#8 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 05 December 2008 - 06:38 AM

Thanks for the replies everybody!!

@TrueValhalla - I never saw that happen before! It´s a bit crazy... and I have no idea why it happens. I´ll play around some more with the Other example and if I discover a fix post back...
  • 0

#9 True Valhalla

True Valhalla

    ಠ_ಠ

  • Retired Staff
  • 4880 posts
  • Version:GM:Studio

Posted 05 December 2008 - 06:49 AM

That would be great :medieval:
  • 0

#10 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 05 December 2008 - 10:31 AM

I´ve edited the "Other" example to now include closed space detection. The ship no longer goes crazy if it doesn´t find a path to an enclosed space, instead it goes to the nearest "good" point on the path and bounces around there! I will fix the bouncing too (shouldn´t be too hard...), but I´ve updated the example with the current changes so that anyone interested can see how it works.

Oh, and if you want to know what I´ve done, I´ve added a for loop to check all the path points for a collision and then create the path to goto the last good point before the collision occurs...

EDIT: Fixed the bouncing... Delete these lines from the step event....

speed=(point_distance(x,y,mouse_x,mouse_y)/20);
 //and...
var dx,dy;

and add this instead...

if x>=dx-5 && x<=dx+5 && y>=dy-5 && y<=dy+5 speed=0 else speed=(point_distance(x,y,mouse_x,mouse_y)/20 )

then this in the creation event to declare the variables dx/y...

dx=floor(mouse_x/16)*16+8;
dy=floor(mouse_y/16)*16+8;

I´ll edit the main post later with the new version...

Edited by Mark13673, 05 December 2008 - 01:05 PM.

  • 0

#11 True Valhalla

True Valhalla

    ಠ_ಠ

  • Retired Staff
  • 4880 posts
  • Version:GM:Studio

Posted 05 December 2008 - 12:31 PM

I'll check it out in the morning;i'm very interested to see how well it works :medieval:

Cheers.
  • 0

#12 True Valhalla

True Valhalla

    ಠ_ಠ

  • Retired Staff
  • 4880 posts
  • Version:GM:Studio

Posted 06 December 2008 - 06:42 AM

Hey, me again.

Just letting you know it works perfectly and fixed the biggest bug in my game. I did a complete redo of my movement system (no easy task!) with your code, and it is better than ever, THANKYOU.

And yeah, I could have editted above post, but this is worth a bump.

10/10!
  • 0

#13 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 06 December 2008 - 06:51 AM

I´m very glad to hear it. Helping just one person makes the effort worth while...
  • 0

#14 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 09 December 2008 - 09:55 AM

New example added. File No1 is now a Very Basic example and is fully commented. Ideal for beginners with a little knowledge of GML...
  • 0

#15 King Killa

King Killa

    <-- ?Which Way? -->

  • New Member
  • 572 posts

Posted 12 December 2008 - 11:11 AM

I'm new to the mp_ functions, and these examples look like they'd be a great help..

However, example one crashes as soon as I try to open it. It comes up with the 'This program has experienced errors and needs to close' thingo that Windows gives you when a program becomes unresponsive.

I can still get the help from the file by looking at the code, however feel that the actual result would be beneficial for my learning.

Any possible ideas on why the game might be crashing?
  • 0

#16 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 12 December 2008 - 11:17 AM

I´m sorry to say that I have no idea whatsover... I just downloaded it to see if the file is corrupted or anything and it works just fine. Try downloading again just in case your copy is corrupted as sometimes this error happens to me when their has been a glitch in the connection when downloading... If that doesn´t work then I don´t know... If you want I can upload the file to another server and PM you the link...

Edited by Mark13673, 12 December 2008 - 11:19 AM.

  • 0

#17 King Killa

King Killa

    <-- ?Which Way? -->

  • New Member
  • 572 posts

Posted 12 December 2008 - 11:26 AM

EDIT: Seems like the reboot did the trick!

Nice tutorial (#1), I have no problems with it all. I'll move onto the others and then give some better feedback!

Edited by King Killa, 12 December 2008 - 11:47 AM.

  • 0

#18 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14389 posts
  • Version:GM:Studio

Posted 12 December 2008 - 05:19 PM

I think the Alerted mode should cause the AI to create a path all the way to the alerted spot instead of making small path to close by nodes... It causes odd movement.
  • 0

#19 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 12 December 2008 - 05:34 PM

You´re right, but I used the alert function in the Basic example to show how to make random search function for the AI, and in the Advanced example there is the "spotted" state. This is very useful for what you are saying as you can set spotted to "true" and change the spotted_x/y coords from another object for the area where a gunshot was heard (for example) and the AI will move there and then start searching in alert state...

Oh and thanks for checking these out...
  • 0

#20 maaic

maaic

    GMC Member

  • New Member
  • 2 posts

Posted 28 January 2009 - 06:41 PM

This system is great and has worked great for me up to the point when i want to add another pathfinder into the room. After i set it's creation codes for the new patrol nodes and the pathfinder the new pathfinder hits its first node, then moves to a wrong node sometimes a few wrong nodes, but when it makes its way back to its designated nodes his patrol path works and never goes astray again. Do you know how to fix this?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users