ID:687950
 
(See the best response by .screw.)

buy(x as text)
var/obj/x/w = new
w.loc = usr.contents


I was having a bit of trouble with some shtuff. Just kinda walking along the coding sidewalk when I found this little problem. I was trying to make a buy verb. It was to be used with an interface. (I'm experimenting) And i wanted it to me somewhat universal. I didn't want to have to write out Buysword(...) Buybow(...) Because that would be a lot of work. I was curious if somone could come along, slap me, and tell me how poory i'm coding this and that i'm using the wrong input type most likely

Best response
// this can be placed on a shopkeeper or can remain a global variable
var/list/shelf = list("Axe"=130, "Whip"=10, "Fedora"=45, "Exit")

mob
verb
Shopkeeper()
// allows you to buy whatever is on the shelf
var/item = input("Hello! What would you like to buy on this fine day?", "Shopkeeper") in shelf
if(item=="Exit") return

var/price = shelf[item] // this grabs the price

world << "<tt><b>Shopkeeper</b></tt>: You just bought: [item] for $[price]. Have a nice day!"


As a side note, I suggest reading the DM Guide and using the reference to answer your questions.
I think you mean to use something like text2path

var/X = text2path("/obj/[x]")
new X (src)


Note: With this method you have to put x in so that it matches the path. I usually use a variable and check a list.
What you need is text2path() to create the path dynamically:

buy(x as text)
// (Check that the player can buy things)

var/objPath = text2path("/obj/[x]")
if(objPath)
var/obj/w = new objPath() // Create the object of type stored in "objPath"

// (Check that the usr should be able to buy the item)

w.Move(usr) // Move the object into the usr (aka usr.contents)


However, be aware that verbs are inherently "insecure", as a player can call them themselves with whatever values they want. So you need to check at the start of the verb if it's an object they should even be able to buy, if they're in a place where they can buy things, ect.

Instead of passing part of the path, which is a bit dangerous, I would suggest you have a list of types the player can buy, and pass an index to that list. You can also attach the verb to a shopkeeper NPC to limit the player to using it only when at the shop (it's not clear from your snippet where you have the verb defined):
mob
var
gold = 0


NPC
ShopKeeper
// Associative list to hold types and costs
var/list/itemTypes = list(/obj/Sword = 10,
/obj/Potion = 30,
/obj/Key = 300)

verb
buy(n as null|num)
set src in view(2, usr)
set hidden = 1

// Check that the index passed is within the length of the list
if(n && n>0 && n<=itemTypes.len)

// Grab the type by index from the list
var/itemType = itemTypes[n]

// Grab the associated cost by the type
var/itemCost = itemTypes[itemType]

if(usr.gold >= itemCost)
usr.gold -= itemCost
// Create the object of type stored in itemType, and place it in usr
new itemType(usr)
else
usr<<"You can't afford that! It costs [itemCost] gold."

This example also uses an associative list to associate a cost with each type. If you aren't familiar with associative lists, you can read up on them in the reference.
Thanks, I was just working on getting the basics of this down. Not worrying about if you can by this yet. But still, thanks for that added bonus and saving me some work XD