How do I swap two items?

Not favoritedFavorited Favorited 0 favourites
  • 8 posts
From the Asset Store
Full game Construct 2 capx code source to post on Google Play
  • Hi I was changing my inventory system and I noticed that it could be better if I could swap the items' position on array by dragging them to another slot. They have Name, arrayID, Amount and Type

  • Temporarily store the slot id in a variable and also the details of the item you grabbed, in a separate array, then when the item is dropped onto an existing item, use the slot id of the slot where you are dropping and set all of that item's details to at stored id. Then for the item being placed use the dropped slot id to set the stored details.

    So for example, you picked up item at 6 and plan to drop it onto 2. Store in a variable '6'(itemid) and everything at 6,0 6,1 6,2 etc will be stored in a row of the new array. When the item is dropped onto slot 2, set var id(6),0 6,1 6,2 etc to the details of itemarray at dropped slot id(2),0 2,1 2,2 etc. Then set the dropped slot id(2),0 2,1 2,2 in item array to the corresponding values from the new array.

    Pick up item 6 - > store 6 in var and inv.at(6,y) in secondarray.at(0,y)

    Drop on item 2 - > set inv.at(6,y) to values inv.at(2,y) (swaps 2 to 6)

    set inv.at(2,y) to values secondarray.at(0,y) (swaps 6 to 2)

  • I was saying like, how I identify the item I dropped and the item that was originally standing

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • When you click an item you can grab its details from the itemid ready in case the user tries to swap. If its a drag and drop scenario then you could check if dropping and overlapping slot is not equal to the same itemid then perform a swap as above.

  • I still can't understand. Here's the file (I forgot to send it before, sorry)

    dropbox.com/scl/fi/odccu66cvyfuhe9uyqdky/Power-Gem-Fetcher.c3p

  • Think I got it working. Note I had to disable an event at line 199 not sure what that does right now but it was interfering with the swap. dropbox.com/scl/fi/5j1ivu7mujkfeur7kypkb/game_edited-3.c3p

  • Cool but one last question: The amount displaying text when the amount is 0 still show up as the previous amount. How do I fix it?

  • Event 254 controls the quantity text and there is a condition for don't update if the amount is 0. With event 199 disabled I think we can delete 199 and tweak 254 to display what you want if the amount is 0 i.e. display ""

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