ScriptUO

Official ScriptUO EasyUO Scripts => Scripting Tutorials => Topic started by: camotbik on October 18, 2011, 05:06:54 AM

Title: The need of brackets.
Post by: camotbik on October 18, 2011, 05:06:54 AM
I saw that many newcomers to easyuo are having the same old problem - they are not using brackets on statements. So I would say always use brackets when possible (except if there's only one line of code(example1)). The main purpose of brackets is to determine the margins of a statement(see example 2).
The usage of brackets.

Example1 - one line (if) statement, that doesnt need any brackets.
Code: [Select]
if #hits < #maxhits                   ; if hits get lower than maximum hits
event macro 1 1 Guards                ; say Guards

In this example if your hits get lower than maxhits, you will call Guards. As in this example we are only using 1 line (calling the guards) after the if statement, so we have no need for brackets.

Example2 - multiple line (if) statement, that needs brackets.
Code: [Select]
if #hits < #maxhits                   ; if hits get lower than maximum hits                
{                                     ; open statement if #hits lower than maxhits
event macro 1 1 Guards                ; say Guards
event macro 15 28                     ; cast spell greater heal
target                                ; wait for target
event macro 23 0                      ; target self
wait 40                               ; wait 2 seconds
}                                     ; close statement if #hits lower than maxhits

In this example if your hits get lower than maxhits, you will call Guards, then you cast greater heal spell on yourself and wait for 2 seconds. As in this example we are using more than 1 line after the if statement - we need to use brackets, so our code would work the way we want.

Example2.1 -  multiple line (if) statement, that needs brackets, but doesnt have them. -- this is the bad way - the way you dont want to do this.
Code: [Select]
if #hits < #maxhits                   ; if hits get lower than maximum hits                
event macro 1 1 Guards                ; say Guards
event macro 15 28                     ; cast spell greater heal
target                                ; wait for target
event macro 23 0                      ; target self
wait 40                               ; wait 2 seconds

What does happen here? Well if you get your hits bellow maxhits, you will call guards, but in the mean time while you are at your maxhits, you will cast greater heal on yourself and wait 2 seconds. If you ask yourself why? Well because the margins of the statement arent set and you will execute only one line after the if statement(see example1).

Example 3 - Usage of brackets in other circles.
if / else
Code: [Select]
if #hits < #maxhits
{                                     ; open statement
event macro 1 1 Guards                ; say Guards
event macro 15 28                     ; cast spell greater heal
target                                ; wait for target
event macro 23 0                      ; target self
wait 40                              ; wait 2 seconds
}                                     ; close statement
else
{                                     ; open statement
event macro 1 1 Everything seems fine ; say Everything seems fine
wait 40                               ; wait 2 seconds
}                                     ; close statement
while
Code: [Select]
While #hits < #maxhits                ; while hits are less than maxhits
{                                     ; open statement
event macro 1 1 Guards                ; say Guards
event macro 15 28                     ; cast spell greater heal
target                                ; waitfortarget
event macro 23 0                      ; target self
wait 40                              ; wait 2 seconds
}                                     ; close statement
for
Code: [Select]
for %i 1 20                           ; repeat 20 times
{                                     ; open statement
.....
.....
}                                     ; close statement

I hope this will help some of the newcomers to understand the need of brackets.
Title: Re: The need of brackets.
Post by: Endless Night on October 18, 2011, 05:24:45 AM
nice and much needed tutorial camotbik
Title: Re: The need of brackets.
Post by: Crome969 on October 18, 2011, 07:05:37 AM
Thanks buddy, you did a great job. Newbies need this.. and Oldies might find something they not knowed yet:)