Sign in to follow this  

Walking through the debugging process

This topic is 1184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this messy chunk of code here which isn't doing what I want it to yet. I have two Items of class Item, Rock : Item and Twig : Item. I have an inventory system which has 5 slots, each item will go in one slot and will stack if another item of the same type is picked up, like any inventory system. When I pick up an object, it goes into my inventory and shows up at the bottom. When I pick up a different type of item however, the item count will increase but will not move to the next inventory slot. So heres the code and ill describe it the best I can.

            for (int t = items.Count() - 1; t >= 0; t--)
            {
                if (player.rectangle.Intersects(items[t].ItemRectangle))
                {
                    for (int i = 0; i < player.PlayerInventory.itemSlots.Count(); i++)
                    {
                        if (player.PlayerInventory.itemSlots[i].IsSlotOpen == true)
                        {
                            player.PlayerInventory.itemSlots[i].item = items[t];
                            player.PlayerInventory.itemSlots[i].IsSlotOpen = false;
                            items[t].ItemPickedUp = true;
                            break;
                        }

Looping through the Items in my list (items) checking for a collision. Also looping through my inventory and checking if a slot if open. If it is, add that item to the open slot and close the slot. Set the itemPickedUp to true which is used later on.

                        else if (player.PlayerInventory.itemSlots[i].item is Twig && player.PlayerInventory.itemSlots[i].IsSlotOpen == false)
                        {
                            player.PlayerInventory.itemSlots[i].item.NumberOfItems += 1;
                            items[t].ItemPickedUp = true;
                            break;
                        }

                        else if (player.PlayerInventory.itemSlots[i].item is Rock && player.PlayerInventory.itemSlots[i].IsSlotOpen == false)
                        {
                            player.PlayerInventory.itemSlots[i].item.NumberOfItems += 1;
                            items[t].ItemPickedUp = true;
                            break;
                        }
                            
                        else
                        {
                            return null;
                        }

I think this is where my problem is. If the item in the slot is a twig and the slot is closed, add 1 to the quantity of the item. Set pickup to true. 

Now... if the item is a rock in the slot and the slot is closed, do the same. But this is not what is happening! No idea why :P 

                if (items[t].ItemPickedUp == true && items[t] is Twig)
                {
                    Twig pickedUpTwig = new Twig(items[t].Position, items[t].Texture, items[t].NumberOfItems);
                    items.RemoveAt(t);
                    return pickedUpTwig;
                }

                else if (items[t].ItemPickedUp == true && items[t] is Rock)
                {
                    Rock pickedUpRock = new Rock(items[t].Position, items[t].Texture, items[t].NumberOfItems);
                    items.RemoveAt(t);
                    return pickedUpRock;
                }

Final chunk of code. If the item is a twig and it is picked up, remove the twig from the list and add a twig to your inventory. Same with the rock.

 

The code is messy right now but I'm having trouble getting this to work. I might just have to stop staring at it lol.

Share this post


Link to post
Share on other sites

Try something like this:

            for (int t = items.Count() - 1; t >= 0; t--)
            {
                if (player.rectangle.Intersects(items[t].ItemRectangle))
                {
                    int bestSlot = -1;
                    for (int i = 0; i < player.PlayerInventory.itemSlots.Count(); i++)
                    {
                        if (player.PlayerInventory.itemSlots[i].IsSlotOpen == true)
                        {
                            if(bestSlot<0) {
                               bestSlot = i;
                             }
                        }
                        else if (player.PlayerInventory.itemSlots[i].IsSlotOpen == false)
                        {
                            if( (player.PlayerInventory.itemSlots[i].item is Twig && items[t] is Twig) ||
                                (player.PlayerInventory.itemSlots[i].item is Rock && items[t] is Rock)) {
                                bestSlot = i;
                            }
                        }
                   }

                   // add now
                   if(bestSlot>=0) {
                      if(player.PlayerInventory.itemSlots[bestSlot].IsSlotOpen == true) {
                            player.PlayerInventory.itemSlots[bestSlot].item = items[t];
                            player.PlayerInventory.itemSlots[bestSlot].IsSlotOpen = false;
                      } else {
                            player.PlayerInventory.itemSlots[bestSlot].item.NumberOfItems += 1;
                     }
                     items[t].ItemPickedUp = true;
                  }

Edited by Ashaman73

Share this post


Link to post
Share on other sites

I don't like the use of "is" there.

 

  1. You could compare types in most languages (e.g. Python "if(type(a) == type(b)) {...}" Java "if(a.getClass() == b.getClass()){...}")
  2. You could add a type field (int/string) to the base item class ("if(a.itemType == b.itemType))") (this also works if you use say the Rock class for multiple things, e.g. different sized rocks without having to create a class for each, you could even define them in external datafiles).
Edited by SyncViews

Share this post


Link to post
Share on other sites

Because your having to do that for every single item you add, which in my opinion is bad for a few reasons.

  1. When you have 200 things in your game, you now have a 200 line conditional
  2. You will likely find other places similar to 1.
  3. When you add, remove or change some type, its the sort of things you completely forget about, then wonder why it is not working

If you just use the class type directly (like the type(a) == type(b) in Python), or if you use your own field, then conditionals like that just automatically do the right thing.

Share this post


Link to post
Share on other sites

This topic is 1184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this