ID:270845
 
Most probably dont remember me, I used to use BYOND about 4 years ago or so, but I moved onto better languages. Decided to take a break though and check out how good ol' BYOND was doing, so Im throwing together a small game.

Here's my question;

Im starting with the basics and creating a simple Shopkeeper, you double click the shop keeper, a list pops up with all ofthe shop's items, you chose what you want, and the item is added to your inventory.

Now, as opposed to writing out a bazillion if statements checking against what I chose in the list and then placing that object in my inventory, I thought Id try to take a more efficient shortcut. The code;

obj
ShopKeeper
DblClick()
var/A = alert("How can I help you?","Shop Keeper","Buy","Sell","Leave")
if(A == "Buy")
A = input("Ah, a new customer! Take a look, ")in list("Sword","Shield","Armour","Potion")
new/obj/[A](src)

Pretty basic, if you chose But it lists 4 items, Sword, Shield, Armour, and Potion. Objects for these items were all previously created. What you choose from the list is stored in A, and then I figured using the standard object creation code Id add [A] to the user's contents, unfortnately it doesnt work.

I get 2 warnings, "operation has no effect here" and "unused label", both pointing to the new/obj/[A](src) code.

Any ideas?

Thanks,
- Conj'

p.s: Sorry, I've forgotten what the code tags are or I'd of used them.
text2path() is what you're looking for. Something like this..
A=text2path("/obj/[A]")
if(A) new A(usr)


Notice I changed the location of creation from src to usr, since src in Click/DblClick is whatever you've clicked on, meaning the item would be given to the shopkeeper.
Hey, Conjuror, I actually do remember you. ;P. Welcome back.

The code tags are <DM></DM> you know, since you said you forgot...

First of all, the DblClick() argument belongs to the shopkeeper, first, let's fix the error, and then you are going to have to change: new/obj/[A](src) to new text2path("/obj/[A]")(usr). Otherwise, your shopkeeper will receive all the items players buy.

Note also, the use of associated lists is a good approach as well.

For instance:

obj
shopkeeper
DblClick()
var/item = alert("How can I help you?","Shop Keeper","Buy","Sell","Leave")
if(item == "Buy")
var/list/wares = list("Sword"=/obj/sword,"Shield"=/obj/shield,"Armour"=/obj/armour,"Potion"=/obj/potion)
item = input("Ah, a new customer! Take a look, ")in wares
usr.contents += new wares[item]


As for your notation, I am going to nitpick, you can pick and choose your method as well, however, I will say this one thing. Most programmers are taught to write variables, objects, etc. In one format. However, this is just a standard, and not a requirement, and you may do what you wish.

I however, go by this standard, as I was taught:

Never start variables with capital letters.
Never start object definitions with capital letters.
Two-word datatypes should be seperated by either the second word being a capital, or having an underscore in between.

Though, like I said, this is a nitpicky standard. You can do whatever you want. Just suggesting for readability purposes...

Again, welcome back.
In response to Detnom
Nice, thanks guys! Hey Ter13 ;), about the variable naming yeah I usually do follow that type of structure but for the time being I was just being a bit lazy, more focused on getting the system working.

Thanks again for all the help,
- The Conjuror
Just a hint: Players tend to appreciate cancel buttons. =) Here's how:

item = input("Ah, a new customer! Take a look:") as anything|null in wares
if (!item) return // Player pressed cancel
new wares[item](usr)


(Doing new blah(usr) is better than usr.contents += new blah IMO - the contents list is one of those odd corners in DM that doesn't always entirely work quite the same way as you expect it to. Setting the location of the new object has always felt more robust. But, that's a small nitpick.)
In response to Ter13
I always find that using proper capitaliztion on everything made it easier to read.
In response to CaptFalcon33035
It's more a matter of preference, as I stated. What is easy to read is different on a per-person basis, I guess.

I just find it easier to not capitolize every minor procedure, and only capitolize to break up words...