When developing your own scripts, there may be some frequently used judgment statements,
like to statement “If there exist a certain image, click on it” we have show you earlier.
if (find(“res/20190809-131033.png”)) {
click(“res/20190809-131033.png”);
} else {
print(“No image found.”);
}
If my script uses this statement in many places,
I have to write it down everywhere, isn’t it too tired?
Fortunately, Blackey Script supports the function of custom functions.
Now let’s see how to define our own function ε≡ヘ( ´∀`)ノ
Syntax
The self-defined functions need to setup the function name and input parameters,
as shown below:
def function_name (parameter1, parameter2, …) {
expression1;
expression2;
…
return expression; // optional
}
The variable inside the self-defined functions are independent from the outside main script,
and currently Blackey Script only supports Call-By-Value.
Which means that when the self-defined function is called, the incoming parameters will be copied into it.
An example is as follows:
def foo(x, y) {
print(“foo1: x, y = “, x, y);
x = 3;
print(“foo2: x, y = “, x, y);
return (x+y);
}x = 1;
y = 2;
print(“foo result: “, foo(x, y));
print(“main: x, y = “, x, y);
This code will print the result below:
foo1: x, y = , 1.000000 , 2.000000
// Pass value x = 1, y = 2 into foo function.foo2: x, y = , 3.000000 , 2.000000
// We change the x’s value to 3 in the foo functionfoo result: , 5.000000
// The return result will be 3 + 2 = 5main: x, y = , 1.000000 , 2.000000
// The variable x outside the main script is still 1, which is not affected by internal modification
The syntax of the self-defined function is very similar to the way to define a function in Python.
But notice that we have one more pair of curly braces {}. (=´ω`=)
Example
Here I list a few self-defined functions that I commonly use for your reference:
// Sleep by second
def sleep(second) {
msleep(1000 * second);
}
// Sleep for 3 seconds
sleep(3);// Wait until a certain image show on screen, wait up to given count second.
def wait(path, count) {
while (count > 0) {
if (find(path)) {
return 1;
} else {
sleep(1); // You can still call another self-defined function in a self-defined function.
count = count – 1;
}
}
}
// Wait for the OK.img image shows, up to 3 seconds.
wait(“res/OK.img”, 3);
So far, we have introduced all functions of Blackey Script.
What are you waiting for? Just hurry up to write you own script! ᕦ(ò_óˇ)ᕤ
If you have any questions, please kindly leave a message to us.
Also welcome to our fan page to have an interaction.
(<ゝω・) Waiting for you ☆