ID:273547
 
I would like a good example on how to use the do while() proc
mob/verb/pick_a_name()
var/chosen_name,len
do
chosen_name = input("Type a name consisting of 2-6 characters.")
len = lentext(chosen_name)
while(len < 2 || len > 6)
src.name = chosen_name

<small>EDIT: Moved len declaration.</small>
In response to Kaioken
do you mind put comments in that so i can understand it
In response to The ComEdiAn
There are no comments because they'd be redundant, as the code is very simple. Use the DM Reference if you see a proc or a language feature that you don't know about.

The difference between do-while() and while() is that with do-while(), the condition is ignored for the first iteration.

As for the purpose of the code, it's to continually prompt the player to type in a name until he gives one that is considered valid. do-while() is used instead of while() because we don't want the condition to be tested for the first iteration, as the player hasn't chosen anything yet. We could replicate the same with while() with no issue, but it'd require less clean workarounds, such as: 1) using duplicate code. 2) making the condition true the first iteration by initializing vars with dummy acceptable values 3) manually making sure the condition is ignored the first iteration.
In response to Kaioken
Actually you need to define var/len before the block, since its scope is only within the block and therefore the var won't work within the while() statement. Otherwise though it's a solid example.

(Well actually I'd also use length() instead of lentext() because the latter is basically deprecated.)

Lummox JR
In response to Kaioken
thanks
In response to Lummox JR
Lummox JR wrote:
Actually you need to define var/len before the block, since its scope is only within the block and therefore the var won't work within the while() statement.

Hmm, thought so but wasn't certain. Thanks.

(Well actually I'd also use length() instead of lentext() because the latter is basically deprecated.)

I used it instead of length() on purpose, as the argument is certain to be text and lentext() isn't going to be removed in the foreseeable century.