ScriptUO
Official ScriptUO EasyUO Scripts => Scripting Chat => Topic started by: Cerveza on March 14, 2009, 03:16:22 PM
-
Here's two snips that do the same thing...
while #findCnt <> 0
findItem !target
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?
-
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
findItem !target
while #findCnt <> 0
findItem !target
bugs the crap out of me.
-
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.
-
One line shorter doesn't beat unexpected functionality and buggy code.... ;)
-
Here, lemme break your code:
Suppose you have some gold in your pack, or you *think* you do:
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?
-
and don't forget the ...
{
}
with the repeat / Until
;) 8)
sometimes this might be considered a BUG :'(
-
and don't forget the ...
{
}
with the repeat / Until
;) 8)
sometimes this might be considered a BUG :'(
ROFL!! I've got your back, Rana.. ;)
-
what Repeat Until Rules.... You never need { or } with repeat until
Repeat
Msg Repeat Until Rules
Until The_Disbelivers_Believe
-
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.
set #targCurs 1
while #tarcCurs = 1
wait 0
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.
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.
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.
-
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.
-
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.
-
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:
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
-
I do not user {} for repeat ever... :o
-
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
-
Well the docs are a wikki right .. so who knows someone was having some fun lol.
-
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:
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)!!!!
-
Thanks for that concise explanation!
-
Hmm, I thought I already said that. I must not have been as precise :p
XII
-
Precise-er-er?