ID:162809
 
I'm trying to make it so when you click the verb "Search" it searches for whoever's name is "Bob" and says "You found bob!" And if no one is named "Bob", it says "Bob was not found...".

I tried to do this with the for() proc, but I don't want it to check each player in the server and say if they're Bob or not, like:

Bob was not found...
Bob was not found...
Bob was not found...
You found Bob!
Bob was not found...

I was thinking the for() proc wasn't used in a situation like that, but I'm not sure.
{EDIT}
mob/verb/FindBob()
for(var/mob/M in world)
if(M.name=="Bob")
src.FoundBob=1
if(src.FoundBob)
src<<"You found Bob!"
else
src<<"Bob was not found..."
mob/var/FoundBob=0
mob/Person/Bob
name="Bob"
mob/Person/Dan
name="Dan"

Tested and fixed, it works perfect.
You should put an if() statement(tabbed) under the for() loop, and if Bob is found, say so. If not, say not.
In response to Kaiochao2536
Wow that was a fast reply. Thanks a bunch guys, can't believe I never thought of that.
In response to Infusions
Yeah I was already here posting my own devoloper how-to topic, so might as well reply to yours since it is easy
In response to Infusions
if() statements make the world go "hmm".
In response to Bakasensei
Gah, bad! You never use a single space for indentation.
In response to Bakasensei
mob/verb/FindBob()
for(var/mob/M)
if(M.name=="Bob")
src<<"You found Bob!"
return 1
else
src<<"Bob was not found..."
return 0
mob/Person/Bob/name="Bob"
mob/Person/Dan/name="Dan"


Cleaned up a little.
As long as there is only one of each name you're searching for in the world, you could use tags to speed things up considerably. Just set someone's tag variable to equal their name, and then use locate(src.name) to locate them instantly.

var/list/used_names = list()

mob/Login()

// Wait for the new user to choose an available name
var/accepted = 0
while(!accepted)
var/new_name = input(src, "Name", "What is your name?") as text
if(used_names.Find(new_name))
src << "That name is already taken. Pick another one."
continue
src.name = new_name
used_names += lowertext(new_name)
accepted = 1

// Give the user a tag equal to their new name
src.tag = lowertext(src.name)
world << "tagged as [src.tag]"


mob/Logout()
// Make sure to remove their name when they log out!
used_names -= lowertext(src.name)
del(src)


Then you can do this...
mob/verb/LocateMob(mob_name as text)
var/mob/M = locate(lowertext(mob_name))
if(!M)
src << "Couldn't find anyone named [mob_name], sorry."
else
src << "Located [mob_name] at [M.loc] ([M.x], [M.y], [M.z])."


Which is a lot faster and easier than this...
mob/verb/LocateWithFor(mob_name as text)
var/found = 0
for(var/mob/M in world)
if(lowertext(M.name) == lowertext(mob_name))
src << "Located [mob_name] at [M.loc] ([M.x], [M.y], [M.z])."
found = 1
break
if(!found)
src << "Couldn't find anyone named [mob_name], sorry."


Note that I use lowertext() when storing the names so that there's no capitalization issues when searching for them. Also note that you'll have to do a bit more work than this if you want a saved name list - or if you just go by everyone's key name, you don't have to worry about it.


Still, its probably a lot easier to just keep a list of all the players if you're searching for players, and then you can still use a for() loop to find them, but instead of looping through the whole world (thousands of objects), you're just looping through a couple dozen at best.