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
ID:175297
May 11 2003, 9:15 pm
|
|
May 11 2003, 9:19 pm
|
|
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
|
|
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
|
|
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
|
|
This is good but it can lead to bloated code. Be carefull.
|
In response to Maz
|
|
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
|
|
If that looks confusing, thats ok. Here's a code for you:
<HTML> mob 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
|
|
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
|
|
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
|
|
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
|
|
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. 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
|
|
Umm thanks but i already have the code done lol.
|
In response to Lummox JR
|
|
... what do you mean?
|