ScriptUO

Official ScriptUO EasyUO Scripts => Scripting Tutorials => Topic started by: Cerveza on April 18, 2011, 05:35:27 AM

Title: Repeat Until While
Post by: Cerveza on April 18, 2011, 05:35:27 AM
Ok, lets get into this.

I mentioned in another tutorial that I'd talk about Repeat/Until and While. And he it is.

Both will do whatever follows over and over until a condition is met.

Lemme show you an example of both.

Code: [Select]
repeat
check the amount of beer in Cerv's glass
until the amount of beer in Cerv's glass = 0

See how whatever the until line is checking for has to be done in between the repeat-until? Now to do that with a while statement we have to check first, then check again inside.... watch

Code: [Select]
check the amount of beerin Cerv's glass
while the amount of beer <> 0
{
Cerv chills out and drinks beer
check the amount of beerin Cerv's glass
}
See how the while statement needed to check the glass both before and during the statement to see if it met criteria?

In that case, using Repeat/Until makes much more sense.

Here's Cerv's Rules For Using Repeat/Until or While:

If I'm checking something that I have set in the script - use Repeat/Until.
If I'm checking for something that EUOx sets - use While.

What that means is like in the example above. I'm checking for something that I have setup in the script. So I would use the Repeat/Until.

Now here's when I like to use While.

Code: [Select]
repeat
do this thing that requires mana
until #mana < 10

See that will repeat whatever is in the middle until my mana drops below 10. Since #mana is an EUOx function thingy, I like to use While for this.

Code: [Select]
while #mana > 10
do this thing that requires mana

You can see the difference right there. If it's a # command from EUOx I always prefer to use While because I don't have to constantly check whatever is the criteria in the statement.

Another thing to remember when using repeat/until and while.

Repeat/Until will do everything in between them. Everything.

Code: [Select]
repeat
do this
do that
do the other
do some more
until #done

With While it will only do the NEXT LINE OF CODE unless you use brackets {}

Code: [Select]
while something is something
do this
do that
do the other
do some more

That code will ONLY do the one line under the while (do this) until the criteria is met (something is something). The correct usage would be:

Code: [Select]
while something is something
{
do this
do that
do the other
do some more
}

By putting everything in brackets they will all be done until the criteria is met.

That's about it. Hopefully it'll spark some conversation.

Key Points:

1 - Use Cerv's Rules, use repeat/until if I set it, use while if EUOx sets it.
2 - Repeat does everything between it and Until, While does the next line or between the brackets {}
Title: Re: Repeat Until While
Post by: Endless Night on April 18, 2011, 06:16:13 AM
This is how I differentiate/think of repeat/until or while

- While: Check before doing X
- Repeat/Until: Do X then check before doing X Again.

So WHILE might never do X .. where as Repeat/Until is Guaranteed to do X at least once  
(where X = a line or block of code)


Which is why Repeat/Until makes a great loop for the main function of your script.  Eg
Code: [Select]
; script start
repeat
  gosub scriptaction1
  gosub scriptaction2
  gosub scriptaction3  ; etc
until %ScriptFinished
halt  ; script end
; subs below

EDIT: Nice write up Cerveza


Title: Re: Repeat Until While
Post by: 12TimesOver on April 18, 2011, 06:47:23 AM
Great write-up Cerv, can always use more script tutorials and I'm soooo damn lazy lately...

Exactly how I think about it EN. Anytime I need something run until a criteria is met the choice is REPEAT/UNTIL, if I need something run only if a criteria is met it's a WHILE.

I always use REPEAT/UNTIL as my main loop nowadays as well.

Code: [Select]
repeat
   gosub sub1
   gosub sub2
   bosub subetc
until #FALSE ;<--- which will never evaluate as true thus loops 4-evah!

X
Title: Re: Repeat Until While
Post by: UOMaddog on April 18, 2011, 07:15:18 AM
I agree 100% with EN's outlook on it.

Repeat/Until (essentially do/while loops in C) guarantee the code executes once! This is perfect for main loops, since it's assumed you want the script to run at least once (and perhaps more).

While loops will only execute IF the condition is already met, so they're great for "waits" in my opinion. Such as:
Code: [Select]
set #targcurs 1
while #targcurs <> 1
   wait 1
Using this method, you set the cursor, and ideally check the next WHILE condition (which hopefully fails) and you move along (no waiting at all!) If you had used a repeat/until instead, you'd have to do:
Code: [Select]
repeat
set #targcurs 1
wait 1
until #targcurs = 1
Note the added wait 1, which is now guaranteed.
Now you could do:
Code: [Select]
set #targcus 1
repeat
wait 1
until #targcurs=1
which would execute the same, but now you've used 3 lines of code (and still guaranteed a wait 1) when you could have used 2 lines (and not necessarily any waiting)
Title: Re: Repeat Until While
Post by: gimlet on April 18, 2011, 07:44:54 AM
These are great - how about a lua or open example
Title: Re: Repeat Until While
Post by: Tidus on April 18, 2011, 02:36:40 PM
you should throw a For Loop in there and just get all the loops out of the way ;)
Title: Re: Repeat Until While
Post by: Cerveza on September 20, 2011, 06:40:10 AM
I still go with using while when the "trigger" is something with a # in front of it LOL.

If I need to wait for a condition to be met, or check a condition over and over, and that condition is a set EUO function, then I use while because I don't have to constantly check it with a repeat/until

while #mana < 20
  wait 1

Simple 2 line to wait for mana to be greater then 20. When doing the same thing with repeat/until

repeat
  wait 1
until #mana > 20

Additional line that's not needed.

Also, the repeat/until is actually a loop, correct? I'd be interested to see if there's CPU difference between those two examples...
Title: Re: Repeat Until While
Post by: UOMaddog on September 20, 2011, 12:15:19 PM
Looking at your original example, you have a slight flaw you might want to look at:

You used two examples:

Example 1:
Code: [Select]
repeat
do this thing that requires mana
until #mana < 10
Example 2:
Code: [Select]
while #mana > 10
  do this thing that requires mana

The flaw is that in example 1, you never check to see if you even HAVE 10 mana before attempting to execute it. So in this case, example 2 is by far the better one!

As EN stated, the real difference is:

Repeat/Until - Guarantees the action executes AT LEAST ONCE and then checks if it should repeat (Do, then Check)
While - Checks if it should execute THEN does the action (Check, then Do)
Title: Re: Repeat Until While
Post by: camotbik on September 20, 2011, 01:02:42 PM
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until
Title: Re: Repeat Until While
Post by: Neo on September 20, 2011, 01:37:04 PM
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until
I've never used brackets inside repeat/until cycles... Never found any issues...
Title: Re: Repeat Until While
Post by: camotbik on September 20, 2011, 01:41:50 PM
well many people don't close they're subs with a final return and don't encounter any problems as well.. But if the syntax says they're should be there, there must be a reason for that.
Title: Re: Repeat Until While
Post by: gimlet on September 20, 2011, 02:40:17 PM
Whats the open EUO equivalent of these statements?
Title: Re: Repeat Until While
Post by: Neo on September 20, 2011, 03:24:00 PM
Well, if the SYNTAX says it, who am I to question? :)
Title: Re: Repeat Until While
Post by: Neo on September 20, 2011, 03:34:01 PM
Whats the open EUO equivalent of these statements?

In Cerveza's words:

Code: [Select]
while(Mana>=10)do
do this thing that requires mana
end

Code: [Select]
repeat
do this thing that requires mana
until(Mana<=10)

I think that's it...

I could be wrong though...
Title: Re: Repeat Until While
Post by: Endless Night on September 20, 2011, 04:26:45 PM
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until

The above statement is incorrect.

{}  are not required on Repeat..Until   ever.
They are required on While  loops of more than one line of code
They are Required on For loops of more than one line of code

The wiki can be wrong. As its editable by use users.

edit:fixed typo
Title: Re: Repeat Until While
Post by: camotbik on September 20, 2011, 10:09:42 PM
huh, I didn't know it's user editable. Sorry for misleading.

They are not Required on For loops of more than one line of code

Is that a typo?
Title: Re: Repeat Until While
Post by: Cerveza on September 21, 2011, 03:05:30 AM
repeat
will repeat
anything in between
itself and the
very next instance
of the command
until

You do not need {} for them.

while
only does one line

So if you want more then one line for a while you need {}

while
{
does the next line
ONLY
so you
need to use
brackets
}

For loops need {}

This is for EUO, I have no idea about OEUO
Title: Re: Repeat Until While
Post by: Endless Night on September 21, 2011, 04:23:31 AM
huh, I didn't know it's user editable. Sorry for misleading.

They are not Required on For loops of more than one line of code

Is that a typo?

guilty of excessive cutting and pasting as charged..  correct typo

For loops require {}
Title: Re: Repeat Until While
Post by: 12TimesOver on September 21, 2011, 05:24:43 AM
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until
Nope, no brackets needed for Repeat/Until.

And Cerv's examples are correct. There is no need to check the mana before the loop, this is the inherent difference between repeat and while. Repeat ALWAYS executes the code the first time, While only executes code if the criteria is met first.

X

<edit> whoops, I see my reply is now redundant. I'll leave it anyhow, now we have quorum ;)
Title: Re: Repeat Until While
Post by: UOMaddog on September 21, 2011, 12:47:18 PM
@12X: The reason I said it was a flaw is because most of the times when you want to do something that requires mana, you should CHECK FIRST, so in those cases, WHILE would be a better choice than REPEAT/UNTIL. I'm not saying it wasn't behaving properly, just that it's not an ideal choice if that circumstance
Title: Re: Repeat Until While
Post by: 12TimesOver on September 21, 2011, 02:14:33 PM
@12X: The reason I said it was a flaw is because most of the times when you want to do something that requires mana, you should CHECK FIRST, so in those cases, WHILE would be a better choice than REPEAT/UNTIL. I'm not saying it wasn't behaving properly, just that it's not an ideal choice if that circumstance
Ok, well I would agree that the example might not be the best as for game mechanics (and after re-reading the thread I see that that was your point) but it was syntactically accurate which was the point anyhow. Sounds like we both agree in either case.

;)

X
Title: Re: Repeat Until While
Post by: UOMaddog on September 21, 2011, 03:31:06 PM
Now if only we were in Congress! We could agree AND get things done!
Title: Re: Repeat Until While
Post by: slyone on September 21, 2011, 04:47:49 PM
Whats the open EUO equivalent of these statements?

Lua is pretty similar in structure.  Check out these links from the online manual...

repeat-until:
http://www.lua.org/pil/4.3.3.html

while:
http://www.lua.org/pil/4.3.2.html

for:
http://www.lua.org/pil/4.3.4.html

I started programming in C.  Half of the time I forget the "do" in the while, for and if statements in Lua...
Title: Re: Repeat Until While
Post by: mmoore5325 on October 14, 2014, 10:55:59 PM
I've seen people put <> in their statements, I was just curious what they do.
When I use #targcurs
set #targcurs = 1
while #targcurs = 1
wait 2
set %bag #lobjectid
set #lobjectid #ltargetid
event macro 17
Thats actually almost all the code I am familiar with
But I was just curious.
Cervesas beer <> 0
and I thought I saw someone put
while #targcurs <> 1 or something close to it
If Cervesas beer < 0, OMG NO!
if it is > 0, GREAT!
But if its <> 0, I take it as maybe I'm drinking, maybe i'm not.  Please help me understand.
Title: Re: Repeat Until While
Post by: TrailMyx on October 14, 2014, 11:07:58 PM
The <> operator simply means "not equal".  Other languages use "!=" for the same purpose.