How do I select different items with a gamepad

0 favourites
  • 11 posts
From the Asset Store
Adjusting the game screen for different resolutions (Letterbox scale)
  • Hi,

    I'm working on a new game and I'm a bit stumped.

    What I want to be able to do is select from a group of items/sprites using a gamepad and then drag and drop the item into a position.

    This is obviously easy to do using the drag and drop behaviour and using a mouse but I'm having no luck at all using a game pad, what I don't want to do is have a cursor that's controlled by the analog stick as this is slow and clunky.

    This is a capx of what I have so far working with the mouse. https://www.dropbox.com/s/z3lqh84m5196vyj/GamepadItemSelection.capx?dl=0

    Any help at all would be appreciated as this is really getting to me, ha!

  • First thing that comes to mind is have a global variable set for x/y coordinates. Upon selecting/picking up an object, set this variable to the object's x/y. Then have your gamepad input increment/decrement the variables, and set the object to the variable x/y every tick.

  • Hi,

    I'm not really sure what you mean on the above(I'm still quite new to construct).

    Could you expand a little bit?

    I've attached a better Capx.

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

    What I would like is for the yellow sprite called Selecter to snap to the nearest sprite.

    So when I hit the Left Arrow/Left bumper button on the keyboard it snaps to nearest sprite on the left and when I hit the Right Arrow/Right bumper button on the keyboard it snaps to nearest sprite on the right.

    Image: http://i.imgur.com/wsFHRwO.png

  • OK I get what you are trying to do.

    Here is an example.

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

    Basically you'll want a list of valid targets stored in an array. This array will contain the value you want to sort by, as well as the UID of each sprite, which allows you to pick the correct one you want to snap to.

    Then by using an incrementing variable with the array, you can look up which sprite is next or previous, and set your selector position to that sprite's position.

    To compact it more, you can use the array expression IndexOf to get the current position of your selector instead of storing it in a variable... but then you start sacrificing readability.

  • Hi, thanks! Wish I fully understood arrays.

    This works great, but what I want to do as well is once one of the blocks is selected(overlapped by the selector) I'd like to enable the 8 directional movement for that sprite so I can move it anywhere on the screen. Then when I hit left/right on the keyboard I'd like it select the sprite on the left/right of current position of the selector, rather than the sprite on the left/right of the original position, not sure if this can be done as they are stored in the array in a certain order? Any way to refresh the order of items in the array after I've moved one of the sprites?

    e.g.

    If I have the sprites set up as below, green is selected and I hit the right key it'll move along: Green > Blue > Pink > Yellow.

    Green -- Blue -- Pink -- Yellow

    If I select the green sprite and drag it across so it's between the pink and yellow sprites, when I hit the right arrow key it currently moves the selector to blue, I'd like it to move to Yellow. Does that make sense?

    Thanks

  • Hi,

    Ok, my friend who is working on this in a slightly different way has hit a bit of a wall and we can't seem to fix it.

    Can someone please take a look at this capx and tell us why the item selector will cycle right but not left? We thought we'd done what was needed to get that working...

    You'll need an xbox controller plugged in to test it.

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

    Again, any help at all much appreciated

  • Don't be intimidated by arrays! They are simply little spreadsheets with a bunch of tools you can use to manipulate them.

    The solution to update the array is simply to repeat the actions called on start of layout, with the simple addition of setting array width to 0 beforehand to erase the current array. So upon dropping a block, set array width to 0, then add each block's (new) x position and uid to the array again and sort by x position.

  • Thanks, that makes sense.

    I've implemented that, so it resets the array on a key press(for now N on keyboard and B on xbox controller).

    I'm still facing the same issue as before though where once I've moved a block to a different position and I hit left/right to select the next block it goes to the block that would have been on it's left/right in it's original position.

    How can I refresh the CurrentSelection variable so that it updates to the new position?

    Latest Capx: https://www.dropbox.com/s/24ma6wohueukg ... .capx?dl=0

    Thanks

  • Getting right into advanced array usage <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

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

    I've updated your capx with comments, let me know if there is anything you don't understand. The key points are that we want the cursor to follow the object based on its UID only instead of its position in the array (since that can change now chage), and also to update the CurrentSelection when the UID is in the "wrong" place after it's position in the array moves.

    I've also modified a few events to make them more compact, again with comments. Also want to emphasize there is nothing wrong with having more events - doing so can make a project more readable and easier to understand, especially if you were the one that put them together.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks a lot oosyrag that's brilliant!

    Certainly helping me to understand arrays a little better, it seems to work almost perfectly except if you move the first block(light green) into another position and then carry on cycling through, once it gets to the last one on the right and should go back to the first block on the left it goes back to the light green block(regardless of it's position) and then to the first on the left. Do you know why this is? Easy fix?

    This is only a problem going Left to Right as well, not the other way.

  • thanks for the capx..

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