FANDOM


The easiest way to make reusable scripts is to create scripts which retrieve information about the block/item that you want to alter and use it to give you the result. This is especially useful if you want to increment the metavalue of a block without knowing the block's id or current metadata. This is especially useful if used for a crop, since you can give use the script for every non-adult version of the crop without knowing the value for the freshly planted metadata or the fully grown version of the crop. Here is an example of what this script could look like:

if(Math.floor(Math.random()*100) < 5) {
   world.setBlockMetadata(position, world.getBlockMetadata(position) + 1);
}

This script gives a 5% chance every time the script is called to increase the metadata of the block by 1. The growth speed of a plant with this kind of script would be based on tickrate of that plant. This script could become even more reusable by incorporating a variable outside of the script to use as the chance to grow per run, as 5% may be too fast or slow for certain plants.

Variables outside of script

If you want to reuse scripts that require variable data then you have to pass the value to your script by setting the value from outside of the script. The easiest method for this is to create and set the value of a variable before calling the script. As long as you do not also initialize the value inside the script, you can use this to make very reusable scripts. Here is a simple example:

onActivated[0] = "var block = 35; var blockmeta = 10; var height = 7; var width = 3; mod.loadScript('squarePillar.js');";

in squarePillar.js:

for(var i=0; i<height; i++) {
   for(var j=0; j<width; j++) {
         for(var k=0; k<width; k++) {
            world.setBlockIdAndMetadata(position, block, blockmeta);
            z++;
         }
      z -= width;
      x++;
      }
   x -= width;
   y++;
}
y -= height;

This method was used on the cherry blossom tree's example page for the tree growth script, making a tree growth script that can be reused for multiple trees without modification.

Calling Functions

One of the problems of the previous method was that as you get more variables set outside of the script, your call to the function will start getting quite lengthy. Additionally, you will most likely end up with a crash if you accidentally type any of the variable names wrong. This can be solved by writing your script as a function instead of loose code. Interestingly enough, this causes the code to not immediately run from just loading the script. This is how the previous script would look if it was written as a function:

onActivated[0] = "mod.loadScript('squarePillar.js');drawSquarePillar(35, 10, 7, 3);";

in squarePillar.js:

function drawSquarePillar(block, blockmeta, height, width) {
   for(var i=0; i<height; i++) {
      for(var j=0; j<width; j++) {
         for(var k=0; k<width; k++) {
            world.setBlockIdAndMetadata(position, block, blockmeta);
            z++;
         }
         z -= width;
         x++;
      }
      x -= width;
      y++;
   }
   y -= height;
}

This version is much simpler to reuse, as you don't need to retype the variable names each time you use the function. Additionally, this can be taken even further with the use of the arguments object. An example using the arguments object might be added to this page at a later time, but for now, please read the linked page if you want more information.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.