2 Replies - 349 Views - Last Post: 15 October 2012 - 02:39 PM Rate Topic: -----

#1 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Items not adding correctly.

Posted 14 October 2012 - 02:51 PM

Hey
Here is the code that I am using to add items to an 'inventory'. It is a fixed length of 35 (the array):
Dim inventory(35) As InventoryItem

The code for InventoryItem is as follows:
Public Class InventoryItem
    Public Shared amount As Integer
    Public Shared type As String
    Public Property Amnt() As Integer
        Get
            Return amount
        End Get
        Set(ByVal value As Integer)
            amount = value
        End Set
    End Property
    Public Property ItemType() As String
        Get
            Return type
        End Get
        Set(ByVal value As String)
            type = value
        End Set
    End Property

End Class

All I wanted it to be was just a container. Things seem better in classes.
Here is where my issue lies. When I break a 'block' it runs a subprocedure called AddToInventory:

 Sub addToInventory(ByVal type As String)
        Debug.Write(type)
        If Not type = "air" Then
            For i As Integer = 0 To inventory.Count - 1
              
                If inventory(i).ItemType = "" Then
                    inventory(i).ItemType = type
                    inventory(i).Amnt += 1
                    Exit For
                ElseIf inventory(i).ItemType = type And inventory(i).Amnt < 64 Then
                    inventory(i).Amnt += 1
                    Debug.WriteLine(inventory(i).ItemType & inventory(i).Amnt & " , " & i)

                    Exit For
                End If

            Next
        End If

    End Sub

The issue with this code is: when I break a block, it adds to the inventory fine. But afterwards, only blocks of that type will add to the inventory. Nothing else will. I'll add some sample output:
stonestonestone2 , 0
stonestone3 , 0
stonestone4 , 0
stonestone5 , 0
stonestone6 , 0
dirtdirt


Hopefully someone can help.
Thanks and regards

Is This A Good Question/Topic? 0
  • +

Replies To: Items not adding correctly.

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: Items not adding correctly.

Posted 14 October 2012 - 06:10 PM

For some reason the second if, where it checks for an empty string, bothers me. Since I couldn't see any other issues, I decided to just read it out to you, so you tell me if you see something wrong. It's hard for me to understand the logic you're aiming for because it's your application, and given it's a game, I'm certain I'll have more issues recreating the environment than if I just read it out to you this way haha. So let me know Dimitri if you see anything! Despite though I'm still going to work on an environment where I can test your logic. See what happens, just will take time.


Edit: Never mind I read it one more time and suddenly everything seems clear as to what you are doing lol. Why not write a function that returns a boolean checking if the item already exists in the inventory, and is also not a full stack?

Public Function CheckForItem() as Boolean
For each ItemInInventory as Item in Inventory
If ItemInInventory.Type = type then
if ItemInInventory.Amnt <= 63 then
return True
exit for
end if
end if
Loop

return false

End Function



Then you can proceed through your logic. I have a feeling though that your problem does not lie within your code here, but maybe elsewhere. Have any other code you're willing to share that is somehow connected?

This post has been edited by trevster344: 14 October 2012 - 06:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Items not adding correctly.

Posted 15 October 2012 - 02:39 PM

Thanks a lot. I'll work this into my code now! If anything comes up I'll post it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1