Topic category: User side tutorials
THE LOCAL VARIABLES OF THIS METHOD CAN ONLY BE USED BY THE “WAIT” BLOCKS YOU CREATE THEM IN AND NOT BY ANY OTHER PART OF THE PROCEDURE
If you ever tried to use local variables inside a Wait block you’ll have found yourself with an error message saying “Statement do does not support local variables.”: this happens because, when you use a Wait block inside a procedure, it creates a separate space and whatever runs inside it is not related to the original procedure, therefore any action you wanna perform inside the Wait block cannot count on the resources that were created inside and only for the original procedure, like local variables (which, as the name suggests, are limited to the space they are created in and they cannot go outside it in any way, unlike global variables).
To fix this, you just have to create the local variable inside the Wait block, so that everything running in there will have access to it, since they belong to the same area. Unfortunately, whenever you create a local variable in the simple way, this is automatically created at the top of the procedure’s code, and with this method it’s impossible to create it somewhere else like in the area of the Wait block, that’s why we have to use custom code to do that.
First you need to pick the “custom code snippet” action block from the Advanced Tab, place it inside the Wait block and then create your variable: the basic setup is: variable_type variable_name = variable_value; (don’t forget to add that semicolon (;) at the end or else the game will crash).
Here are a few examples: for number variables, you’d have to write double pizza(or any name you like) = 3; If you want to create a logic variable, type boolean pizza = true (or false); if you want to create a string variable, write String pizza = “Hello”; (Remember that string variables need to have their value enclosed in double quotes as shown in the example). These are basic variables, however you can create any variable the game can offer; if you don’t know how, you can create a local variable like usual, open the procedure’s code, find the variable you’ve just created (located at the top of the code sheet), copy its code line, then open the procedure, delete the variable (you only needed it to copy its code), then paste your code line inside the custom code snippet you put inside the Wait block.
If you need to get the variable’s value just pick a “custom code snippet” data block and type the variable’s name (in our case “pizza”), else if you wanna set its value pick a “custom code snippet” action block and do the same thing you did for creating it without writing the variable’s type (e.g. pizza = 2; instead of double pizza = 2;
In the photo there's a rough demonstration of how each step of the tutorial should look like: in the first block we are creating the variable, in the second one we are getting the variable value (in this case to build a text line) and in the third block we are setting the variable value (not creating another one)
In most cases, you want to use tick counter instead of wait block