Problem Handling Inventory System
Hey. I'm making a mobile rpg. Currently, when my player walks over an item I want him to pick it up. Simple right? However, I've been having a lot more trouble with this than I thought I would.
I've having difficulty handling how to tell the item to go into the first unused slot in the inventory. I have attributes for each inventory slot that saves the item ID when the player walks over it. Any suggestions on how to handle say, if slot 1 has item then check slot 2 and if it has item and then check slot 3 and if does not have item give that slot the pickedup item, etc.?
I tried setting up a basic If Then statement saying If invSlot1 = 0 (no item) then add item, else if invSlot2 = 0 add item, etc. but it was extremely buggy and didn't work as planned. Please leave some ideas I could try, thanks!
Comments
A counter might work. Rather than checking what's open, assume what's open.
So for any function that picks up an item, have an integer attribute game.Slot = game.Slot+1, and any function which consumes/equips/drops an item have Game.Slot = game.Slot-1.
If your inventory is using a table, and you need to change what column or row is being referenced, you can use game.Slot to provide that number.
I would use an integer attribute to keep track of the next available slot. The attribute should start at one and each time an item is collected, you then increase the attribute by one. So if you've picked up 5 items, game.nextSlot would be 6. If you drop an item, decrease the attribute by one. Always add the item to game.nextSlot however you're storing the items (e.g. in a table with row=game.nextSlot).
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
I tried this, and it did work. However, say slot 1 is full, slot 2 is empty, and slot 3 is full, it would put the next time in slot 4 rather than slot 2. I'm having problems trying to figure out how it would check each slot and find the first open one and fill it. Thanks!
This bit of code will always pick the first open slot.
Rule
All
If overlaps and collide with object
Rule inside
Game.slot.1=0
Put into 1
Otherwise
Rule
Game.slot.2=0
Put into slot 2
Otherwise
Rule
Game.slot.3=0
Put into slot 3
Otherwise
And so on
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
See as I said I tried this as well, but it seemed to glitch sometimes filling multiple slots,
It shouldn't as it's pretty straight forward logic. I use similar code for multitouch which is way more complex and it works fine. Show a screenshot of your code as you must have some other logic issues.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Here is an example. works perfect.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Yours does indeed work perfect, but how come when I do the same exact thing it doesn't behave the same?
http://imgur.com/Gdkxc6l Heres an example that handles 3 inventory spots
When running multiple tests, when my player collides with the dropped item actor it gives different results every time. Sometimes it will fill the first slot only, other times it will fill the first and third, and occasionally it will fill the first two or even all three. Note that this is when picking up only one item and it shouldn't function like that, it should only fill one inventory space.
I see what's happening. Look at my code, mine is different. You have the overlap and collide on every rule. That will cause a loop. Wrap all the rules in one rule that has the overlap and collide and the rest should just have the attribute condition.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Wow, I can't believe I didn't try that.
However, now when I load the game the items on the ground instantly destroy themselves? Why is this happening even though my character hasn't collided with the item? I also tried to put the destroy action inside its own rule, but then that seems to cause the item to destroy before assigning it to the inventory spot
Make sure the first rule only has the overlap and collide condition and then put the first rule with the game.inventory =0 inside the do area. Make sure the other rules are in the otherwise of the inventory rules and not the otherwise of the overlap and collide rule. Look at my example it is very clear.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
I got it! Thank you a ton, I appreciate all the effort:) Have a good day / night!