GMS 2 (Solved) Help using item from inventory

K

Karlabos

Guest
Hi.

I am trying to code my inventory stuff.

So far I have an inventory with 'mymaxitems' slots and the latest slot which actually contains an item is the number (mytotalitems)th. It turns out every item of the inventory is an instance of obj_Item, for which then I assign the sprite and the other properties as they appear in the game.

I am trying to code the item usage, and part of the script is as follows:

for(j=localitemindex;j<=localobjtocheck.mytotalitems;j+=1){
if localobjtocheck<localobjtocheck.mytotalitems{ //i had to include this so that j+1 part didn't return error
localobjtocheck.item[j] = localobjtocheck.item[j+1];
localobjtocheck.item[j].name = localobjtocheck.item[j+1].name;
localobjtocheck.item[j].sprite_index = localobjtocheck.item[j+1].sprite_index;
}
}
localobjtocheck.item[localobjtocheck.mytotalitems] = obj_Empty;
localobjtocheck.item[localobjtocheck.mytotalitems].name = "";
localobjtocheck.mytotalitems-=1;

So in my view the for part should vary j from the item to be used (whose number is localitemindex) until the last slot containing an item (localobjtocheck.mytotalitems, I have to call object from other object because it's not the one executing the script)
Then while j varies in this for, every slot should receive the properties of the next one, so if I'm using the 2nd item from an invetory with 4 items, then 1 stays as it is, 2 becomes 3, 3 becomes 4 and the for ends. So I
end up with an item sequence 1,3,4,4 of kinds.

For the part of the code after the code, it's only the last object vanishing from the inventory, hence staying with a sequence 1,3,4. So with this item 2 should have vanished from the inventory and all the other items should have walked to the prior position, right?


However it's not what happens... What happens is that every item of the inventory disappears!


And after that, if I get another item from the room, then instead of appearing only one, the whole inventory is filled of items of that kind, up until mymaxitems... This last part happens only if I have already used an item from the inventory, before that the colliding/getting an item part is working fine, so I suspect the error must be here...

Any idea on why this happens?
 

2Dcube

Member
If I understand what you're doing, you are using an array and shifting all the items when one of them in between gets used?

You might consider learning how to work with the "list" data structure.
Lists are useful, because when you remove an item in the middle they automatically become shorter. You also won't have to keep a "max items" value, as you can check the length of a list directly.

It might take some time to learn it, but once you do it makes everything much easier. No need to keep track of anything, it should just work.
 

CloseRange

Member
personally i disagree only because I never liked lists and souly use array's in all my projects (dumb as it may be)

Your code looks perfectly fine (there are a couple things that I would change to make it neater / proper but they don't change the code much)
The question I would have is how do you use an item? is it on a single key press?
Make sure the code above only happens once and only once
If you have this occuring in a 'key down' event then it will happen every frame until you release, causing every single item to be used.

at the end of the code try putting:
Code:
show_debug_message(localobjtocheck.mytotalitems);
and making sure the code only happens once
 
K

Karlabos

Guest
Thanks for the replies, but I think I found the error.

If anyone is interested, it turns out re-assigning the id of the objects item[j] isn't a good idea.
The line localobjtocheck.item[j] = localobjtocheck.item[j+1] makes it so a lot of items on the inventory share the same id, so whatever code called by an object obj_Item will happen to all at once.

Instead I tried storing on item[j] only the name of the item and working with an if depending on the name on a separate location to assign the other variables such as sprite, properties, etc. And now it works fine.

Make sure the code above only happens once and only once
Yep. it was being executed only once, because right after I had instance_destroy() (being called by the obj_Item the player collided with) so, the player got the item, it made what it was supposed to do, it re-organized the inventory, and then vanished (destroyed itself)
However since a lot of items on the inventory were the same (because I was assigning ids instead of only name and sprite) then if one of them destroyed itself all the others also did

If I understand what you're doing, you are using an array and shifting all the items when one of them in between gets used?

You might consider learning how to work with the "list" data structure.
L
Oh nice, I didn't know there was such thing. i'll probably start using that once I need something similar
 
Top