//So I downloaded an Item Stack resource to use for stacking items... I was wondering how...
//I can drop more than one item and how I can use itemAdd() to buy more than one of the same...
//item through the trader in my game.
//The code I got for item stacking is here.
obj
Items
var/canStack=0
verb/Get()
set src in oview(1)
usr.itemAdd(src)
verb/Drop()
set src in usr
usr.itemDrop(src)
mob/proc
itemDrop(obj/Items/o)
if(o.Firm != 1) // The o.Firm is because I didn't want to allow people to pick up every obj they see.
if(o.canStack && o.contents.len)
var/obj/Items/theItem=pick(o.contents)
theItem.loc=src.loc
if(o.contents.len)
o.suffix="x[o.contents.len+1]"
else
o.suffix="x1"
else
o.loc=src.loc
o.suffix=""
else return 1
itemAdd(obj/Items/o)
if(o.Firm != 1)
var/list/items=list()
for(var/obj/i in src.contents)
items+=i.type
if(o.type in items) //if a similer item is found in the contents
if(o.canStack)
var/obj/Items/theItem
for(var/obj/i in src.contents) if(i.type == o.type) theItem=i
if(theItem)
theItem.contents+=o
theItem.suffix="x[theItem.contents.len+1]"
else
src.contents+=o
o.suffix="x1"
else
src.contents+=o
else
src.contents+=o
o.suffix="[o.canStack ? "x1" : "[o.suffix]"]"
else return 1
mob
Trader
icon = 'Trader.dmi'
verb
Trade()
set category = null
set src in oview(1)
start
switch(input(usr,"","") in list("Buy","Sell","Prices","Cancel"))
if("Sell")
var/obj/z = input("What would you wish to Sell?") as obj in usr.contents
var/x = alert("I can give you [z.Value] for that.","Shop","Sure","No")
if (x == "Sure")
usr.Money += z.Value
usr << "You sold [z] for [z.Value]"
usr.contents -= z
shop.Add(z)
if (x == "No")
goto start
if ("Buy")
var/obj/Items/B=input("What item would you like to buy?","Buying") as null|anything in shop
var/obj/Items/b = (input("How much do you want to buy?","Quantity Amount") as num)
//I need to know how to multiply b with B for money and also how to apply it to itemAdd()
usr.Money -= B.Value
usr.itemAdd(B)
usr << "You bought [B] for [B.Value]"
if ("Prices")
var/obj/G=input("What item would you like to examine?","Prices") as null|anything in shop
usr << "The [G] is worth [G.Value]"
var/list/shop = list(new /obj/Items/Ruby, new /obj/Items/Apple, new/obj/Items/Loaf_of_Bread, new /obj/Items/Coffee, new /obj/Items/Paper, new /obj/Items/Pack_of_Cigarettes,
new /obj/Items/Lighter, new /obj/Items/Beer/Colers_Green_Ale, new /obj/Items/Beer/Blue_River_Beer)
//If anyone can help it would be greatly appreciated. Thanks in advance.
</b>
Problem description: I want to be able to buy more than one of the same item through the trader using item stacking and I want to be able to drop more than one of the same item.
I've rewritten the itemAdd proc to give you an example. Notice how it's a lot easier to follow, the code doesn't get indented so much, and we even removed some redundant code:
Now we can tackle the trader mob. You were using what we call an anti-pattern. Anti-patterns are common ways to try and solve a problem that seem like good solutions, but lead to bigger problem. The anti-pattern here was putting everything into one verb and using goto for control flow. Using goto is a sure sign that you should restructure your code. In my entire time programming I've never had to use a goto because something else wouldn't work better.
Here I've factored out the buy, sell, and price behaviors into their own procs. When you talk to the trader, he calls the trade_menu() proc. This farms out the user response into the trade_buy(), trade_sell(), and trade_prices() procs, which can then call trade_menu() again when they're done. Notice how everything is a lot easier to follow now that it's been separated out and structured?
From here you could also add dropping and selling more than one item. Just ask the user how many they want to drop or sell, like we did in the trade_buy() proc. I would also suggest adding an itemRemove() proc that handles unstacking, and changing itemDrop() to use that proc: