Introduction to Editor Scripting
Editor Scripting is an experimental feature that can be used to build levels by programmatically placing blocks. The editor is currently available as of Fancade 1.12, on Fancade Web, by entering the EditorScript
command in the command line.
Getting Started
Let's create a new level and enter the EditorScript
command. You will be greeted by a simple code editor with some examples in place. The code might seem unfamiliar at first, because it is written in JavaScript. But don't be scared, because we will learn the basics in this tutorial!
(*Note: you can press π to see further details about the topic from linked mdn web docs)
πFunctions
So let's look at our first example.
// Set block at position (x, y, z)
setBlock(x, y, z, prefabIndex);
We can ignore the first line, since it begins with //
. This makes the editor ignore any code in the current line, but it can give us some hints on what the following line does.The second line starts with the function
setBlock
. You can imagine this as placing a script block with the aforementioned name. The following text represents the inputs given to this function, separated by ,
and enclosed by brackets. So to put this in visual scripting terms, we are placing a block called setBlock
and give it the inputs x
, y
, z
and prefabIndex
.
But those inputs were actually just placeholders, so let's replace them with some actual values!
// Set block at position (0, 0, 0) in level or open block
setBlock(0, 0, 0, 3);
Copy this code and paste it in the editor, then hit Run
and Save and Close
. As you might have guessed, this puts a block on the coordinates (0, 0, 0)
. The fourth value is the unique identifier for the Grass block to be placed. There is a list of unique ids for each built-in block, but what if we want to place a custom block instead?
Creating Functions
We have already seen some built-in functions, but what if we wanted to create our own ones?
function moveBlock(fromX, fromY, fromZ, toX, toY, toZ) {
// Get block at position (fromX, fromY, fromZ)
let block = getBlock(fromX, fromY, fromZ, block);
// Remove block at position (fromX, fromY, fromZ)
setBlock(fromX, fromY, fromZ, 0);
// Set block at position (toX, toY, toZ)
setBlock(toX, toY, toZ, block);
}
This time, the line starts with a function
expression, followed by the name of the function we want to create and the name of the inputs the function takes. The code inside the curly brackets is run when we run the function:
moveBlock(0, 0, 4, 1, 0, 4);
You will see that this function we have just created moves the block at (0, 0, 4)
to (1, 0, 4)
. This way, we can simplify our code by putting common tasks in a block of code that we can use later.
Strings
Create a custom block, for instance My Block
. Then replace your previous code with this one:
// Find the block called "My Block"
findBlock("My Block");
You can once again see a function, but this one is called findBlock
. In contrast to the previous function, this one doesn't take numbers as input. Instead it takes a block of text, called a string, which is encapsulated by a pair of quotes. We can now run this code, but it will do nothing at first, because we haven't used the output of the function anywhere. We can change this by storing the output in a variable!
πVariables
// Find block by name and store it in a variable
let myBlock = findBlock("My Block");
setBlock(0, 0, 0, myBlock);
This line is different, because it doesn't start with the name of a function. Instead, we can see the let
keyword, which creates a new local variable for us. The name of the variable is determined by the next word, myBlock
. To store a value in the new variable, we follow the variable name with a =
and the value to store, similar to a Set Variable block. In this case, this value is the output of the function we have used before.The next line looks familiar and it once again places a block at the coordinates
0, 0, 0
. But this time we don't directly input the id of our block, but instead we use the value stored in the variable which we have created before. This allows us to not only store any kind of value, but also use it in multiple other places.After we have created the variable, we can change its value by adding following code in order to set a new custom block next to our previous block:
// Change the value of the variable
myBlock = findBlock("My New Block");
setBlock(1, 0, 0, myBlock);
But what if we want to store multiple blocks in our variable?
πArrays
// Create an empty array
let myBlocks = [];
// Store block ids in the array
myBlocks[0] = findBlock("My Block");
myBlocks[1] = findBlock("My New Block");
Like previously, we are creating a new variable. But this time, we instead store an empty list []
in it. We then add values to the array by assigning them to the indexes in curly braces. This works similar to a List Element block and is also used to access the values afterwards:
// Set blocks from the list
setBlock(0, 0, 1, myBlocks[0]);
setBlock(1, 0, 1, myBlocks[0]);
Additionally, we can create entire arrays in just one line, similary to how we pass our inputs:
// Create a filled array
let myBlocks = [
findBlock("My Block"),
findBlock("My New Block")
];
This way, we can store multiple values of any type, including arrays themselves! This can be used to create simple multidimensional lists.Now what if we wanted to place all our blocks at once?
πLoops
// Set block for each block in the array
for (let index of myBlocks) {
setBlock(index, 0, 2, myBlocks[index])
}
This time, the line starts with a for
loop expression. Inside this expression, we create a variable index
and assign it to each index in the array. The code inside the curly braces will then be repeated for each new index. This is similar to using a Loop block with the array length as input.
Take note that there are also other kinds of loop iterators. In our example, we have used a forβ¦of statement.
πObjects
// Create an empty object
let myBlocks = {};
// Store block ids in the object
myBlocks["My Block"] = findBlock("My Block");
myBlocks["My New Block"] = findBlock("My New Block");
Objects {}
are quite similar to arrays. However, they instead use strings as an index. For instance, this allows you to store blocks based on their name instead of their id.
// Set blocks from the object
setBlock(0, 0, 3, myBlocks["My Block"]);
setBlock(1, 0, 3, myBlocks["My New Block"]);
This also allows you to create a structured group of values, called properties, which represents as single item:
// Create object with custom properties
let myBlock = {
x: 0,
y: 0,
z: 4,
id: findBlock("My Block")
};
If the name of the property can be considered a variable (it means it doesn't contain illegal characters such as spaces), after the variable name, instead of using [""]
, we can instead use .
and the property name next to it:
// Set block by accessing custom properties
setBlock(myBlock.x, myBlock.y, myBlock.z, myBlock.id);