Skip to content

zFunctionMachine Helper Library Documentation

Dan edited this page Nov 8, 2022 · 3 revisions

zFunctionMachine Helper Library Documentation

Make sure you add the zFunctionMachine library to your level. Use Ctrl+Click to add the library to your level!

Prerequisite: You also need the zCriterionValidationLibrary library in your level too. Use Ctrl+Click to add the library to your level!

Copy-and-paste this into each level you want to validate using this library

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();
  setupFunctionValidation(); // <-----------

  // addCriteria functions here

}
getHelperVars(); 
check();
updatePrevious();
updateFunctionValidation();

Functions

setupFunctionValidation()

Adds special variables to track specific behaviors with special function machine blocks created in the FunctionBlocks pool. This function is designed to be called just once, inside the if-statment when writing validation code.

updateFunctionValidation()

Keeps track of previous state of specific variables for the special function machine blocks. This function is designed to be called every frame, outside the if-statement when writing validation code.

checkValidation()boolean

Checks if we're in a level that involves validation. This function is designed to be called within the helper code for a custom block, not in validation code itself.

addFunctionBlock(blockObj)

Adds a new custom block to the special variable keeping track of things for the special function machine blocks. The block should be an object with properties, but the specific format can be whatever makes the most sense as long as it's consistent. This function is designed to be called within the helper code for a custom block, not in validation code itself.

setupFunctionValidation()

Adds special variables to track specific behaviors with special function machine blocks created in the FunctionBlocks pool. This function is designed to be called just once, inside the if-statment when writing validation code.

Kind: global function
Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();
  setupFunctionValidation(); // <-----------

  // addCriteria functions here

}
getHelperVars(); 
check();
updatePrevious();
updateFunctionValidation();

updateFunctionValidation()

Keeps track of previous state of specific variables for the special function machine blocks. This function is designed to be called every frame, outside the if-statement when writing validation code.

Kind: global function
Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();
  setupFunctionValidation(); 

  // addCriteria functions here

}
getHelperVars(); 
check();
updatePrevious();
updateFunctionValidation(); // <-----------

checkValidation() ⇒ boolean

Checks if we're in a level that involves validation. This function is designed to be called within the helper code for a custom block, not in validation code itself.

Kind: global function
Returns: boolean - true if we're in a level we want to validate and false otherwise. Does not check if the zFunctionMachine helper library has been added to the level.
See: addTwoBlock Custom Block
Example

function addTwoBlock() {
  //For Validation
  if(typeof checkValidation === 'function' && checkValidation()) { // <-------------
    var newBlockObj = {
      blockName: 'addTwo',
      input: input
    };
    addFunctionBlock(newBlockObj);
  }
  //Function
  input = input + 2;
  output = input;
}

addFunctionBlock(blockObj)

Adds a new custom block to the special variable keeping track of things for the special function machine blocks. The block should be an object with properties, but the specific format can be whatever makes the most sense as long as it's consistent. This function is designed to be called within the helper code for a custom block, not in validation code itself.

Kind: global function
See: addTwoBlock Custom Block

Param Type Description
blockObj object an object representing a custom block in this level.

Example

function addTwoBlock() {
  //For Validation
  if(typeof checkValidation === 'function' && checkValidation()) {
    var newBlockObj = { // <---- this is the custom block object
      blockName: 'addTwo',
      input: input
    };
    addFunctionBlock(newBlockObj);  // <-------------
  }
  //Function
  input = input + 2;
  output = input;
}

Functions

getFunctionBlocks()array

Gets the current block log for the custom function machine blocks.

checkNewFunctionBlockThisFrame()boolean

Checks if a new function machine block was used with an event.

getMostRecentFunctionBlock()Object

Gets the most recent function block object that occurred

checkThisFunctionBlockThisFrame(blockName)boolean

Checks if a specific function machine block was used with an event.

getFunctionBlocks() ⇒ array

Gets the current block log for the custom function machine blocks.

Kind: global function
Returns: array - An array of the blocks that have occurred so far in the level. Each element of the array is an object in a specific format.
See: Click here for an example Level that uses this function
Example

addCriteria(function() {
  var functionBlocks = getFunctionBlocks();
  return functionBlocks.length >= 1;
}, "noFunctionBlocks");

checkNewFunctionBlockThisFrame() ⇒ boolean

Checks if a new function machine block was used with an event.

Kind: global function
Returns: boolean - True if the function machine block was used with an event, false otherwise
See: Click here for an example Level that uses this function
Example

addCriteria(function() {
  //Check if the first sprite had an event that caused a function block to run
  return checkThisSpriteClickedThisFrame(spriteIds[0]) && checkNewFunctionBlockThisFrame();
}, "noFunctionBlocks");

getMostRecentFunctionBlock() ⇒ Object

Gets the most recent function block object that occurred

Kind: global function
Returns: Object - The function block object, which always has a blockName property but the other properties may vary based on the block.
Example

addCriteria(function() {
  //Check if a represent block had an input of 5 was added to a 'when run' block
  var block = getMostRecentFunctionBlock();
  return block.blockName == "represent" && block.input == 5;
}, "wrongRepresentBlock");

Example

addCriteria(function() {
  //Check if a represent block had an input of 5 was added to a click event
  var block = getMostRecentFunctionBlock();
  return checkNewFunctionBlockThisFrame() && block.blockName == "represent" && block.input == 5;
}, "noRepresentWithClick");

checkThisFunctionBlockThisFrame(blockName) ⇒ boolean

Checks if a specific function machine block was used with an event.

Kind: global function
Returns: boolean - True if the function machine block was used with an event, false otherwise

Param Type Description
blockName String the name of the block. Must be the same as the hard-coded name used when creating block objects within the block helper function (see addFunctionBlock() for an example)

Example

addCriteria(function() {
  //Check if the first sprite caused a represent block to trigger
  return checkThisSpriteClickedThisFrame(spriteIds[0]) && checkThisFunctionBlockThisFrame("represent");
}, "noFunctionBlocks");
Clone this wiki locally