Sprite Scripts
The scripts of a sprite are the meat and potatoes of a project. Each statement roughly equates to one scratch blocks that gets created on compile.
Stacking
Scratch blocks stack on top of one another. In Scratch Script, this happens automatically when there are two or more statments.
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
While this does work as the backend for most blocks, this is not the intended way of interacting with Scratch Script
Macros
Built into Scratch Script is a set of macros that maps opcodes to more common place names
For example:
-
motion_movestepshas been renamed tomove -
looks_saytosay -
operator_roundtoround
Both the alias and the original are allowed, but macros are meant to be easier to remember.
A list of all aliases can be found here
Entry Points
While stacking blocks on empty space is fun, placing them on something is better.
There are two types of entry points,
-
User defined functions
-
Scratch defined 'cap blocks'
Both types of entry points use the func keyword.
The difference is the name of the function declared.
The main function declared is a scratch defined entry point that aliases to the "when flag clicked" block.
You can have multiple main functions. They will all run asyncranicely. Good luck getting async to work tho.
User defined function names can be anything else that is not is a scratch defined entry point.
Data
Currently there are three types of data within Scratch Script: the var data type, list data type, and pointer 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 "": an empty string. 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 += keyword.
These operations change the value of the variable (varName) by a value (10).
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.
Both of these lines create 'delete all of list' and 'add item to list' blocks, which means that they do not update the default state of the list.
The difference between the two is that the first one creates a list to be used.
To change a specific value at an index, you are able to do list indexing by doing the folloing:
Base Scratch is 0st indexed compared to base scratch
In Scratch Script:
In normal Scratch:
Attempting to change or get the 0th item does nothing.
To get the value at an index you can index through it as if you were to modify it, but within an expression.
Pointers
Similarly to the C programming language, pointers are variables that point to positions in memory. In scratch this "memory" is a set of lists.
There are two types of pointers in Scratch Script: * Smart pointers (garbage collected) * Dumb pointers (not garbage collected)
The major difference between normal Scratch variables and pointers, is that pointers are created at runtime and can be deleted.
To use pointers, there must be a require "std.scratch"; line before the use of pointers
To learn more about the import and require keywords, go to the Importing page.
To make a smart pointer, use the ptr keyword.
You can modify and asign values.
To get the value of a static var
To make a dumb pointer, use the dptr keyword.
Everything a smart pointer can do, a dumb pointer can do.
Unlike smart pointers, you also have to manualy garbage collect them.
You can delete a dumb pointer by using the del keyword.
This automatically happens with a smart pointer.
Deleting dumb pointers is important
If you do not delete a dumb pointer, 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.
Functions can have as many inputs as you want and are deliminated by commas.
You can then place statements within the function. Statements inside of a function can use inputs.
Any smart pointers created in functions are deleted at the end of the function.
Function Returns
Functions in base scratch are not able to return values.
However, by the use of pointers, it is possible to pas in a reference to a pointer for it to be set inside of the function for it to be used outside of the function.
To use function returns, a function must return something.
To set a pointer to the function return, the << keyword is used.
You can create a pointer for the function return assignment, or you can use a pre-existing pointer.
The portion on the right side of the << must be a singular function call with nothing more.