Sprite Scripts
The scripts of a sprite are the meat and potatoes of a project. Each function or command equates to one or more scratch blocks that gets created on compile.
Stacking
Scratch blocks are able to stack on top of one another. This happens in scratch script automatically when two functions gets called after one another.
The way one would break this stacking is by placing a block that is not able to be stacked to break up the flow.
This would equate to two stacks that both are ran when the green flag is clicked. One stack with a move by 3 and another with a move by 4.
Opcodes
A Scratch Opcode is a specific identifier for a block. In scratch script you can create any scratch block by calling the opcode as a function.
A list of all opcodes used in Scratch Script can be found here
Aliases
An alias is a scratch opcode that has been renamed.
For example:
- motion_movesteps()
has been renamed to move
- looks_say()
to say
- event_whenflagclicked()
to start
Both the alias and the original are allowed, but aliases are meant to be easier to remember.
A good example of this is control_if
as it is renamed to if
to make if statements work
A list of all aliases can be found here
Data
Currently there are three types of data within Scratch Script: the var data type, list data type, and static data type.
Vars
The var data type is the built-in scratch variable data type. Vars are created on compile time, or in other words, cannot be created after it is compiled.
To create the var data type you can run the following:
This will initiate the variable (varName
) with the value 0. To specify a different value the following can be ran:
You can set the value of the variable by using the =
keyword.
You can then apply operations with the variable by using the +=
or ++
keyword.
These operations change the value of the variable (varName) by a value (10 or 1).
To get the value of a var, you can use the name of the variable in an expression.
In action this could look like:
Lists
By using the list keyword, a Scratch list is created similarly to a variable. It is created on compile time and cannot be created or deleted while the Scratch Project is running.
To create a list, use the list keyword:
Both of these scripts creates an empty list called listName that looks like this: []. I don't know which one is better so I will keep both.
If you want to initiate values or set them, you can simply use commas to deliminate different values at indexes.
To change a specific value at an index, you are able to do list indexing by doing the folloing:
Base Scratch is 1st indexed compared to most programming languages
In python:
In Scratch Script:
Attempting to change or get the 0th item does nothing.
You are currently not able to change the value at an index by a value
This does not work:
To get the value at an index you can index through it as if you were to modify it, but within an expression.
Static Vars
Similarly to the C programming language, static vars are accessable in all scopes (within the same sprite). The reference to "static variables" is a combination of static variables and pointers in c as static vars in Scratch Script can be deleted/freed.
Static vars are created at runtime and can be deleted, making them different from normal Scratch variables
Static vars, behind the hood, use the import
/require
keyword to add to a Scratch Script project
To learn more about the import
and require
keywords, go to the Importing page.
To make a static var, use the static keyword.
You can modify and asign values.
To get the value of a static var
You can delete a static var by using the del
keyword.
Deleting static variables is important
If you do not delete a static variable, errors can pop up in the console.
Functions
Functions are part of base scratch and can be implamented by using the func
keyword.
A function can have inputs parameters.
Function parameters can be of type:
-
text (numbers or strings)
-
bool (boolean expressions)
Functions can have as many inputs as you want and are deliminated by commas.
input1
is an input to funcName and is of type text.
input2
is an input to funcName and is of type bool.
You can then place blocks within the function. Those blocks can also call inputs.
Any static variables created in functions are deleted at the end of the function.
Experimental: Function Returns
Functions in base scratch are not able to return values.
However, by creating a static global variable, running the function, setting the value within the function, getting the value of the variable outside of the function, and deleting the static variable, it is possible to make function returns.
This technology is still in production and does not work with bools or multiple return values and may be buggy.