Step 1: Creating our list of data to search through
We'll need some data to search through and sort. Data is stored in a database, and a list (also known as an array) is a very simple database. For this example we'll make a short list of different sized rectangles by drawing each rectangle in a loop that creates a certain amount rectangles, and then adding each rectangle to our list of rectangles.
We'll start by putting a game object in an empty level. This object will only sit there so that the script on it can create our rectangles. Add a script to the object.
To make our list of rectangles we'll create a function. A function is basically a chunk of repeatable instructions. Using functions can help you to figure out the outline of your algorithm before you start actually using program instructions. If you name your functions to describe each step of the algorithm, then you can make sure it all makes sense. For example: When the level starts, "create a list of rectangles," "Target a rectangle to search for".
To create this, grab "to do something" from the Functions tab, and drag it out into the workspace. Rename this function "Create a list of rectangles and draw them". This is a 'function definition', because it defines what the function will do.
Then grab the "When created" block from Events, and drag that above the function. Drag out another function, and name this "Target a rectangle to search for", and drag that function underneath the first one.
Now go to the Functions tab and grab the smaller green blocks called "Create a list of rectangles and draw them" and "Target a rectangle to search for". These are the 'function calls', because they call the function to run. Drag "Create a list of rectangles and draw them" into "When created", and then drag "Target a rectangle to search for" underneath that. This is the basic outline of the first part of our program.
Let's define what our first function will do. We want to create a rectangle, set how long it is, set it's x-position, set how far away the next rectangle will be placed, and then repeat this until we have a bunch of rectangles that increase in height.
We'll start by setting up a few variables for this function so that we've defined:
- The list of rectangles (list variable)
- The height of the shortest rectangle (number variable)
- The height of the tallest rectangle (number variable)
- The x-position of the rectangle (number variable)
- The size of the gap between each rectangle (number variable)
These variables can be properties (medium green) or local variables (light green), but you need to remember that local variables will only hold their value inside the event or function that they are set inside, while property variables will retain their value anywhere in this script. A local variable is used for storing temporary information only. Since we need the list of rectangles, and the shortest and tallest height numbers to be useable in other functions and events, we'll make them properties.
Grab five "set true/false i to" blocks from the Properties section of the Variables tab, and drag them into our "Create a list of rectangles and draw them" function definition. Make one of them a list named "The list of rectangles", and make the other four number variables named "height of shortest rectangle", "height of longest rectangle", "rectangle x-position", and "rectangle gap".
If you need to see a closer view of any image, just click on it!
Now we'll give these variables some values. The list variable can start as an empty list, so grab "create empty list" from Operators, and drag it into the list variable slot. The shortest rectangle can be 100, so grab "0" from Operators, drag it into the variable slot, and change it to 100. Also set the tallest rectangle to 150, rectangle x position to 120, and rectangle gap to 10.
Once we've set up our variables, we can move on to actually creating the rectangles. We'll use a new block called a counting loop that will allow us to repeat instructions until the amount of repetitions hits the limit of the counter. Grab "count with i from 1 to 10 by 1" from Control Flow, and drag it underneath the variables we created before.
Click on "i" and rename it to "height". This loop will increase the height of each rectangle by 1 each time it loops. We'll get it to start looping from the shortest height rectangle, up to the longest rectangle. This will make it loop through 50 times, and create 50 rectangles. Replace 1 with "height of shortest rectangle" (from Variables), and replace 10 with "height of longest rectangle" (from Variables).
To create the rectangles, grab the block "Create new rectangle with width/height of 100 100" from Draw, and drag this block into the counting loop. The counting loop will count up by 1 each time it loops, and the number of counts is kept track of in a local variable that we've renamed to "height". Grab the local variable "height" from Variables, and drag it into the second 100 in the rectangle block. Also change the first 100 to 5, since we don't want the rectangles to be too wide.
Next we'll set the x and y position of the rectangle, so grab two "set x position of myself" blocks from Transform, and drag them both under the "create new rectangle" block. Change one of them to "set y position of myself". Since we want to set the position of the rectangle, replace both "myself" blocks with "instance rectangle" from Variables. Change the x position to "number rectangle x position" (from Variables), and change the y position to 200.
Now that the rectangle is created and positioned, we'll increase the variable "rectangle x position" by the amount "rectangle gap", so that the next loop will create another rectangle further along. Grab "set number rectangle x position", "number rectangle x position", and "number rectangle gap" from Variables, and also grab the "+" block from Operators. Join these together to make "set number rectangle x position to number rectangle x position + rectangle gap", and drag these blocks underneath the positioning blocks.
Lastly, we'll make sure this new rectangle that's been created gets added to our list of rectangles. Grab the block "in list 'list list' add _ to the end" from Operators, and drag it underneath the previous block. Replace "list list" with "list list of rectangles" (from Operators), and place "instance rectangle" (from Operators) in the gap.
This loop will complete itself as fast as the computer is able to, so you won't actually see rectangles being created one by one - they'll appear all at once.
Hit Play to watch your program run. Make sure to give the script a name like "Searching through rectangles".
After you've played it, it would be a good idea to return to the Level Editor and Save your game - you don't want to lose any work!