Scripting... What?

This is just one of the questions you won't be asking after you've finished reading this blog!

I'm going to outline some of the basics in Brick Hill scripting that should hopefully kick-start you guys to start making some more interactive and fun games.

First of all, I think we should get familiar with the Workshop.
In order to script, you must click on a brick and click this button:
This change the 3D environment to a text-based one, where you can begin writing your amazing scripts. But, before we start that, there's some other changes you might want to make.
Once on this page, you can press F9 on your keyboard to select a different text editor. Usually I use Notepad++, as shown here:


Now you know how to open and edit scripts, let's start writing some!
To keep things simple, I'm going to begin with a simple countdown script that announces the start and end of a minigame, and respawns everybody when it's finished.

Making a Timer

First of all, we need to define some variables.
I've gone for MAX_TIME and TIME because we'll need to be able to know how far the timer has gone, and how far it needs to go. Both are global variables because other aspects of our game might depend on what the time is, so we can always call them from different objects:


Now we need something to continually reduce the timer. To achieve this, we can use the schedule() function.

Here, I have created a basic script that will eventually handle all of the timing for my minigame.
Right now, all my script does is subtract one from the time, then sets up a 1000 millisecond (1 second) schedule to execute itself.
Essentially, we have a loop that will cause our time to count down once per second.

While that's good, our timer won't stop at zero. In fact, it will keep going down to -1, -2, and so on... How can we stop that?

Our script is a bit more complex now, but hopefully you should be able to follow.
I've now added an if/else statement, where it checks if our timer has reached zero seconds yet (in other words, it is done counting down). If it has, it sets our time back to our MAX_TIME variable, then waits 10,000 milliseconds before counting down.
This means that there's a 10 second "lobby time" between each round.

This is all looking very nice, but how will our users know what the time is, or when our minigame is starting? Thankfully, we have lots of scripts that can do that;

In this script, I've added some basic messageAll() functions and a topPrintAll(). What this will do is, when the timer is 120 seconds (i.e. the start of our minigame) it will send everybody a chat message letting them know that it's a new round.
In each step after that, so long as the time is greater than zero (i.e. the round is still active) it will print at the top of the users' screen how long they have left.
At the end of the minigame, when our TIME = 0, it sends two chat messages letting them know that it's over, and how long they have to wait until the new round.
Cool, right? Let's see it in action!
All we need to do is add at the end of our script, otherwise the countdown won't start.


Nice work, now our game has a timer!
You can decorate your script with other things, such as respawning every player at the start of the minigame like this:

This cycles through all players and respawns them using their unique object id.

Our finished code:
global.MAX_TIME = 120;
global.TIME = 0;

script = '
if(global.TIME > 0) {
if(global.TIME == 120) {
messageAll("The minigame has started!");
with obj_client {playerRespawn(id);}
}
topPrintAll(string(global.TIME)+" seconds remaining",1);
global.TIME -= 1;
schedule(1000,script);
} else {
messageAll("The minigame is over!");
messageAll("Restarting in 10 seconds...");
global.TIME = global.MAX_TIME;
schedule(10000,script);
}
';
execute_string(script);