0 Favourites

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

  • 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:

    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.

    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.

    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.

  • Using your example, store the 3 to a local variable, replace it with the 6, then store the 3 at the old 6 location.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Ah, I hadn't thought of that! I can see how to do it now that you've mentioned it! Thank you!

  • edit: moved my question to a new thread because it's a bit more in-depth than this one.

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