• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML how to check if next move/field is in range?

Traversal

Member
Thinking about how to check a players move on a given map.
Map consists of (to keep this sample simple) 5 X 5 rows = 25 fields

The player should only be able to move to the next field from his starting field
(but in all directions) and positioned centered in the new field.
All 25 fields are objects.

How would you check what fields are allowed?


 
Last edited:

roozilla

Member
Think of your room as a grid, your player object will have an x and y bases on that. You can then add logic in your movement code to prevent out of bounds movement. If your map is smaller than your room then youd need to base it off your width and length of thr map. You can figure that out with some conversions of your corner tile objects x and y values if you have the sprite draw at 0, 0
 

Traversal

Member
Had some hope to find a predefined function to handle it.
Might now check for a fixed radius and check if the click on that field/object is within the range.
If so, move the player to that object/field.

 

NightFrost

Member
You mentioned "click" so I am going to assume mouse-driven movement, not direction keys. One solution would be to check distance between instance player is standing on and clicked instance is less than distance between centers - that is, length of the sprite's side - times two. Distance to all permitted destinations is going to be 1.0 or 1.41 times that distance.

(This of course has no provisions for blocked grid cells and moving multiple hops will be problematic, but that wasn't the question asked.)
 

FrostyCat

Redemption Seeker
In this case you could simply take the max norm of the vector from your current position to the target position (i.e. max(abs(x-mouse_x), abs(y-mouse_y))), divide it by the cell size, round down and compare it to 1, but that's not the best path forward.

I would advise mapping that playing field onto a 2D array or grid. Then you can just check adjacent indexes for being in range (i.e. both coordinates between 0 and 4, and not occupied) and that would give you the answer of whether a move is allowed. It would also give easy answers to most other questions your project would need to ask of itself in the foreseeable future.

Anyone who uses instance placement as the main data model for a grid-based strategy game is doing it wrong. 9 times out of 10 it will struggle with movement, waste most of its time checking collisions and scaling distances, its testing of edge cases will be inadequate, and whatever AI it can muster will suck. And I'm telling you this as someone who "accidentally" developed an AI for a grid-based strategy game that only one or two Jam reviewers out of more than 20 could beat.
 
Top