How do I select different items with a gamepad

Get help using Construct 2

Post » Thu Jul 14, 2016 8:54 pm

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!
B
10
S
1
Posts: 9
Reputation: 485

Post » Thu Jul 14, 2016 9:01 pm

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.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Tue Jul 19, 2016 11:11 pm

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
B
10
S
1
Posts: 9
Reputation: 485

Post » Tue Jul 19, 2016 11:57 pm

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.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Wed Jul 20, 2016 2:05 pm

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
B
10
S
1
Posts: 9
Reputation: 485

Post » Thu Jul 21, 2016 10:21 pm

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
B
10
S
1
Posts: 9
Reputation: 485

Post » Fri Jul 22, 2016 12:09 am

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.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Tue Jul 26, 2016 11:27 pm

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
B
10
S
1
Posts: 9
Reputation: 485

Post » Thu Jul 28, 2016 5:58 am

Getting right into advanced array usage ;)

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.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Sun Aug 07, 2016 9:20 pm

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.
B
10
S
1
Posts: 9
Reputation: 485

Next

Return to How do I....?

Who is online

Users browsing this forum: Bro7hers, ezgaming420, Yahoo [Bot] and 10 guests