ID:175297
 
I was wondering if insted of using if("sword") i could use a var to take its place like if(choice) choice = sword is theres such a var or can i make one?


Hendrix
What you need to do is leave the switch() part out. Then just do if(varname == "sword"). You should look up if() in the reference.
In response to DarkView (#1)
Maybe i didnt make my self clear before or maybe im an idiot but im trying to shorten my shop keeper code and i think its a good idea. I made a list like so
mob
var/wlist = list("sword","armor")

then i made a mob to buy stuff from

Shopkeeper
verb
Buy()
switch(input("What would you like to buy.","Buy",) in src.tlist)))
if("cancel")
return 1
else
this is were i need a var to define what i picked
In response to Hendrix (#2)
Ok, what you want to do is get rid of switch completely.
You want something like this instead.
var/choice = input("What would you like to buy?","Buy") in src.tlist
if(choice == "Cancel")
//Do cancel stuff.
if(choice == "armor")
//Do armor stuff.
if(choice == "sword")
//Do sword stuff.
And so on and so on.
In response to DarkView (#3)
This is good but it can lead to bloated code. Be carefull.
In response to Maz (#4)
Maz wrote:
This is good but it can lead to bloated code. Be carefull.

I'll rephrase that slightly for you, Maz. =)

This is bad because it can lead to bloated code. Be careful.

There is no reason to use heaps of if()s here. In fact, this is exactly what switch() is designed to do; replace lots of if()s that are exactly like that. But that's irrelevant, because I think Hendrix's point is being completely missed here.

Hendrix, I believe what you're looking for is to avoid repeating code that creates items and takes away the players' gold. Correct? If so, here's how...

<code>mob Shopkeeper var/shoplist[]=list("Sword"==/obj/weapon/sword, "Armour"==/obj/armour) verb Buy() var/choice=input("What would you like to buy.","Buy") in src.shoplist))) if (choice=="cancel") return 1 //Create bought item var/itemtype=src.shoplist[choice] var/obj/BoughtItem = new itemtype(usr) //Make the player pay for it usr.money -= BoughtItem.cost //Check the player's money if (usr.money<0) //Can't afford it! usr << "You can't afford [BoughtItem], which costs $[BoughtItem.cost]" usr.money += BoughtItem.cost //Give their money back del BoughtItem //Delete the item</code>

To add new items, just add names and type paths to the shopkeeper's shoplist var.
In response to Crispy (#5)
If that looks confusing, thats ok. Here's a code for you:

<HTML>
mob
npc
itemgiver
//all the stuff like icon and name goes here
verb
Get_Something() //the '_' will make a space
var/choice = input("What do you want?","Get Something") in list("Sword","Nothing")
if(choice == "Sword")
usr<<"You got a sword!"
usr.contents += /obj/sword //or what ever the path is for the sword
else
return

Hope that helps!

Also, I wouldn't recommend doing that, because it will take up too mauch space, and it will be to hard to trouble shoot. I recommend only using it when you have to, and sparsley then, too.
In response to Draks (#6)
Draks, not only is that the exact method I was trying to avoid, but it also won't work correctly (you can't add a type path to the contents list). It's great that you're trying to help, but in this case following your advice would cause problems and not answer Hendrix's queston. =P
In response to Crispy (#7)
I figured it out long time ago lol and crispy's way is pretty close to what i did but my way is easier less coding and i get the same results I used names to find obj's then found the type path from the obj. But Thanks
In response to Crispy (#7)
Crispy, I answered his question, and you can use type paths for the usr.contents. I use it and it works. But i forgot the new.
use this:
usr.contents += new /obj/ //bla bla bla
In response to Draks (#9)
Draks wrote:
Crispy, I answered his question, and you can use type paths for the usr.contents. I use it and it works. But i forgot the new.
use this:
usr.contents += new /obj/ //bla bla bla

That's what Crispy meant: You can't add a type path, but you can add a new object that you create via new. Still, it's much better to do this:
new /obj/thing(usr)

Lummox JR
In response to Lummox JR (#10)
Umm thanks but i already have the code done lol.
In response to Lummox JR (#10)
... what do you mean?