ID:164061
 
hey there, here is what i need help learning (or finding a way) how to make a FindText thing at the begginging of a say verb, and it would have to find a / and if it does it will continue the process as like if the person puts /Teleport 1 it will teleport the person to Town 1, but if he types like /Poop! then it will put a Invalid Command! on their text log, but pretty much thats it, i have it like this:

mob
verb
say(T as text)
if(T=="/Teleport 1")
src<<"<font color=red>Warped!</font color>"
src.loc=locate(/area/Town1)
if(T=="/Teleport 2")
src<<"<font color=red>Warped!</font color>"
src.loc=locate(/area/Town2)
if(T=="/Teleport 3")
src<<"<font color=red>Warped!</font color>"
src.loc=locate(/area/Town3)
if(T=="/Teleport 4")
src<<"<font color=red>Warped!</font color>"
src.loc=locate(/area/Town4)
if(T=="/Teleport 5")
src<<"<font color=red>Warped!</font color>"
src.loc=locate(/area/Town5)
if(T=="/Spell 1")
src<<"You cast spell 1"
//Spell stuff here!
return
if(T=="/Spell 2")
src<<"You cast spell 2"
//Spell stuff here!
return
if(T=="/Spell 3")
src<<"You cast spell 3"
//Spell stuff here!
return
if(T=="/Spell 4")
src<<"You cast spell 4"
//Spell stuff here!
return
if(T=="/Spell 5")
src<<"You cast spell 5"
//Spell stuff here!
return
else
view()<<"\[Say\] \<[src]\>, \"[T]\""


As you can see its not very good, and i need the FindText & Invalid Command part, the main problem with my version is that it'll teleport me to town 1 then it'll go and say what i said... Can you all help me?

If so... Thanks!

P.s.: I couldent remember what i named the project so i couldent put the Invalid Command part in, but i just found it!
From what I can see the code is fine... It works on my computer... What errors do you get?
In response to Burnination
i dont get errors... if you read my post you'd see what i said, i said it'd teleport me and say it did, but then say what i said (/Teleport 1) and now.. read my whole post above please.
In response to VolksBlade
it appears to work fine, i dont understand your errors or whatever is wrong with the code. please tell me what is not working, i dont believe it was clarified in your post
You could probably learn a lot by looking at some command parser demos. (if there are any...?)

What I'd do is use copytext() to get the first letter of the string, if it's a slash, then use FindText() to get the position of the space and copytext() everything from the slash to the space (If there isn't a space, just copytext() from the slash to the string's Length().) Then perform a switch() on what you just copytext()'ed out to execute the command. If nothing is matched by the switch, output the invalid command part. If a slash is never found, just do a normal say.
VolksBlade wrote:
As you can see its not very good, and i need the FindText & Invalid Command part, the main problem with my version is that it'll teleport me to town 1 then it'll go and say what i said... Can you all help me?

the bold part is the problem
Aside from the horridly bad way you're doing this I'll give you a little hint as to the problem. You're forgetting 'return' at the end of the teleport sequence.
In response to Nadrew
oh... thanks ^__^

and also, why is it when i type / in byonds word bar (the pink bar on the bottom of a game) that you usually talk with, why it is the say " part goes away?
In response to VolksBlade
Sorry, I gathered from the top part of your post, your post subject, and the fact that this was posted in Developer How-To, that you would be more interested in a way to find the command text on the string rather than a quick answer for your code problem.

Nadrew already provided you with the quick and simple answer, but I'd just like to add to it by pointing out an issue with your if()s. First, are you familiar with switch()?

Right now, you're ALMOST using those if()s like case checks in a switch, except you're not, they're just normal if()s. What does that mean? Well, for starters, that else at the bottom is only checking one thing, the if() above it:

if(T="/spell 5")
//blah blah
else
//say something


Using switch() would save you a lot of typing and readability. You also wouldn't run into issues where an overlooked return would mess up your say verb.

switch(T)
if("/Command 1")
//Do this command
if("/Command 2")
//Do this command
//etc etc
else //no matching case was found
//say something


Just some advice.
In response to Zagreus
...Indeed it would now that ive looked at it, and i know basic switch() stuff, but not that much.

Thanks for the help.
var/list/commands=list("/Teleport","/Spell")
mob
verb
say(T as text)
var/foundc=0
var/invalidc=0
if(findtext("[T]","/",1,2))
for(var/f in commands)
if(findtext("[T]","[f]",1)) foundc++
if(!(findtext("[T]","[f]",1))) invalidc++ //search for / being used but if it doesnt exist in commands, it becomes invalid
invalidc-=round(commands.len-1)
if(invalidc) {src<<"[copytext(T,2,length(T)+1)] is an Invalid Command";return} //if finds an invalid command
if(!foundc) view(src)<<"\[Say\] \<[src]\>, \"[T]\"" //if doesnt find 1 command, just viewsay text
else
if(findtext("[T]","/Teleport",1)) //searches for /Teleport
if(T=="/Teleport") T="/Teleport 1"
var/towndigit=text2num(copytext("[T]",11,length(T)+1)) //copies the digit that was used in teleport, no matter how many digits it is
if(towndigit<=1) towndigit=1
src<<"<font color=red>Warped to Town [towndigit]!</font>"
// this if(text2num==1) src.loc=locate(whatever,whatever,whatever) //locate here
//or this src.loc=locate(round(world.maxx/2),round(world.maxy/2),towndigit) //to save you less trouble if your putting each town's digit on a Z axis
if(findtext("[T]","/Spell",1)) //searches for /Spell
if(T=="/Spell") T="/Spell 1"
var/spelldigit=text2num(copytext("[T]",8,length(T)+1))
if(spelldigit<=1) spelldigit=1
src<<"You cast spell [spelldigit]"
//stuff here