ID:159164
 
As it is known, in a lot of games people tend to like to make names like " " and " " and such (without the quotes) I have found that to be a big problem.. that I do not like.

Now there is a function in the DM language that can get rid of the whitespace and misc characters (non letters) and then you can go something like;

if(src.name==null)
src<<alert("You have to write something before hitting enter!")
//back to the naming section
else
src.name=src.original_name
//continue


Anyone mind telling me what function I am describing?
VolksBlade wrote:
Anyone mind telling me what function I am describing?

A close built-in instruction is cKey(), though it may strip more than you want. People usually use a custom proc for cases like these, to strip only the characters they want stripped.
Also (tho perhaps you might not have done it in actual code), do note when working with strings*, comparing to null won't really work for you at all. If the sting is empty, its value would be "", which is distinct from null, and so a comparison between them would evaluate false. This is also related to what is called 'boolean checking'; when checking for true or false values, you should do exactly that by just specifying the expression (e.g. var) with no comparison operator - since that's exactly what the if() statement does. You should only use the comparison operator if you need to compare to a specific value.
if(Something)
//this checks if Something is true

As you probably know, the ! operator is used to 'reverse/flip' true-false values, so you can use it to check if a value is not true.

(*: It actually probably wouldn't work with generally any type of value except objects, and that's because of a "luck" effect since DM specifically sets object vars to null when they're deleted.)
In response to Kaioken
so in theory using;
if(name)

would work?
In response to VolksBlade
Not only in theory. ;) It looks like you haven't completely understood the if() statement (though that's okay; common mistake, and even the Reference is misleading there), so let me explain it further. Obviously, if() takes an expression in it. If this expression evaluates to (essentially, its final value returns) a true value, the statements under it/in the if-block execute. If not, then they don't (though an else block may execute, if present).
But what is considered a true value? A true value is any value that isn't a false value. False values in DM are: null, "" (empty string), and 0. Note: Only these 3. For example, numbers smaller than 0 (e.g. -1) aren't considered false values, and so they are true values.

So, to conclude:
if(Expression) //Expression can be any combination of vars, procs, operators...
world << "This executes if Expression was true."

The code above is very similar in function to this one:
if(Expression != null && Expression != "" && Expression != 0)
world << ...


It's worth to note that if() always works like this, and does a truth-check on the expression in it. So when you do something like:
if(Var == "X")

First, the == operator returns either 1 or 0. Then it's the same as doing as say, if(0): it checks if the result is true or false.
A small addendum on the ! operator: it just 'reverses' the truth of the value used with it. In other words:
var/Var = 0
var/X = !Var //X is now set to 1

If you feed ! a false value, it returns 1 (true), and if you feed it a true value it returns 0. So in effect, if(!(Expression)) means "if Expression is not-true/false".
In response to Kaioken
Ok, I tried the whole;
if(name)
src<<"Yeah.. try again.."
goto start
else
continue stuff


and now even if I do put stuff for the name it comes back and says "Yeah.. try again.." mind explaining what I am doing wrong?
In response to VolksBlade
VolksBlade wrote:
and now even if I do put stuff for the name it comes back and says "Yeah.. try again.." mind explaining what I am doing wrong?

Ok, I can see your confusion. First things first: you've probably heard this before more than once, but try and refrain from using goto; it creates messy code that's jumping (literally) all over the place. You should use while() or for() instead, which is possible to do 95% of the time, so don't expect the only answer to be goto.

The problem here is... well. See, I've just made an entire post explaining this in detail, so I shouldn't need to do this again. >.< Perhaps you've just skimmed my post. Anyway, please try and re-read my previous post again once or twice, then look at the code you've used. I'm sure you can figure it out. =) I realize that post has been a bit technical, so if there's something you don't understand I'll clarify it in an easier manner; this kind of thing is generally important to know anyway.
In response to Kaioken
You are awesome, because of you I figured it out, thanks!

src.name=input("what is your name?","name") as null|text
var/nametest = ckey(name)
if(!nametest)
src<<"Yeah.. try again.."
goto start
else
src<<"Your name is \"[name]\" and it is a pass!"


Sure it still uses goto start, but that is a minor set back to this great accomplishment that I have been trying to figure out for over a year, once I am done basking in my glory of defeating my foe I will get back to the grind..

Thanks again!
In response to VolksBlade
Here, instead of using goto look up while.

var/nametest
while(!nametest)
src.name=input("what is your name?","name") as null|text
nametest = ckey(name)
if(!nametest)
src<<"Yeah.. try again.."
else
src<<"Your name is \"[name]\" and it is a pass!"

That will do the same thing as your code before.
In response to Bakasensei
Thanks mate.
In response to Bakasensei
wouldnt it be better if u had a letter count needed so say they put "" its invalid and says your name has to be 2 letters or more??

maybe something along these lines would help just a thought


var/name = input("","Name") as text|null
if(length(name) < 2)
alert("Your name must be longer than 2 letters!")
return
In response to Jakb2k7
Bakasensei wrote:
var/nametest
while(!nametest)
src.name=input("what is your name?","name") as null|text
nametest = ckey(name)
if(!nametest)
src<<"Yeah.. try again.."
else
src<<"Your name is \"[name]\" and it is a pass!"


That's fine, although do-while is a little more appropriate and made specifically for situations like this. :P Also, a quirk you'll notice is that for each iteration the code ends up checking '!nametest' twice; not a real issue, but it can be avoided.

Jakb2k7 wrote:
wouldnt it be better if u had a letter count needed so say they put "" its invalid and says your name has to be 2 letters or more??

(Note "" is really 0-letters long)
If a player has input an empty string, the code would catch it because if(!Var), as I've extensively explained above, would catch Var being equal to "".
Whether he wants to have a length-bound(s) is however a different matter and his choice, of course.
In response to Kaioken
You all are awesome, I am setting up some more stuff to the name code to have it work more efficiently but I (yet again) need your help!

I have a file called "Usednames.txt" with a few sample names in there (to make sure it works) well I put it with a variable (to be able to read the contents) and it didn't work, I ended up just making a variable list the names and it worked, but then I wanted to have all of the names (that the players used) save and update as they were made, but I couldn't figure out how to do so, so I went back to the file idea and tried about 10 methods of opening and such and nothing has worked.

Any idea on what I would need to do?
In response to VolksBlade
file2text(), probably.
You can also use a standard savefile to save just about anything; it doesn't have to be a text file if you're less comfortable with handling it. With the text, you'll need to parse it on your own (well, or just use a library ;)).
hub://Deadron.TextHandling essentially has functions that will do all of your work for you here. I'd recommend at least reading them though to get a general idea of how it's done; basically though, handling text amounts to healthy, good usage of copytext() and findtext().
In response to Kaioken
alright thanks mate. thats the sound of this thread being completed.