Drag / drop - Replace table value when dropped.
Hello,
Im still making the ultimate inventory to suit my game.
Ive been trying the last hours to make it "sortable" so that the player if they like can change places of the items in the inventory.
Lets say that slot 1,2,3,4 is different potions.
1 = health
2 = mana
3 = strong health
4 = strong mana
But as a player maybe he wants to have the order 1,3,2,4 of the potions.
Why as ask for help is because I managed to make the slot follow the mouse / touch when touch is pressed of the slot.
BUT I cannot manage to make the table row + collum of the slot change place with the item it collides with.
So If I touch press the slot nr 1 and drag it to the slot nr 3 and release the touch they should change place (table value should change).
Well more to say I dont know how to swap table values. Is there a good way to do this?
I tried to make it by storing the table value in attributes and then clear it / add it to the row/collum etc but that did not work.
Would love if someone with experince of this could just explain how it can be done.
My inventory is set by 30 slots / 30 rows + 2 collums. 1 collum hold the item id and one how many (stacksize).
All I need is how to make it swap values for its own row/collum to the item it swaps with and back.
I cannot explain how I mean but you might recon the function as drag and drop / swap items spots in an inventory.
Dont need anything advanced made for me only explained how to swap values easiest way :P
regards,
Comments
The algorithm for swapping two values (A and is:
Store A in temporary variable
Change B to A
Change A to temporary variable
You can do this by storing one table value in an attribute using a Change Attribute behavior and then using two Change Table Value behaviors to make the other two changes.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Thank you , I will try this out!
Do you know if its possible to make the actor that is pressed moved to the top layer of the scene? So when I move it to the other slot its above it? Because now some of the slots are under the scene layer so it looks like its under it when draged to the slot :P
Did not work, I stored the value in the slot self attribute when touch is pressed. That part worked. BUT I guess swaping values on the same actor with different table value is harder?
slot 1 swap for slot 2 value. Cannot figure it out haha
No, it's not harder. But make sure that you are allowing the behaviors to complete. If you do something like this:
If Touch is Pressed
Change Attribute
Change Table Value
Change Table Value
and the person taps/clicks briefly, the rule may not run long enough to process all of the behaviors. Instead, try something like this:
If Touch is Pressed
Timer After 0 seconds (run to completion CHECKED)
Change Attribute
Change Table Value
Change Table Value
You can also just change a boolean value from false to true when touch is pressed and then have a separate rule that changes the attribute/table values when that boolean is true. You'd then need to reset the boolean to false after a short while (e.g. 0.1 seconds). Two ways to do the same thing.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
I understand that part , I guess so far I know how that works.
But the issue I have is how will it know what values the other item has?
I store it self row/collum but how does it know what values to set of the other slots values.
Gets wierd for me since its the same actor but different values.
If it would be 2 different actors sure.. but now its same actor with rules set in the same actor etc. I have hard time to explain what the issue is but yeah.. Its more that I dont know how to determen the swap values.
Instead of doing all that you should make a grid and use snap to grid. The when you grab and object you store it's grid spot and when you overlap with another slot you swap grid spots. You shouldn't have to worry about the table if you are using my method as it will always access the right value of the object in it.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
But that metod will make the inventory a bit messy since the storage function works with the table. Lets say that there is 20 slots, slot 1 - 20 and if the player move the grid function as you say then the slot IDs will be reranged wich makes the inventory messy because if they pick up an item later on it wont put it in order of "empty" slot.
Because the next empty slot lets say nr 10 maybe is on position 20.. so it will jump 10 slots and place it there if the grid function is set.
Thats why I try to make it so when player drop the slot on another slot they swap information. That way the slot ID never swap place only the value of the tables.
But still after hour of trying cannot manage to swap the values
Let me look at my template and write some code.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS