In most of my programs, I need a variable that saves the starting position. For example, in Handy Compass I needed a reference point from which to print the four directions. I call such a reference point “origin.” Origin is the player world position except that it is always on the ground even if the player is flying or standing on a structure. Origin is on the ground where the player’s feet would be if the player standing on the ground. The X and Z coordinates of origin are the same as the player world position. The function setOrigin is designed for a flat world.
Sometimes I run multiple programs at the same time, for example, The Art of Random Walks. In such a case, I usually want origin to be the same for all programs and set only once. This is accomplished in the following two steps:
- Make origin a global variable by declaring it outside all function and on chat blocks. Since origin is a position variable, it should be set to null, which means it has no value. The variable origin cannot be set to zero because it is a position variable. A position variable consists of 3 values, one for each of the e 3 coordinates X, Y and Z. In MakeCode blocks, global variables should be initialized in an on start block.
- In the setOrigin function, or any other program that might set the origin variable, first test to see if origin already has a value. Only if it is null (does not contain a position) should origin be (see code below).
The on start block below sets the variable origin to null when the start button is pressed. I program my on start block as is shown on the left below; however, MakeCode usually, eventually changes it to be as shown on the right. The two are equivalent, so I don’t get too excited.
The on chat command below is used to test the function. It calls setOrigin, then places a gold block under origin, which is be level with the grass in a flat world.
In the JavaScript below, note that origin is set outside the onChat and the function; therefore, it is a global variable and can be used by both the run on chat and the setOrigin function. Also notice the use of the value “null.” I have not found such a value in the MakeCode blocks, so I switch to JavaScript to set it.
Get the Code
Remember Where You Started code is shared on Code Connection at this URL
https://makecode.com/_3wLH9K3Ashg1
To get and use the code, follow these steps.
Click the Import button , which is upper right in the Code Connection window just below the banner. This will open the window shown below.
Click the Import URL button , which is on the right, to open the window shown below.
Paste the URL supplied for the program you want to use in the space under the text “Copy the URL …”
Click the Go ahead! button .
The next window you will see will be the MakeCode window with the code downloaded from the URL. At this point, you can treat it like any other code, e.g., run it, save it locally, modify it, publish your changes or whatever else your heat desires.
We have tested several other methods of downloading the code using the URL, for example, pasting the URL in a browser. No joy. For more detailed instruction see our post How to Use Shared MakeCode on Microsoft Code Connection for Minecraft.
Pingback: Builder Fence with Size and Block Arguments in MakeCode for Minecraft | We Code MakeCode
Pingback: More Grandma’s Chickens 2.0: A Makecode for Minecraft Game | We Code MakeCode