ID:1987657
 
(See the best response by Kozuma3.)
Code:
mob
NPC

shopkeeper
planet=""
icon = 'Shopkeeper.dmi'

Vegeta
icon_state="Vegeta"
layer=50
density=1
attackable=0
Namek
icon_state="Namek"
layer=50
density=1
attackable=0
Earth
icon_state="Earth"
layer=50
density=1
attackable=0
Icer
icon_state="Icer"
layer=50
density=1
attackable=0
Hell
icon_state="Hell"
layer=50
density=1
attackable=0
Heaven
icon_state="Heaven"
layer=50
density=1
attackable=0

DblClick()
Installshop()
var/player/P = usr
if(!P.frozen)
switch(input("Welcome to your local shopkeeper.") in list ("Buy","Sell","Cancel"))
if("Buy")
var/obj/Item/R = input("What would you like to buy?","Shopkeep",text)in contents
if(alert("Buy [R]? Cost: [R.cost] zenni","Yes","No")=="Yes")
if(R.cost>P.zenni)
P.zenni -= R.cost
new R(usr)
P<<"You bought [R]!"
else
P<<"You do not have enough zenni!"
if("Sell")
var/list/sellable = list()
for(var/obj/O in usr)
if(istype(O))
sellable += O
break
var/selling = input(sellable)
if(alert("I'll give you [O.cost * O.buyprice] for it. Deal?", "Yes", "No") == "Yes")
usr << "Thank you for your business!"
usr.zenni += O.cost * O.buyprice
del(selling)


Problem description:

After the input opens asking to Buy or Sell, I click Buy and I get an in-game error:

runtime error: Cannot read null.cost
proc name: DblClick (/mob/NPC/shopkeeper/DblClick)
source file: Shopkeeper.dm,48
usr: Lol (/player)
src: Vegeta (/mob/NPC/shopkeeper/Vegeta)
usr.loc: Desert Five (114,135,4) (/turf/newturfs/Desert_Five)
src.loc: the cliff23 (114,136,4) (/turf/cliff2/cliff23)
call stack:
Vegeta (/mob/NPC/shopkeeper/Vegeta): DblClick(the cliff23 (114,136,4) (/turf/cliff2/cliff23), "default.map", "icon-x=17;icon-y=10;left=1;scr...")


I Click Sell, nothing happens.


Any pointers?

R doesn't exist(null) and is null resulting in the error.
But R does exist.

But I'm sure if I defined it var/obj/Item/clothing/R it would give the same error.
After this line

var/obj/Item/R = input("What would you like to buy?","Shopkeep",text)in contents


Put

world << "R = [R.name]"


What does it output?
 mob
proc
Installshop(var/obj/Item/clothing/I)
for(var/mob/NPC/shopkeeper/M in world)
I+=M.contents


The Installshop proc btw, this is what I use to throw the clothes in the shopkeepers contents.
For fucks sake..
/mob/npc/shopkeeper
icon='shopkeeper.dmi'
attackable = 0
layer = 50
density = 1

Vegeta{icon_state="vegeta"}

What exactly are you trying to accomplish here?
var/obj/Item/R = input("What would you like to buy?","Shopkeep",text)in contents

The reason R is null is because it was never created/initialized. You need to create it before u do anything with it
In response to Kozuma3
Kozuma3 wrote:
After this line

var/obj/Item/R = input("What would you like to buy?","Shopkeep",text)in contents

Put

world << "R = [R.name]"

What does it output?

Didn't get anything in the output, instead it gave mthe same error this time pointing to line .46 which is the line where I inputted-
world<<"R = [R.name]"


I don't even think any executes after it labels the var.
In response to HaxRp
HaxRp wrote:
Didn't get anything in the output, instead it gave mthe same error this time pointing to line .46 which is the line where I inputted-
world<<"R = [R.name]"

I don't even think any executes after it labels the var.

That means R is null and not being set properly.
 var/obj/Item/R = input("What would you like to buy?","Shopkeep",text)in contents
So this is an incorrect way to set?


I basically just want it so it reads all the items in the Shopkeepers contents for the user to catalog through for buying.
In response to HaxRp
Is each item created or are you only retrieving their types?
If you mean


obj/item

1

2

3


Then yes, I figured maybe typesof would work too but I wanted to find a shortcut aside from doing all that and including lists and what not.
Best response
When the world boots up you could loop through the types and add the created item to a global list to retrieve from.
Yeah i know that method, was seeing if there was a more simple way. Guess not xD
Learn from this
/mob/verb/check()
var/obj/items = locate() in usr.contents
if(items)
for(items in usr.contents)
world<<items
else
alert("no items")

/obj/items
icon='items.dmi'
apple{icon_state="apple"}
orange{icon_state="orange"}

/mob/verb
give_apple()
usr.contents += new/obj/items/apple
world<<"++apple"
give_orange()
usr.contents += new/obj/items/orange
world<<"++orange"
In response to GreatPirateEra
I personally don't recommend using usr in those verbs.
Also, I don't understand why you would do the for() that way.
In response to GreatPirateEra
GreatPirateEra wrote:
Learn from this
> /mob/verb/check()
> var/obj/items = locate() in usr.contents
> if(items)
> for(items in usr.contents)
> world<<items
> else
> alert("no items")
>
> /obj/items
> icon='items.dmi'
> apple{icon_state="apple"}
> orange{icon_state="orange"}
>
> /mob/verb
> give_apple()
> usr.contents += new/obj/items/apple
> world<<"++apple"
> give_orange()
> usr.contents += new/obj/items/orange
> world<<"++orange"


Appreciated.
In response to Kozuma3
Just something I did really quick on my phone. Wasn't tested but it should work. Anyways, I added upon it a little to demonstrate how u can get it's variables
mob/verb/check()
var/obj/items/item = new
item = locate() in usr.contents
if(item)
for(item)
world<<"[item]: cost = [item.cost]"

else
alert("no items")

/obj/items
icon='items.dmi'
var/cost = 0
apple{icon_state="apple";cost=5}
orange{icon_state="orange";cost=10}

mob/verb
give_fruits()
usr.contents += new/obj/items/apple
world<<"++apple"
usr.contents += new/obj/items/orange
world<<"++orange"
In response to Kozuma3
Kozuma3 wrote:
I personally don't recommend using usr in those verbs.
Also, I don't understand why you would do the for() that way.

Kozuma is telling the truth. Use src instead of usr for this. Of additional note, why are you
locate() in usr.contents
? Seems awfully redundant.
In response to Somepotato
In my defense, I did state this was done rather quickly on my mobile device xD. Looking back at it, there are a couple of things I'd change. Regardless, it did demonstrate how to go about accessing an object's variables, meaning the snippet served it's purpose. Sure using usr in this particular case isn't safe, but I wasn't thinking about safety as I wrote this down.