How do I use pathfinding when filling empty cells in a grid?

0 favourites
  • 13 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • UPDATE:

    I got them to move, but I'm now running into an issue where the last dots to move down move a cell too far and intersect with other dots.

    Here's the capx:

    ______________________________________________________________________________________________________________________________________________________________

    Hey everyone. I'm new to construct 2 and I'm trying to work out an issue with path finding.

    Right now I have a hex grid filled with "Cell" objects, each with an instance variable "Index" that's a number between 1 and 36 (It's a 6x6 grid).

    The dimension of the grid is denoted by "DIMENSION" global variable (in this case, 6)

    Cells also have an "IsEmpty" Boolean variable.

    Initially the grid is filled with "Dot" objects. Throughout the game the Dots are destroyed, emptying their inhabited cell (Setting IsEmpty to true).

    The distance between each dot on the grid is denoted by a variable "DistBetweenDots"

    To handle this I have the following event block:

    The nearest dot is selected, and we compare its distance to the current cell.

    If there is no dot in the current cell, the distance to the nearest dot will be greater than DistBetweenDots/2.

    Then, for all empty cells (excluding cells in the top row where Index > DIMENSION, because in that case new dots will be spawned)

    I want to move a Dot from above into the empty cell. The dot to be moved is in the cell whose cell index is (empty cell's index - DIMENSION)

    For this, I create a function MoveDot, with parameters (Dot's current cell index, target cell x, target cell y):

    As seen above, I have placed debug statements to see where it goes wrong.

    I am able to get to the point where a path is found. But once I call On pathfinding path found:

    It does not execute. I know this because debug2 does not get set to "setting along path"

    The logic seems correct. I'm not sure why the On "Pathfinding path found" Event is not triggering. Is the dot unable to find a path? Why would that be?

    Here's the capx:

    Thanks for any advice.

  • Your link is broken.

    Try posting it without the "https://"

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Your link is broken.

    Try posting it without the "https://"

    Fixed. Thanks

  • I think you forgot to add solid behavior to your Cell.

    Or change pathfinding Obstacles property to Custom and add obstacles with events.

  • I don't really think I need obstacles or solids, I just want to move the dot from one location to another.

  • I don't have much experience with Pathfinding plugin, but I believe it needs obstacles to find path around them. Otherwise there is no point in having it. As soon as I add solid behavior to your cell sprite, dots start falling to emptied slots.

    EDIT: sometimes they start falling, sometimes not.

    I still don't quite understand how the pathfinding behavior is supposed to work in your code.

    If you want your dots to move to a free cell, I think you need to fill occupied cells with transparent hexagonal solid blocks and then use pathfinding to find path between them.

  • Ok, I think I fixed it. Or at least made it slightly less broken <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    (link removed)

    I changed the way you select dots that should be moved (nearest dot which is above the cell) and I put the entire "For each cell" loop into a function, because I think it should not be executed on each tick.

    I call this function after a line has been completed or after all dots have stopped moving.

    It still uses pathfinding just to move dots, and not for its true purpose <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    You should probably replace it with MoveTo behavior or Custom Movement or something similar.

    Edit: Fixed a few more things, here is a new link:

    https://www.dropbox.com/s/5tw3tg8ilfs0b ... .capx?dl=0

  • Ok, I think I fixed it. Or at least made it slightly less broken <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    (link removed)

    I changed the way you select dots that should be moved (nearest dot which is above the cell) and I put the entire "For each cell" loop into a function, because I think it should not be executed on each tick.

    I call this function after a line has been completed or after all dots have stopped moving.

    It still uses pathfinding just to move dots, and not for its true purpose <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

    You should probably replace it with MoveTo behavior or Custom Movement or something similar.

    Edit: Fixed a few more things, here is a new link:

    https://www.dropbox.com/s/5tw3tg8ilfs0b ... .capx?dl=0

    Thanks. Unfortunately I can't open your file because I only have version 244. I assume 246 is a paid version?

  • UPDATE:

    I got them to move, but I'm now running into an issue where the last dots to move down move a cell too far and intersect with other dots.

    Here's the capx:

  • Okay. I'm getting really frustrated now, because it SHOULD work but it's not.

    I added a "NumEmpty" Variable that gets added to every time a dot is destroyed.

    The system checks if NumEmpty > 0 every tick and is supposed to call the FillEmpty() function as long as there are empty cells.

    However, when I run it and destroy some Dots, they only move once. and Spawn Dot isn't even called.

    Here's the updated capx:

  • Here is the file saved with version 244:

    https://www.dropbox.com/s/keqejly091y9m ... .capx?dl=0

    Hint - CAPX is actually a zip archive. You can extract it to an empty folder, open .caproj file in a Notepad or other editor and change version in this line:

    <saved-with-version>24600</saved-with-version>[/code:1noq4x2f]
  • Wow. Thank you SO much. You really helped me out! I'm super new to construct 2 so I'm still getting the hang of which events are the best to use for what.

    I really appreciate it, thanks!

  • kellehk

    And... one more improvement <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    Dots can now fall many at once, they don't wait for other dots to stop moving.

    https://www.dropbox.com/s/qm2biem1il1p8 ... .capx?dl=0

    Pretty cool game by the way!

    You can improve controls a bit (especially if you are planning to publish it for mobile).

    Add another invisible sprite DotTouch, make it about 4 times bigger than the dot. Create a container with Dot and DotTouch for easier management, and pin DotTouch to each Dot on start of layout.

    Change your touch events:

    Touch -> On touched DotTouch

    Touch -> Is touching DotTouch

    You don't need to pick Dot inside the event, as it is in the same container, it will be picked automatically.

    And DotTouch will be destroyed automatically too.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)