Directives
As you've learned on the previous page,
A directive is an instruction that controls how the program should behave or proceed. Directives can be used to create loops, check for conditions, receive events, and more.
Now let's take a look at the available directives in Powerlang.
The most important part of any code are conditional statements. These allow you to check whether a certain condition is true, and if yes, execute some code. In most programming languages, including Powerlang, this is done with the if directive. The if directive will run code attached to it, if the condition it evaluates is true. Let's demonstrate an example by checking whether a certain equation is true:
The code snippet above will check whether 10 + 9
equals 21
, and if yes, will print Oh no, math is broken!
. As 10 + 9 equals 19, this sentence will never be printed. Note the usage of ==
instead of =
in this case. While =
is used to define variables, ==
is used to check whether two things are equal. This will be explained in more detail later.
If directives can be extended with an else directive. The else directive will run if the previous if directive didn't. Let's use it in our previous example:
The else directive will run if the initial condition isn't met. In our case, 10 + 9 does not equal 21, so the if directive will not run, but the else directive will, and we will see Yay, math is working!
in our output.
Powerlang does not have an elseif
directive, unlike Lua.
RetroStudio Visual Scripting does not have nor elseif
, nor else
. Powerlang emulates the else
directive using variables.
Another crucial part of good code are loops. Loops allow you to repeatedly execute some code a set amount of times or until a condition is met. Powerlang supports two loops: for and while.
The for directive will execute a snippet of code a certain amount of times. The current place can be stored as a variable for the code to access. Let's show an example to print numbers 1 to 10.
This code will run 10 times, starting at 1 and ending at 10. i
represents the place at which the loop is at. For example, i
will initially be 1, then once the first loop is complete, it will become 2, etc.
The while directive will execute a snippet of code until a certain condition is met. Let's show an example:
We define a variable called number
and assign it the value 1. The while loop will run the code while number
is less than 5. On each loop, Number is less than 5!
will be printed, and the number will be incremented by 1. When number
reaches 5, the loop will stop running, and Number is bigger or equals 5
will be printed.
It's also great to have functions in your code. As you learned on the previous page, functions help organize common tasks and reuse blocks of code. Functions can be defined and called.
Let's define a function that will add two inputs together, and then call it to calculate the sum of 20 and 49.
Executing this piece of code will output the number 69. Let's break this snippet down:
function Sum(num1, num2)
declares a function with the name of Sum and allows it to take 2 arguments: num1 and num2;return num1 + num2
first adds num1 to num2, and thereturn
directive "returns" the result of the function. Note thatreturn
isn't required if you are not "returning" any outputs.print(Sum(20, 49))
first calls the function Sum we defined earlier with two arguments: 20 and 49, then it gets the "returned" result of the sum, and prints them to output using theprint
function.
Note that a function does not necessarily need to have arguments or outputs. It is up to you to choose how many arguments and outputs a function needs.
In real games, it is important to listen to changing conditions, such as a user clicking a button, or a score increasing. In programming, this is done using events. Powerlang allows you to connect to predefined events using the event directive. This example, when used in a local script, will print Clicked!
each time the user clicks the left mouse button:
Events will be explained in greater detail in the future.
On the next page you'll learn how to interact with the game's environment.
Last updated