ID:145452
 
Code:
obj
var
Shop = null
list/wares = new(obj,obj.value)

obj/WeaponMan
icon = 'pics/person.dmi'
icon_state = "Merchant"
density = 1
Shop = 1
wares = newlist(/obj/equipable/armor/clothes,10)
Click()
if(Shop==null)
return
else
switch(input("Would you like to Buy or Sell?")in list("Buy","Sell","Cancel"))
if("Buy")
Buy()
if("Sell")
Sell()
if("Cancel")
return

proc/Buy(var/o in wares(onj,obj.value))
set src in oview(2)
switch(input("[src]: Hello, what would you like?")in list(wares,"Cancel"))
if("Cancel")
return
else
if(usr.Gold >= wares.value)
usr.Gold -= wares.value
new/wares(usr)

proc/Sell(var/obj/O in usr
set src in oview(2)
switch(alert("[src]: Are you sure you want to sell [O] for [O.value] gold?","Selling","Yes","No"))
if("Yes")
usr.Gold += O.value //
del(O)


Problem description:

I'm trying to make it so if i set a NPC's var Shop to 1 then, if clicked on, it brings up the shopping procs. I also want the wares that the NPC is to be selling defined in the var wares. Might anyone help me or offer suggestions of what to do?
You put all NPC's under /obj/ or /obj/NPC. Then, you'd store their goods in a list, thus for example
/obj/NPC/Weapon_Guy sellist=list(/obj/sword,/obj/shield)

Then let them choose what to buy, thus
var/obj/a = input("Buy what?") in src.sellist
In response to Mysame
obj/NPC/Shop
armorshop1/contents = newlist(/obj/equipable/armor/clothes)


obj //the shopkeeper
Click(obj/Q, var/obj/item, var/num)
if(istype(Q,/obj/NPC/Shop))
switch(input("Would you like to Shop?")in list("Shop","Cancel"))
if("Shop")
switch(input("[src]: Hello, what would you like to do?")in list("Buy","Sell","Cancel"))
if("Buy")
var/cost = item.value
if(num) cost*=num
if(cost>usr.Gold){src<<"Not enough gold!";return}
usr.Gold-=cost
if(istype(item,/obj/Ability/Basic/Item))
var/obj/Ability/Basic/Item/O = locate(item.type) in src.contents
if(O) O.suffix="[(text2num(O.suffix)) + num]"
else {var/obj/Ability/Basic/Item/buy=new item.type;src.contents+=buy;buy.suffix="[num]"}
else src.contents+=new item.type
if("Sell")
usr.Gold+=round((item.value*num)/2)
if(istype(item,/obj/Ability/Basic/Item))
var/numitem = text2num(item.suffix)
numitem-=num
if(numitem<1) del(item)
else item.suffix="[numitem]"
else del(item)
if("Cancel")
return


I changed my shopping to this. It compiles with no errors but i cannot get it to go when i click on my shop guy.
In response to Newen
obj
var
list
shopinventory
item
obj
O
obj
NPC
Shop
armorshop1
contents = newlist(/obj/equipable/armor/clothes)
density = 1
Click()
Shop()

obj/proc/Shop(obj/Q, mob/M)
switch(input("[src]: Hello, what would you like to do?")in list("Buy","Sell","Cancel"))
if("Buy")
set src in oview(0)
var/list/wares[] = new //sets up a new list
var/i = 1
for (O in Q.contents)
var/w = num2text(O.value,8)
wares["\A [O] --- [w] Gold"]=O
item=wares[i]
i++
wares += "Cancel"
var/shopchoice = input("What will you buy?") in wares //ask the player what he wants to buy, from the items on the list
if (shopchoice == "Cancel")
M << "Come again."
return 0
O = wares[shopchoice]
if (M.Gold >= O.value) //checks to see if
M.Gold -= O.value
M.contents += new O.type
M << "Here is your [O.name]."
else
M << "You don't have enough money."
return 0
if("Sell")
set src in oview(0)
var/list/sellcontents[] = new
for (O in M.contents)
sellcontents += O
sellcontents += "Cancel"
var/obj/sellchoice = input("What do you want to sell?") in sellcontents
if (sellchoice == "Cancel")
M << "Come again."
return 0
if(sellchoice.sellable == 0)
M << "I cannot buy this. I'm sorry"
else
var/sellvalue = round(sellchoice.value / 2)
var/shopconfirm = input("I can give you [sellvalue] gold for this. Are you sure?") in list ("Yes","No")
if (shopconfirm == "Yes")
var/makesure = M.contents.Find(sellchoice)
if(makesure != 0)
M.Gold += sellvalue
del(sellchoice)
M << "You have sold [sellchoice] for [sellvalue]."
M << "Thank you."
else
M << "You have already sold it."
else
M << "Come again."
if("Cancel")
return


I'm now clicking on the shop guy and getting whether or not i wish to buy or sell. Upon selecting either one i recieve

runtime error: Cannot read null.contents
proc name: Shop (/obj/proc/Shop)
source file: shop.dm,48
usr: Newen (/mob/players/Newen)
src: Armor (/obj/NPC/Shop/armorshop1)
call stack:
Armor (/obj/NPC/Shop/armorshop1): Shop(null, null)
Armor (/obj/NPC/Shop/armorshop1): Click(the tile (22,14,4) (/turf/ground/tile/tile))

and i dont know why.
In response to Newen
You didn't pass M as an argument into the Shop() proc. 'Neither did you pass 'Q'.
In response to Mysame
Mysame wrote:
You didn't pass M as an argument into the Shop() proc. 'Neither did you pass 'Q'.

That's always something i have problems with VB and JAVA aswell >.<, could you give an example of how to properly do that?
In response to Newen
nevermind, i tried it with

obj
NPC
Shop
armorshop1
contents = newlist(/obj/equipable/armor/clothes)
density = 1
Click()
Shop(src, usr)


And it worked. Thanks for your help :D