Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, I've been working on a RPG game in VB 2010 and I've gotten to the point where I am adding items and giving them properties but I'm a bit confused and stuck. I currently have a form with 28 picture boxes and 28 'inventory settings & amounts' (inv1, inv2, etc) and on loading the inventory form the pictures are updated accordingly to the setting and a number below it to show the amount you currently have of the item. Then I want to be able to click the items and it "equips" it which will then show up on a different form called the 'Character Screen'.

Here is some of my current coding for it:

Inventory updating on load
If My.Settings.inv4 = "fvisage" Then
    inv4.Image = My.Resources.fVisage
    inve4.Text = My.Settings.fvisage
ElseIf My.Settings.inv4 = "draBody" Then
    inv4.Image = My.Resources.draBody
    inve4.Text = My.Settings.drabody
ElseIf My.Settings.inv4 = "dralegs" Then
    inv4.Image = My.Resources.draLegs
    inve4.Text = My.Settings.dralegs
ElseIf My.Settings.inv4 = "draboots" Then
    inv4.Image = My.Resources.draBoots
    inve4.Text = My.Settings.draboots
ElseIf My.Settings.inv4 = "drahelm" Then
    inv4.Image = My.Resources.draHelm
    inve4.Text = My.Settings.drahelm
ElseIf My.Settings.inv4 = "clothbody" Then
    inv4.Image = My.Resources.clothBody
    inve4.Text = My.Settings.clothbody
ElseIf My.Settings.inv4 = "clothlegs" Then
    inv4.Image = My.Resources.clothLegs
    inve4.Text = My.Settings.clothlegs
ElseIf My.Settings.inv4 = "clothboots" Then
    inv4.Image = My.Resources.clothBoots
    inve4.Text = My.Settings.clothboots
ElseIf My.Settings.inv4 = "clothhat" Then
    inv4.Image = My.Resources.clothHelm
    inve4.Text = My.Settings.clothhat
ElseIf My.Settings.inv4 = "plaBody" Then
    inv4.Image = My.Resources.plaBody
    inve4.Text = My.Settings.plabody
ElseIf My.Settings.inv4 = "plalegs" Then
    inv4.Image = My.Resources.plaLegs
    inve4.Text = My.Settings.plalegs
ElseIf My.Settings.inv4 = "plaboots" Then
    inv4.Image = My.Resources.plaBoots
    inve4.Text = My.Settings.plaboots
ElseIf My.Settings.inv4 = "plahelm" Then
    inv4.Image = My.Resources.plaHelm
    inve4.Text = My.Settings.plahelm
ElseIf My.Settings.inv4 = "maiBody" Then
    inv4.Image = My.Resources.maiBody
    inve4.Text = My.Settings.maibody
ElseIf My.Settings.inv4 = "mailegs" Then
    inv4.Image = My.Resources.maiLegs
    inve4.Text = My.Settings.mailegs
...
...
...
...


Gaining item loot from a fight and it being stored in the first open inventory slot
If My.Settings.inv2 = "" Or My.Settings.inv2 = "fvisage" Then
                                   My.Settings.inv2 = "fvisage"
                                   My.Settings.fvisage = My.Settings.fvisage + 1
                                   MsgBox("You gained: " + fgold.ToString + " gold" & vbNewLine & "You gained: " + fExp.ToString + " experience." & vbNewLine & "You gained: " + fExp.ToString + " experience." & vbNewLine & "You've also found a Flaming Visage!", vbOKOnly, "Loot")

                               ElseIf My.Settings.inv3 = "" Or My.Settings.inv3 = "fvisage" Then
                                   My.Settings.inv3 = "fvisage"
                                   My.Settings.fvisage = My.Settings.fvisage + 1
                                   MsgBox("You gained: " + fgold.ToString + " gold" & vbNewLine & "You gained: " + fExp.ToString + " experience." & vbNewLine & "You've also found a Flaming Visage!", vbOKOnly, "Loot")

                               ElseIf My.Settings.inv4 = "" Or My.Settings.inv4 = "fvisage" Then
                                   My.Settings.inv4 = "fvisage"
                                   My.Settings.fvisage = My.Settings.fvisage + 1
                                   MsgBox("You gained: " + fgold.ToString + " gold" & vbNewLine & "You gained: " + fExp.ToString + " experience." & vbNewLine & "You've also found a Flaming Visage!", vbOKOnly, "Loot")

                               ElseIf My.Settings.inv5 = "" Or My.Settings.inv5 = "fvisage" Then
                                   My.Settings.inv5 = "fvisage"
                                   My.Settings.fvisage = My.Settings.fvisage + 1
                                   MsgBox("You gained: " + fgold.ToString + " gold" & vbNewLine & "You gained: " + fExp.ToString + " experience." & vbNewLine & "You've also found a Flaming Visage!", vbOKOnly, "Loot")

                               ...
                               ...
                               ...
                               ...


This is my "equip" method that occurs when clicking one of the picture boxes
it is in a switch case that switches to, in this perticular case, what is stored in my.settings.inv2
Case clothbody
                If My.Settings.clothbody - 1 = 0 Then
                    If My.Settings.body = "" Then
                        inv2.Image = My.Resources.nothng
                        My.Settings.inv2 = ""
                        inve2.Text = "-"
                        chrScreen.body.Image = My.Resources.clothBody
                        My.Settings.armor = My.Settings.armor + 30
                        My.Settings.body = "clothbody"
                        My.Settings.clothbody = My.Settings.clothbody - 1
                    ElseIf My.Settings.body = "clothbody" Then
                    Else
                        Dim x As String
                        Dim mre As String
                        x = My.Settings.body
                        mre = "my.resources." & x
                        My.Settings.body = "clothbody"
                        If My.Settings.inv2 = x Then
                            inv2.Image = My.Resources.nothng
                            inve2.Text = "-"
                        ElseIf My.Settings.inv3 = x Then
                            inv3.Image.Tag = mre
                            inv3.Image = inv3.Image.Tag
                            inve3.Text = "-"
                        ElseIf My.Settings.inv4 = x Then
                            inv4.Image = My.Resources.nothng
                            inve4.Text = "-"
                        ElseIf My.Settings.inv5 = x Then
                            inv5.Image = My.Resources.nothng
                            inve5.Text = "-"
                        ElseIf My.Settings.inv6 = x Then
                            inv6.Image = My.Resources.nothng
                            inve6.Text = "-"
                        ElseIf My.Settings.inv7 = x Then
                            inv7.Image = My.Resources.nothng
                            inve7.Text = "-"
                        ElseIf My.Settings.inv8 = x Then
                            inv8.Image = My.Resources.nothng
                            inve8.Text = "-"
                        ElseIf My.Settings.inv9 = x Then
                            inv9.Image = My.Resources.nothng
                            inve9.Text = "-"
                        ElseIf My.Settings.inv10 = x Then
                            inv10.Image = My.Resources.nothng
                            inve10.Text = "-"
                        ElseIf My.Settings.inv11 = x Then
                            inv11.Image = My.Resources.nothng
                            inve11.Text = "-"
                        ElseIf My.Settings.inv12 = "" Then
                            inv12.Image = My.Resources.nothng
                            inve12.Text = "-"
                        ElseIf My.Settings.inv13 = x Then
                            inv13.Image = My.Resources.nothng
                            inve13.Text = "-"
                        ElseIf My.Settings.inv14 = x Then
                            inv14.Image = My.Resources.nothng
                            inve14.Text = "-"
                        ElseIf My.Settings.inv15 = x Then
                            inv15.Image = My.Resources.nothng
                            inve15.Text = "-"
                        ElseIf My.Settings.inv16 = x Then
                            inv16.Image = My.Resources.nothng
                            inve16.Text = "-"
                        ElseIf My.Settings.inv17 = x Then
                            inv17.Image = My.Resources.nothng
                            inve17.Text = "-"
                        ElseIf My.Settings.inv18 = x Then
                            inv18.Image = My.Resources.nothng
                            inve18.Text = "-"
                        ElseIf My.Settings.inv19 = x Then
                            inv19.Image = My.Resources.nothng
                            inve19.Text = "-"
                        ElseIf My.Settings.inv20 = x Then
                            inv20.Image = My.Resources.nothng
                            inve20.Text = "-"
                        End If
                        End If
                ElseIf My.Settings.clothbody = 0 Then
                        inv2.Image = My.Resources.nothng
                        My.Settings.inv2 = ""
                        inve2.Text = "-"
                Else
                        chrScreen.body.Image = My.Resources.clothBody
                        My.Settings.armor = My.Settings.armor + 30
                        My.Settings.clothbody = My.Settings.clothbody - 1
                        inve2.Text = My.Settings.clothbody
                        My.Settings.body = "clothbody"

                End If


The whole 'Elseif my.settings.inv2 = x then' thing was suppossed to be for in cases where you already have a item equipped in that slot but that seems to not work and I can't seem to grab the 'item' from the character screen and bring it back to the inventory screen without causing a problem.

Now, I know my code is very inefficient and long and probably overall unnecessary but I'm stuck and confused and have no idea what I should be doing. I tried creating a new class and making a function, that I'd be able to call on in a different form, to find the next available slot starting with inv2 (inv1 always has gold in it no matter what, even if you have 0 gold) but that didn't work cause I again didn't even know where to start.

I need to know the most efficient way of creating a inventory system and being able to equip and unequipped items, most efficient way of finding empty inventory slot and putting an item into it.

Thank you in advance and I hope you all have a great day. :)
Posted

1 solution

Your problem is a long if-then-else block, instead of having some indexed container of data. A change to switch statement won't really help. I explain one of the right approaches in a very universal form in my article "Dynamic Method Dispatcher, No more long switch statements!". Please see:
Dynamic Method Dispatcher[^].

—SA
 
Share this answer
 
Comments
Kenneth Haugland 14-Aug-12 14:37pm    
:laugh: Shamless self promotion, but still a 5! :)
Sergey Alexandrovich Kryukov 14-Aug-12 15:12pm    
It is not shameless. This article is already helped some inquirers, can help this one. This is the whole purpose of publishing articles. Thank you for voting.
--SA
Kenneth Haugland 14-Aug-12 15:19pm    
I know, was kidding, sorry. Im not nearly as funny as I think :)
Sergey Alexandrovich Kryukov 14-Aug-12 15:29pm    
No problem at all... :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900