ID:146094
 
Code:
    proc
CreateNewCharacter()
var/nam = input("What is your name?","Name","[src.key]")
if(nam in names) //if the name they choose is in the list of names then try again
alert("Name is taken!")
src.CreateNewCharacter()
else
names += list("[nam]") // put the name they choose in the name list so no one else can use it


Problem description:
It is not recognizing that a name is in the list and therefore letting more than one person have the same name. I think it has to do with the syntax of
names += list("[nam]")
or
if(nam in names)
but I am not sure. Thanks for your help.

Drumersl wrote:
     proc
CreateNewCharacter()
var/nam = input("What is your name?","Name","[src.key]")
if(nam in names) //if the name they choose is in the list of names then try again
alert("Name is taken!")
src.CreateNewCharacter()
else
names += list("[nam]") // put the name they choose in the name list so no one else can use it


Well, firstly no put usr in proc, Ungh! Procs like alert(), input(), view(), oview(), range(), and orange() default to usr. Secondly, that is not how you add to a list. This should work:

    proc
CreateNewCharacter()
var/nam = input(src,"What is your name?","Name","[src.key]")
if(nam in names) //if the name they choose is in the list of names then try again
alert(src,"Name is taken!")
src.CreateNewCharacter()
else
names += nam // put the name they choose in the name list so no one else can use it
In response to Kalzar
alright thanks ill try that
I'm curious why you called the CreateNewCharacter() proc all over again if the name is in the list, since you could do it easily with a while loop (if you didn't know already).

CreateNewCharacter()
var/nam = src.key
while(1) // this will loop forever until you call the break statement
nam = input(src, "What is your name","Name",nam) as null|text
if(nam in names)
alert(src, "Name is taken!")
else if(nam)
names += nam
break

// ...


It'd also be inefficient if you wanted to input the player two or more variables, you'd basically just start over again if you didn't edit the existing programming.

~~> Dragon Lord

[edit] fixed programming
In response to Unknown Person
Actually, doing something like this:
CreateNewCharacter()
do
name=input(src, "What is your name","Name",key) as null|text
if(name in names)
alert(src, "Name is taken!")
while(!name&&!(name in names))

names += name
In my opinion would be easier to read & do.
In response to Ol' Yeller
Actually, that wouldn't work when you input a name which is already taken. It'd skip right to the next part. It would also keep on adding blank entries to the names list if you kept on putting in a null input.

~~> Dragon Lord
In response to Unknown Person
Thanks for your help. I forgot about while loops and your right that would be a more efficient way of doing it.
In response to Unknown Person
Fixed. =p