How do I swap the places of two values in an array?

Get help using Construct 2

Post » Thu Mar 02, 2017 9:27 am

Hello, I've got a problem I can't quite figure out with my inventory system. I looked in the FAQ sticky and didn't find an answer to my issue. Basically, I'm using an array to store 2 values: The slot it is in, and its item ID. I'd like to be able to swap that second value with another elsewhere in the array when the player clicks on the item in their inventory and drags it to another spot. It's kind of hard to describe what I want to happen in words so here are some doodles:

Image

Here's a representation of the array. The first row is the item slot, the second row is the item's ID. Each column together makes one inventory slot.

Image

The player clicks and drags an item from one slot to another in the game (represented by picking it up from the slot and attaching it to the mouse) and lets go.

Image

As a result, the two values swap places on the array. In the game this is represented by the two items swapping slots.

The problem is that I don't know how to do that in the code. I would greatly appreciate the help because this has proven to be a real headscratcher for me.
Last edited by CJacobsSA on Thu Mar 02, 2017 10:03 pm, edited 2 times in total.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Thu Mar 02, 2017 1:37 pm

Using your example, store the 3 to a local variable, replace it with the 6, then store the 3 at the old 6 location.
ImageImageImage
B
71
S
21
G
232
Posts: 3,696
Reputation: 129,868

Post » Thu Mar 02, 2017 6:06 pm

Ah, I hadn't thought of that! I can see how to do it now that you've mentioned it! Thank you! :D
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Thu Mar 02, 2017 9:10 pm

edit: moved my question to a new thread because it's a bit more in-depth than this one.
B
18
S
9
Posts: 103
Reputation: 1,817


Return to How do I....?

Who is online

Users browsing this forum: JDebstup and 11 guests