This tutorial touches up on the shop system. It adds a multiple shop item cost system, makes it possible to have items that are for free, and adds a nice development feature for editing shops. I’ll be adding just one extra one shop item cost, but adding more will be very easy to do after this tutorial.
Let’s start out on the server!
Go to modConstants and look for “Public Const MAX_SHOPS As Long = 50”
Underneath it, put there “Public Const MAX_SHOP_ITEM_COSTS as Long = 2”
Go to modTypes, and look for “Private Type TradeItemRec”
You should see something like this
costitem As Long
costvalue As Long
Replace it with this.
costitem(1 To MAX_SHOP_ITEM_COSTS) As Long
costvalue(1 To MAX_SHOP_ITEM_COSTS) As Long
Now go to the sub “Sub HandleBuyItem”
Replace “Dim itemamount As Long” with “Dim itemamount(1 to MAX_SHOP_ITEM_COSTS) As Long”
and add “Dim i as long” underneath it.
Replace
' check has the cost item
itemamount = HasItem(index, .costitem)
If itemamount = 0 Or itemamount < .costvalue Then
PlayerMsg index, "You do not have enough to buy this item.", BrightRed
ResetShopAction index
Exit Sub
End If
with
' check has the cost item
For i = 1 To MAX_SHOP_ITEM_COSTS
' Check if the cost item is bigger than 0
If .costitem(i) > 0 Then
itemamount(i) = HasItem(index, .costitem(i))
If itemamount(i) = 0 Or itemamount(i) < .costvalue(i) Then
PlayerMsg index, "You do not have enough to buy this item.", BrightRed
ResetShopAction index
Exit Sub
End If
End If
Next
and replace
TakeInvItem index, .costitem, .costvalue
GiveInvItem index, .Item, .ItemValue
with
' Take all the items!
For i = 1 To MAX_SHOP_ITEM_COSTS
' Check if the cost item is bigger than 0
If .costitem(i) > 0 Then
TakeInvItem index, .costitem(i), .costvalue(i)
End If
Next
' Give the item
GiveInvItem index, .Item, .ItemValue
On to the client!
Go to modConstants and look for “Public Const MAX_SHOPS As Long = 50”
Underneath it, put there “Public Const MAX_SHOP_ITEM_COSTS as Long = 2”
Go to modTypes, and look for “Private Type TradeItemRec”
You should see something like this
CostItem As Long
CostValue As Long
Replace it with this.
CostItem(1 To MAX_SHOP_ITEM_COSTS) As Long
CostValue(1 To MAX_SHOP_ITEM_COSTS) As Long
Now go to frmEditor_Shop and copy these form objects
and paste them right underneath the originals. When it asks if you want to add a control array, hit yes.
Now go to “ShopEditorInit” and add “Dim x As Long” just underneath “Dim i As Long”
replace
frmEditor_Shop.cmbCostItem.Clear
frmEditor_Shop.cmbCostItem.AddItem "None"
with
For x = 0 To MAX_SHOP_ITEM_COSTS - 1
frmEditor_Shop.cmbCostItem(x).Clear
frmEditor_Shop.cmbCostItem(x).AddItem "None"
Next
replace
For i = 1 To MAX_ITEMS
frmEditor_Shop.cmbItem.AddItem i & ": " & Trim$(Item(i).Name)
frmEditor_Shop.cmbCostItem.AddItem i & ": " & Trim$(Item(i).Name)
Next
with
For i = 1 To MAX_ITEMS
frmEditor_Shop.cmbItem.AddItem i & ": " & Trim$(Item(i).Name)
For x = 0 To MAX_SHOP_ITEM_COSTS - 1
frmEditor_Shop.cmbCostItem(x).AddItem i & ": " & Trim$(Item(i).Name)
Next
Next
and replace
frmEditor_Shop.cmbItem.ListIndex = 0
frmEditor_Shop.cmbCostItem.ListIndex = 0
with
frmEditor_Shop.cmbItem.ListIndex = 0
For x = 0 To MAX_SHOP_ITEM_COSTS - 1
frmEditor_Shop.cmbCostItem(x).ListIndex = 0
Next
Now go to “cmdUpdate_Click” and under “Dim Index As Long” put there “Dim i As Long”
replace
With Shop(EditorIndex).TradeItem(Index)
.Item = cmbItem.ListIndex
.ItemValue = Val(txtItemValue.text)
.CostItem = cmbCostItem.ListIndex
.CostValue = Val(txtCostValue.text)
End With
with
With Shop(EditorIndex).TradeItem(Index)
.Item = cmbItem.ListIndex
.ItemValue = Val(txtItemValue.text)
For i = 1 To MAX_SHOP_ITEM_COSTS
.CostItem(i) = cmbCostItem(i - 1).ListIndex
.CostValue(i) = Val(txtCostValue(i - 1).text)
Next
End With
go to “cmdDeleteTrade_Click” and under “Dim Index As Long” put there “Dim i As Long”
replace
With Shop(EditorIndex).TradeItem(Index)
.Item = 0
.ItemValue = 0
.CostItem = 0
.CostValue = 0
End With
with
With Shop(EditorIndex).TradeItem(Index)
.Item = 0
.ItemValue = 0
For i = 1 To MAX_SHOP_ITEM_COSTS
.CostItem(i) = 0
.CostValue(i) = 0
Next
End With
now go to “UpdateShopTrade” and under “Dim i As Long” put there “Dim x As Long”, “Dim Text As String”, and “Dim CostText As String”
replace
For i = 1 To MAX_TRADES
With Shop(EditorIndex).TradeItem(i)
' if none, show as none
If .Item = 0 And .CostItem = 0 Then
frmEditor_Shop.lstTradeItem.AddItem "Empty Trade Slot"
Else
frmEditor_Shop.lstTradeItem.AddItem i & ": " & .ItemValue & "x " & Trim$(Item(.Item).Name) & " for " & .CostValue & "x " & Trim$(Item(.CostItem).Name)
End If
End With
Next
with
For i = 1 To MAX_TRADES
CostText = vbNullString
With Shop(EditorIndex).TradeItem(i)
' if none, show as none
If .Item = 0 Then
Text = "Empty Trade Slot"
Else
Text = i & ": " & .ItemValue & "x " & Trim$(Item(.Item).Name) & " for "
For x = 1 To MAX_SHOP_ITEM_COSTS
If .CostItem(x) > 0 Then
CostText = CostText & .CostValue(x) & "x " & Trim$(Item(.CostItem(x)).Name)
End If
Next
if CostText = vbNullString Then CostText = " free"
End If
End With
frmEditor_Shop.lstTradeItem.AddItem Text & CostText
Next
now go to “picShopItems_MouseDown” and under “Dim shopItem As Long” put there “Dim i As Long” and “Dim CostText As String”
replace
Case 0 ' no action, give cost
With Shop(InShop).TradeItem(shopItem)
AddText "You can buy this item for " & .CostValue & " " & Trim$(Item(.CostItem).Name) & ".", White
End With
with
Case 0 ' no action, give cost
With Shop(InShop).TradeItem(shopItem)
For i = 1 To MAX_SHOP_ITEM_COSTS
If .CostItem(i) > 0 Then
CostText = CostText & " " & .CostValue(i) & " " & Trim$(Item(.CostItem(i)).Name)
End If
Next
If CostText = vbNullString Then CostText = " free"
CostText = CostText & "."
AddText "You can buy this item for" & CostText, White
End With
And lastly, we’re going to be adding in a handy tool for editing shops. On frmEditor_Shop, double click on lstTradeItem and paste this code there.
Private Sub lstTradeItem_Click()
Dim i As Long
With Me
.cmbItem.ListIndex = Shop(EditorIndex).TradeItem(lstTradeItem.ListIndex + 1).Item
.txtItemValue.Text = Shop(EditorIndex).TradeItem(lstTradeItem.ListIndex + 1).ItemValue
For i = 1 To MAX_SHOP_ITEM_COSTS
.cmbCostItem(i - 1).ListIndex = Shop(EditorIndex).TradeItem(lstTradeItem.ListIndex + 1).CostItem(i)
.txtCostValue(i - 1).Text = Shop(EditorIndex).TradeItem(lstTradeItem.ListIndex + 1).CostValue(i)
Next
End With
End Sub
Compile your server and client, and enjoy!