ID:149505
 
Ok, I'm trying to make my game so no one can have someone else's name. Here's the code I have for doing so:

mob
Login()
..()


if(usr.saved<1)

do
name=input("What is your name?","Name") as text
if(charnames && (ckey(name) in charnames))
src << "You can't choose [name] because it's already taken."
name=null
while(!name) // end of do...while loop
if(!charnames) charnames=list() // create the list



Can't figure out why, but the coude doesn't work. Can someone please help?
It's pretty simple really.

<code>mob/Login()</code>

Scan all other mobs in the world

<code>for(var/mob/M in world)</code>

...accept for the user, that is.

<code>if(M == usr) continue</code>

and see if they have the same name

<code>if(M.name == usr.name)</code>

and if they do, delete the user!

<code>del(usr)</code>

otherwise, do the normal stuff

<code>..()</code>

Looks like:

<code>mob/Login() for(var/mob/M in world) if(M == usr) continue if(M.name == usr.name) del(usr) ..()</code>

Well, that's how you check for it anyway. You'll probably want to adjust it to fit your program.
Actually, it kind of depends how you want to check for names that are already taken. Are you checking for people that are in the game, or for people who's names are saved?
In response to Foomer
I mean cheacking for saved characters.
In response to Daemon5532
Okay, well along those lines take the following steps.

declare a new list called 'SavedNames' (like you already did).

<code>var/list/SavedNames = list()</code>


Whenever a player logs out (or saves their character, however you want to do that), usually at Logout() or Del(), add their name to the list, then save the list.

<code>mob/player/Logout() spawn() AddToSavedNames(src) proc/AddToSavedNames(mob/mob) SavedNames += mob.name var/savefile/F F = new("Saves/SavedNames.sav") F["SavedNames"] << SavedNames return</code>


At world.New(), load that list from the savefile you want it saved to.

<code>world/New() ..() spawn() LoadSavedNames() proc/LoadSavedNames() var/savefile/F F = new("Saves/SavedNames.sav") var/list/X = F["SavedNames"] for(var/O in X) SavedNames += O return</code>


Then, when a player logs in, if they're NOT saved, check to see if their new name is in the SavedName list, and if it is, deny them.

<code>mob/Login() if(usr.saved < 1) choose_name name = input("Choose your name.") as text if(SavedNames.Find(name)) usr << "That name is taken, try another." goto choose_name ..()</code>

Hope that makes sense.
(This is why you should stick to board games!)
In response to Foomer
mob/player/Logout()
spawn() AddToSavedNames(src)

proc/AddToSavedNames(mob/mob)
SavedNames += mob.name
var/savefile/F
F = new("Saves/SavedNames.sav")
F["SavedNames"] << SavedNames
return


world/New()
..()
spawn() LoadSavedNames()

proc/LoadSavedNames()
var/savefile/F
F = new("Saves/SavedNames.sav")
var/list/X = F["SavedNames"]
for(var/O in X) SavedNames += O
return




var/list/SavedNames = list()



mob
Login()



if(usr.saved<1)
choose_name
name=input("What is your name?","Name") as text
if(SavedNames.Find(name))
usr<<"That name is taken, try another."
goto choose_name
else
..()


Thats the code fitted..and it doesn't work. Any reason?
In response to Switch
What doesn't work?
In response to Foomer
if(SavedNames.Find(name))
usr<<"That name is taken, try another."
goto choose_name

That part.
In response to Switch
Switch wrote:
if(SavedNames.Find(name))
usr<<"That name is taken, try another."
goto choose_name

That part.

That section maybe your problem but it wont make it easier for us to help you when you're not giving us the exact reason as to why its not working. Does it compile correctly? If so run the game in Debug mode and tell us what you get then.

Lee