ScriptUO

Official ScriptUO EasyUO Scripts => Scripting Chat => Topic started by: Cerveza on March 14, 2009, 03:16:22 PM

Title: While or Repeat/Until
Post by: Cerveza on March 14, 2009, 03:16:22 PM
Here's two snips that do the same thing...

Code: [Select]
while #findCnt <> 0
  findItem !target

Code: [Select]
repeat
  finditem !target
until #findCnt = 0

Both are waiting until a target is killed. I have a timer added to each, but it's unimportant for this question.

OK you real programmers out there... which is better to use, and why?
Title: Re: While or Repeat/Until
Post by: TrailMyx on March 14, 2009, 04:04:14 PM
You need to use the repeat/until.  The problem with while is that you have to "prime" the #findCnt to get a valid number, otherwise you'll have random results from the last known result from a finditem command.  That's always why I used an if/goto in scenarios like that because

Code: [Select]
findItem !target
while #findCnt <> 0
  findItem !target

bugs the crap out of me.
Title: Re: While or Repeat/Until
Post by: Cerveza on March 14, 2009, 04:42:20 PM
But, but, but... it's one line shorter  :P

The main difference, is that the "trigger" for the While must be called before the While line. With a repeat/until you can call it between them.

That is if I'm understanding it right.
Title: Re: While or Repeat/Until
Post by: TrailMyx on March 14, 2009, 04:45:21 PM
One line shorter doesn't beat unexpected functionality and buggy code.... ;) 
Title: Re: While or Repeat/Until
Post by: TrailMyx on March 14, 2009, 04:50:07 PM
Here, lemme break your code:

Suppose you have some gold in your pack, or you *think* you do:

Code: [Select]
finditem POF C_ , #BACKPACKID
... do something, don't care what
... however, this time you didn't find any gold.....then you run:
while #findCnt <> 0
  findItem !target ; Hmm, you didn't run this loop...

See how this can be buggy depending on what the previous value of #FINDCNT was?
Title: Re: While or Repeat/Until
Post by: rana70 on March 14, 2009, 06:53:31 PM
and don't forget the ...
{
}

with the repeat / Until


 ;) 8)

sometimes this might be considered a BUG :'(
Title: Re: While or Repeat/Until
Post by: TrailMyx on March 14, 2009, 06:54:13 PM
and don't forget the ...
{
}

with the repeat / Until


 ;) 8)

sometimes this might be considered a BUG :'(

ROFL!!  I've got your back, Rana.. ;)
Title: Re: While or Repeat/Until
Post by: Endless Night on March 16, 2009, 05:24:33 AM
what Repeat Until Rules....    You never need { or } with repeat until

Repeat
   Msg Repeat Until Rules
Until The_Disbelivers_Believe
Title: Re: While or Repeat/Until
Post by: Cerveza on March 16, 2009, 05:33:59 AM
Quote
what Repeat Until Rules....    You never need { or } with repeat until

Repeat
   Msg Repeat Until Rules
Until The_Disbelivers_Believe

You need them if there's more then one line. I know you can use while without {} also, as long as the following code is just one line long. Anything longer you need to use {}.

If you just want to check for something that is a one liner, while sure works great.

Code: [Select]
set #targCurs 1
while #tarcCurs = 1
  wait 0

Code: [Select]
set #targCurs 1
repeat
{
  wait 0
  until #targCurs <> 1
}

FindItem or something that is a one line thing, or even a counter or clock would work fine with a while. A repeat has to execute the code inside it to determine it's condition, a while can use whatever is currently in the script.

Code: [Select]
if C in #charStatus
{
  wait 10
  gosub cure
}
.....
sub cure
while C in #charStatus
{
cast arch cure, drink cure pots, cleanse by fire
}
return

The gosub is called if the char gets poisoned, but While will only function if the char is still poisoned. It's like an extra check to see if the char is poisoned.

Code: [Select]
if C in #charStatus
{
  wait 10
  gosub cure
}
.....
sub cure
repeat
{
cast arch cure, drink cure pots, cleanse by fire
}
until C notin #charStatus
return


Again, the sub is called if the char is poisoned. Then it will start casting and drinking without verifying the char is still poisoned or not. It must do it at least one time.

In this case you *might* not need to go through the cure process of the sub. Lets say you get poisoned, #charStatus goes to C and the script goes to execute the sub, perhaps you were only poisoned for an instant, like when you are in Vamp Embrace. While won't even execute it's portion because #charStatus is no longer C. Repeat will start casting or drinking regardless.

To make Repeat safe, you would actually have to check #charStatus AGAIN inside it to save it from casting, where While wouldn't need that check.

I agree that while *could* cause problems in poorly structured scripts. Same as ANY misused command.
Title: Re: While or Repeat/Until
Post by: Endless Night on March 16, 2009, 05:58:50 AM
You need them if there's more then one line. I know you can use while without {} also, as long as the following code is just one line long. Anything longer you need to use {}.

If you just want to check for something that is a one liner, while sure works great.


Where is this Myth coming from about the { } .. I know ive been off for a while... but i Have never used and {} in Repeat loops ever.  And yes most are well over one line and many are embedded...

 Repeat
   blab
   bla
  repeat
    bla
    bla
  until x
 until y


But while are also useful .. it depends how you code .. i use repeat a factor of 20 more than whiles... but occasionally ill use a while.  Gotos thow never.

Title: Re: While or Repeat/Until
Post by: Cerveza on March 16, 2009, 06:42:41 AM
QFT - I had forgotten about the Repeats default closing bracket of Until. I just got in the habit of enclosing anything more then one line in {} and I still do it with repeats.
Title: Re: While or Repeat/Until
Post by: 12TimesOver on March 16, 2009, 07:47:33 AM
I don't think one is better than the other in general, I think they both have their places. I use REPEAT when I need a loop to run once regardless of criteria being met in advance whereas I tend to use WHILE when the criteria has been met before the loop. Pretty simple I guess, there is a use for both and either can be used exclusively if that is how you feel like coding.

I get super-annoyed with having to pre-load a variable on purpose just to utilize WHILE but, on the other hand, I'm basically doing the same thing within the REPEAT loop by having the criteria checked AFTER the action.

Oh, and I like REPEAT for my _mainloop's:

Code: [Select]
REPEAT
   Gosub 1
   Gosub 2
   Gosub 3
UNTIL #FALSE

Probably not the most wonderful use but it works for me :p

Funny Cerv and EN, it never even occured to me that the UNTIL was an inferred "close bracket" so I looked back through some of my code and found examples where I've done it both ways though LOL. Guess using brackets isn't such a bad habit to get into really.

XII
Title: Re: While or Repeat/Until
Post by: _C2_ on March 16, 2009, 07:54:02 AM
I do not user {} for repeat ever... :o
Title: Re: While or Repeat/Until
Post by: rana70 on March 16, 2009, 09:07:29 AM
I do not user {} for repeat ever... :o

Dont't worry about it .. it works great without them ..
but this is part of the "offical" dokumentation to use them ...
it was just a kind of an insider joke .. TM knew what I was talking about....

cu
 
Title: Re: While or Repeat/Until
Post by: Endless Night on March 16, 2009, 01:26:19 PM
Well the docs are a wikki right .. so who knows someone was having some fun lol.
Title: Re: While or Repeat/Until
Post by: UOMaddog on April 16, 2009, 09:40:17 AM
Here's the precise use of while and repeat until loops (same as while and "do while" loops in C or C++):

While loops check a condition FIRST, then execute code if the condition is met.

Repeat/Until loops execute code at least once, THEN check the condition.


So....in the case where you want to "wait" until a creature is killed, the appropriate use would be:
Code: [Select]
repeat
  finditem !target
until #findCnt = 0

This is because you want to execute the code THEN check the result. Like has been mentioned before, with the while loop, you have to "seed" the value by adding an extra finditem immediately before the while loop (thereby losing any line-saving advantage)

As for the brackets, it is proper coding format to use brackets for anything that is multi-line. Repeat/Until loops have implicit brackets, however, when NESTING repeat/until loops with no brackets, you can have undesired results (if you are an idiot and don't know how to use them, LOL)!!!!
Title: Re: While or Repeat/Until
Post by: TrailMyx on April 16, 2009, 09:49:22 AM
Thanks for that concise explanation!
Title: Re: While or Repeat/Until
Post by: 12TimesOver on April 16, 2009, 10:33:05 AM
Hmm, I thought I already said that. I must not have been as precise :p

XII
Title: Re: While or Repeat/Until
Post by: TrailMyx on April 16, 2009, 10:48:14 AM
Precise-er-er?