random board generation - Arrow Selection 2D grid

0 favourites
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Hi,

    does anyone have any clear ideas on how to use the arrow keys to select from a randomly generated grid?

    I haven't attempted it yet, but here is the version where I select each cell of the grid - now I just want to be able to hop over the pieces that aren't there.

    It seems really difficult with lots of checks, but I'm wondering if there is a simpler way. Maybe with an array or figuring out the distance.. I know the arrow keys aren't a great nav tool, but I'm thinking of using the D-pad on a game controller so its the same.

    I really want it to snap to each piece. I was thinking of potentially getting the nearest sprite in the angle that was keyed maybe?

    Any suggestions?



  • Try something like on keyboard(udlr) "pick closest" to tile plus, or minus half tile distance to the current tile.

    Add a variable to filter when making a random map.

  • I'd normally use an array to set this up, as it has tools to make this sort of thing easier, but let's go with what you've got.

    First you'll need to identify if a grid spot is valid. You've already set up an instance boolean "filled" for your grid object, so that's good.

    Now when you press a direction, you want to see if the next spot in that direction is "filled" or not. You can easily do that with a compare instance variable condition.

    If it isn't filled, check the next position over in the same direction and repeat until one is filled.

    This can be done in a loop. Repeat x times (where x is the width of your grid, or maximum number of spaces you can move), and multiply the offset by loopindex for your positional check. If you run into a filled grid object, stop loop and move the selection to that object. Otherwise, if no valid spots were encountered, nothing happens.

  • newt the problem is if it looks like this:


    if you were selected on the O and hit UP you'd want to go all the way to the top X in that column.

    but in this case:


    UP would go to the bottom right X

    EDIT nevermind.. this stupid website is so horrible you can't make fixed width text.. pain in the ass..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I think I see what you mean.

    You would just make a rule that if it was on one of the outside edges it would just go from the corresponding edge instead.

  • oosyrag

    If you run into a filled grid object, stop loop and move the selection to that object. Otherwise, if no valid spots were encountered, nothing happens.

    yeah that way is the "lots of checks" I was originally thinking. but it gets even more complicated in the case where nothing happens. You'd always want something to happen, so if there were only 1 in a particular column and you hit UP, you'd want to go the nearest up piece to the left or right.

  • Lots of checks are relative, as the loop would be a single event. And you can add additional loops for neighboring columns or rows.

    Either way you'll need to clearly define the order of priority about where your selection would go. And if its actually random left or right you'll need to define that as well. Then you can put it together.

  • oosyrag oh I didn't mean lots of checks as in performance issue.. I just meant writing complex code in Construct Event script.

    yeah I might be over thinking it.. having the player just navigating to an empty grid tile might be okay looking... I think popping right to the " filled piece" might be more work than it's worth.

  • It's really not that complex...

    + Keyboard: On ↑ pressed
    + System: Repeat 4 times
    + select: Is overlapping grid at offset (0, -128×(LoopIndex+1))
    + grid: Is filled
    -> select: Set position to grid (image point 0)
    -> System: Stop loop

    This is all you need to skip empty spaces.

    If you want to hop over a row, you change the offset amount by -128 or +128 and repeat the same thing.


  • oosyrag oh that's really cool.. I've never used Overlap at Offset.. I like that approach thanks!

  • oosyrag I've also never used Stop Loop, will that break out of a sub event?

    i.e. where do I put code that never passed and executed the stop loop so I know to check the next column?

    however it does seem like it could get complex if I had something like this:

    if I had G6 selected and hit LEFT ARROW I'd want to pop to D4. you'd have to write code that kept checking columns.

  • In the following event (you should make them both subevents of the button trigger), you can make an Else event. The Else event will only run if the previous event did NOT run (meet conditions). So if none of the spots checked in the first loop were valid, stop loop would never have run, and the following event will run. If the first loop did execute, then the else event would not.

    But again, you have to define clearly in what order you want to check available positions. For example, why would it go to D4 and not C5? Would it go to F2 if there was a spot there when you pressed left?

    Don't worry too much about checking X amount of columns, loops can do wonders :)

    Edit: Also where would it go if there was a spot at F5 and F7?

  • oosyrag either F5 or F7 would be okay looking. but that's what I'm saying the rules are somewhat complex.

    if you started at B4 and pressed RIGHT, if there is no piece immediately to the right, then you would search outward in a radius from there for the next closest piece.

    You would not want to move all the way to G4 instead you'd probably want to go to D3

    EDIT: actually not a radius since you wouldn't want to go up to B3...maybe a half radius?

  • oosyrag it's all moot anyhow since I don't think I am going this direction anymore, it's not really worth it - and I'm sure there are scenarios I haven't thought of yet. But thanks for you your help on this! I very much appreciate it.

  • Updated again, this time filtering by angle, and then picking nearest. Works pretty well, until you run into a situation like the screenshots below where the red circled positions can never be reached.


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