ID:140769
 
Code:
        switch(input(S.welcometxt, S.shopname, "Exit") in list(S.item1, S.item2, S.item3, S.item4, S.item5, S.item6, S.item7, S.item8, S.item9, S.item10, "Exit"))
if("Exit")
return
if(S.item1)
if(usr.wealth < S.item1p)
alert("You don't have enough money!")
else
usr.wealth -= S.item1p
alert("Thank you for shopping with us.Your item will now spawn in front of you.")
spawnitem(S.item1id, usr.x, usr.y, usr.z)


Problem description: I keep getting expected expression from this code(specifaclly in the if statements) Is there anyway to do this without long branching if statements? (I am trying to dynamically save load shops in savefiles, btw.

Why not use the contents variable of the npc ? Or a list containing the shopkeeper's items ?

Also where does the compiler point out the error ?
I'm guessing it's happening in the if(S.item1) bit? If so then BYOND probably doesn't allow non-constant expression in switch statements, so you have to have a constant expression like the "Exit" you have there.

Infact you don't even need to use switch().

Just do something like
var/input = input(bla bla) in list(bla bla)
if(input == "Exit") return FALSE

if(usr.wealth < S.item1p)
....
.....
......



Like Andre said, you shouldn't use multiple vars like item1, item2 etc. That just leads to horrible and duplicated code. Instead use a list of the items (could be just their type paths to save on objects), which could be the shopkeeper's contents list (which can contain only actual atoms).

You also shouldn't include something like a Cancel option in the list of input choices when input() supports an actual cancel button, which looks tidier. And as was said, the if()s of switch() can only use constant values in them, not variable ones.
In response to Andre-g1
if(S.item1)


OKay, ill give the list a try.
My suggestion in this case is to make a more flexible shopkeeper. Something along the lines of this:

mob/shopkeeper
New()
//pick up anything we're standing on
for(var/obj/O in loc) O.Move(src)

verb/buy()
//select from what we have
var/obj/O = input() as null|obj in src.contents
if(!O) return
//check if they can buy it
if(O.price > usr.wealth)
usr << "You cannot afford that."
else
//create a copy and give it to them
var/obj/N = new O.type()
N.Move(usr)
usr.wealth -= O.price
usr << "You bought [N] for [O.price]."


Saving and loading is handled simply by saving and loading the shopkeeper, and stocking a shopkeeper is done by simply dropping items at his feet on the map editor. Though, if you save and load and still have those objs to pickup, it'll bloat his contents, so you'll probably want to figure out another method for one or the other.
In response to Garthor
That would be good, except i want it to be modular have the shop opened from anywhere, and so players can make player shops.(The code in the first post is actual part of a proc named openshop)
In response to Neos300
And? Making it modular simply means splitting up what I've posted into separate procs (but this one is so simple there's not much point). Allowing players to have player-made shops simply means letting them put objs into the shopkeeper's contents (and probably disabling the pickup on New()). If you want players to be able to modify the prices, you can simply modify the price of the obj within the shopkeeper. If you want to store more data, you can change it to use an associated list of objs instead of just contents.
In response to Garthor
oh, okay thanks. Didn't understand your first post too well.