Take the Stage!

Lights, Camera Actions!

To get started drawing shapes on the stage, we’re going to need to first look at actions.

We’ve seen a few actions already, scríobh was an action, as well as codladh.

Actions are special values that represent an action the computer can take. In the case of scríobh that action was writing text on the console, and in the case of codladh that action was waiting for some time. In the future we’ll see how to make our own actions, but for now we’ll look at how to use them.

Call me!

When we use an action, we say we are “calling” it. Remember how we used the scríobh action?

scríobh("Write me on the console")

When the Setanta interpreter reads “scríobh("Write me on the console")” it knows to call the scríobh action, and to pass it the text "Write me on the console".

The text in the brackets is called an argument. In the code above, the text "Write me on the console" is the argument to scríobh.

In the following code we saw before, 2000 is the argument to codladh:

codladh(2000)

When this code is run, the computer passes 2000 to the codladh action and then calls it, causing the program to sleep for 2 seconds.

Multiple Arguments

Some actions in Setanta can take more than 1 argument. When we pass in more than 1 argument we use a comma (“,”) to separate them.

For example: The scríobh action can take as many arguments as we want, and it will write them all out with a space in between them. Try it out here:

Not all actions can take as many arguments as you want. The codladh action needs exactly 1 argument, no more, no less.

Some actions can also take 0 arguments

Challenge

Let’s test our knowledge of actions!

Here is some code that uses scríobh to write “Setanta is fun!”. Change the code so that it still prints “Setanta is fun!”, but uses three arguments instead.

Click here to see the answer

Results

Some actions in Setanta return a value after they are called. We call this value the result of the action.

To see what I mean, here’s a quick example. Setanta has an action called “uas”. “uas” is short for “uasmhéid” which translates into English as “maximum”.

So what does uas do? uas is an action that takes 2 numbers as arguments and returns the maximum.

E.g. The result of uas(3, 2) is 3.

We can assign variables to the result of actions like

biggest := uas(3, 2)

In this case, the value of biggest will be 3. Try it out here:

Note that we could also write scríobh(uas(3, 2)), using the result of uas as the argument for scríobh directly

Example from before

In our first intro to Setanta we saw this code:

ainm := ceist("Cad is ainm duit?")
scríobh("Dia duit", ainm)

Now we know enough to see what’s going on here!

  1. First we use the ceist action to print “Cad is ainm duit?” on the console.
  2. The user will type their name into the console, and the result will be returned by ceist and stored in the ainm variable.
  3. Then we use the scríobh action to write “Dia duit” and the value of the ainm variable.

Try it out again to see this all in action!

Try expanding this program to ask the user for their age too

Stage actions!

Now that we’ve learned all about actions, we can finally take to the stage.

We use actions to draw shapes and manipulate the stage. There are a lot of actions at our disposal.

Let’s take at our first stage action “ciorcal” which translates as “circle”. This action allows us to draw circles on the stage. To access this action we have to type.

ciorcal@stáitse

We’ll see why we have to add the “@stáitse” part later on.

Try this code out:

Remember that you can use the tabs to switch between the console and the stage.

You should see that this code drew a black circle on the stage.

Drawing a black circle

To understand what the arguments “(200, 200, 50)” mean, we’ll take a look at coordinates.

Coordinates

Coordinates are a pair of numbers that describe a point on the stage. Every point is described by a unique pair of numbers. For example, the top-left corner is the point (0, 0).

The first number in the pair tells you how far to the right the point is, and the second number tells you how far down.

E.g. The point (10, 20) can be found by starting in the top-left, moving 10 units to the right, and 20 units down.

We call the horizontal direction the “x” direction, and the vertical direction the “y” direction.

When we call the ciorcal action, we pass in three arguments. The first is the “x” coordinate of the center of the circle, the second is the “y” coordinate of the center, and the final argument is the radius. So when we called ciorcal@stáitse(200, 200, 50) we were asking the Setanta interpreter to draw a circle on the stage, with the center at (200, 200) and radius 50.

Circle coords

Colours

What if want to use different colours? Luckily Setanta has an action just for that!

The “dath” action can be used to change the colour of the pen. As with the ciorcal action we call it with dath@stáitse. The dath action takes 1 argument, the colour you want to change to.

dath@stáitse("dearg")
dath@stáitse("buí")

By changing our earlier code to first change the colour to green, we get a green circle instead:

The dath action accepts colours in Irish and English, but also HTML colour codes

Other actions

You’re not just limited to drawing circles! Setanta has a host of other actions to allow you to draw other shapes. We’ll see more of them throughout the tutorial, but if you’d like to see the full list you can find it in English here or in Irish here.