ID:2355090
 
(See the best response by Domdavden.)
Code:
var/list/WeaponNames
AAA=/obj/Weapons/Bronze_Dagger
AAB=/obj/Weapons/Bronze_Sword
AAC=/obj/Weapons/Bronze_Scimitar

mob/verb
Select_Weapon(msg as text)
if((msg) in list(WeaponNames))
usr.contents+=new(msg)
else
usr<<"That was not a valid weaponry code."

obj/Weapons
Bronze_Dagger
icon='Bronze_Dagger.dmi'
Bronze_Sword
icon='Bronze_Sword.dmi'
Bronze_Scimitar
icon='Bronze_Scimitar.dmi'


Problem description:
I am trying to make a system where if a certain text string is achieved/entered, then it runs through a list (in this case WeaponNames) and if the text string matches a variable in said list- then that item is created. I have attempted this, but it doesn't seem to be working in any way that I've tried it and was wondering if anyone could help with a possible solution?
new has a weird format where it expects a type before the (), which normally is like
new /obj/Weapons/Bronze_Sword(loc)

But since your type is a variable, we just replace the /obj/Weapons/Bronze_Sword bit with the variable of the path, which is WeaponNames[msg]. Try
new WeaponNames[msg](usr)
Best response
var/list/WeaponNames = list(
"AAA"=/obj/Weapons/Bronze_Dagger,
"AAB"=/obj/Weapons/Bronze_Sword,
"AAC"=/obj/Weapons/Bronze_Scimitar)

mob/verb
Select_Weapon(msg as text)
if(msg in WeaponNames)
WeaponNames[msg] = new(src.loc)
else
usr<<"That was not a valid weaponry code."


You weren't defining them as items within a list, try this? This'll make it determine whether or not the text you've entered can be found in the list
Kk, so how do I convert the text string into an actual object in the player's inventory now?
You store the paths of the items [Bronze Dagger, etc] inside of a global associative list and check to see if the [msg] is inside of it and if so; create the item under [path] and then add it to the mob's [contents].

var/list/Weapons = list(
"AAA" = /obj/Weapons/Bronze_Dagger,
"AAB" = /obj/Weapons/Bronze_Sword,
"AAC" = /obj/Weapons/Bronze_Scimitar,
)

mob/verb/Select_A_Weapon(msg as text)
if(msg in global.Weapons)
var path = global.Weapons[msg]
path = new path
src.contents += path
src << "You've select a [path:name]"
else
src << "That is not a valid weapon code."

obj/Weapons
Bronze_Dagger
name = "Bronze Dagger"
Bronze_Sword
name = "Bronze Sword"
Bronze_Scimitar
name = "Bronze Scimitar"

mob/Login()
Select_A_Weapon("AAA")