Functions and procedures
Let's assume that you are writing a script which needs some instructions, conditional tests, etc ... to be executed several times. (For instance, a script which tests the pixel's colors several time and acts differently depending on the obtained results. )
Copying the same lines several time in a George Script can quickly become fastidious. It doesn't make the things easier when you need to check or modify your script. That's the reason why writing procedures and functions is very convenient :
-
A function is a set of instructions and commands which allows you to execute a precise task inside a George Script. The functions are independent from the remainder of the George Script and are located at the end of the script. The part of a George Script which distinguish itself from the function(s) will be called “main part of the script”. All the functions are available at any moment in order to be used by the Main part of the script. A function can receive parameters and always returns a result.
-
A procedure is a function which does not return results.
Here is an example of a script using a function : (Of course, using functions is really useful when you have to create bigger and more sophisticated scripts)
Param Single
Parse result command abscissa ordinate button
tv_warn TestBlackColor(abscissa,ordinate)
// Main part of the script ▲
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Function ▼
Function TestBlackColor(x,y)
Local r g b alpha sum
tv_getpixel x y
Parse result r g b alpha
If alpha<>0
sum=r+g+b
If sum==0
Return "the pixel's color is black"
Else
Return "the pixel's color is NOT black"
End
Else
Return " the pixel is transparent "
End
End
The tv_warn TestBlackColor(abscissa,ordinate) line Can be replaced by the two following lines: Answer=TestBlackColor(abscissa,ordinate) tv_warn Answer
Here is the syntax of the instructions relative to the procedures and functions :
Function NameOfTheFunction (Variable1, Variable2, Variable3, etc ... ) This instruction declares the existence of a function or a procedure in a George Script. (there is no instruction called "procedure") This instruction is followed by the procedure or function name, and then by the name of the variables (from the main part) that are needed.
Functions and procedures must always be located after the main part of your script. Calling a function or a procedure in the main part of your script can be done by writing its name and the needed variables between parenthesis, separated by comas.
Local LocalVariable1, LocalVariable2, LocalVariable3, etc ... This instruction allows you to specify the variables which will be used solely inside your function or procedure.
Return Result This instruction allows you to specify the result of your function and to make it available in the main part of the script. It could be a character string, a numerical value, a boolean value, etc ... In the main part of your script, it is possible to save it into a variable, and after to re-use it. This instruction is optional. If you don't use it, you have created a procedure instead of a function.
End This instruction is needed to finish your function or procedure.
The functions and procedures written in George language can be recursive. In other words, a function can call itself. It allows you to create more sophisticated scripts. For instance, drawing a fractal curve can be done thanks to a recursive script (see below)
Creating librairies of functions and procedures
We have just seen in the last chapter that it was possible to create your own George functions and procedures. This may be taken even further as you can store your functions and procedures in ".grg" files and you can re-use them in other scripts written in George. In other words, you can create your own libraries.
To insert one of your libraries in a George Script, you only have to write the following line in your script :
#Include "MyLibrary.grg"
The instruction #Include will search the file called MyLibrary.grg in the directory containing the current George Script or in the root directory of TVPaint Animation. The following line allows you to specify a more precise access path, from the base directory :
#Include "MyFolderOfLibraries/MyLibrary.grg"