Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
Tutorial showing a list of utility functions for AdventureJS. tutorial, custom code, utility functions

Advanced Scripting:Utility Functions

Utility functions are distinguished from public functions because they are methods that exist on the AdventureJS object rather than on the game object. This means that you call them like this:

adventurejs.pickRandom(["a", "b", "c"])
// you can also shorten adventurejs to A 
A.pickRandom(["a", "b", "c"])

Instead of like this:

MyGame.createAsset({ class: "Lantern", name: "brass lantern"})

The main difference between utility functions and game functions – besides the way they're called – is how they're scoped, which is a way of saying, what they know, what information they have access to. Utility functions don't automatically know your game scope – meaning, they don't have access to game state and assets, like this function that can find an object in your game: MyGame.$("brass lantern")

AdventureJS has many utility functions. Authors may use any of them, though they're not all author-friendly. The ones shown here are ones you may find useful.

  • A.arrayToLowerCase() {Array} array Array Takes an array of strings, converts them to lowercase, and returns the array.
  • A.camelToSnake() {String} string String Converts a string from camelCase to snake_case.
  • A.capitalize() {String} string String Capitalizes the first character of a string.
  • A.convertVolume() {Number|String} volume - in milliliters {String} context_id - id of the Vessel containing the substance Number Converts a string or number into milliliters. Accepts a number representing volume in milliliters, or a string to be converted to milliliters (ex: "100ml"), in nL, μL, mL, cL, dL, L, daL, hL, kL, ML.
  • A.countCommonElements() {String} str1 {String} str2 Number Count the number of common items between two comma delimited strings.
  • A.encrypt() {String} text {String} key String Very simple encryption method encrypts a string using a provided key. Used to encrypt point / hint data so it's not human readable.
  • A.decrypt() {String} text {String} key String Decrypts a string encrypted with A.encrypt() using a provided key. Used to decrypt point / hint data.
  • A.deserialize() {String} name String Deserialize strings that were serialized using serialize(). Reverses serialize function. Convert '_' to ' '.
  • A.diff() {Object} baseline_object {Object} updated_object Object Diff function for comparing two objects. Primarily used for comparing the world state against the baseline during save & restore operations.
  • A.getHorizontalDistance() {Object} point1 {Object} point2 String Calculate the distance between the x/z of two points.
  • A.getVerticalDistance() {Object} point1 {Object} point2 String Calculate the distance between the y of two points.
  • A.hasDuplicates() {Array} array {String} string Boolean Takes an array and returns whether it contains any duplicate items.
  • A.isFalseOrNull() {*} value Boolean A simple function to check whether the provided value === false or null.
  • A.pickRandom([]) {Array} an array of strings String pickRandom() accepts an array of strings and returns a random string.
  • A.propercase() {String} string String Converts a string to Propercase (lower case with leading cap).
  • A.sentencecase() {String} string String Capitalizes the first char of a string, leaving the remaining character untouched.
  • A.serialize() {String} string String Convert an arbitrary string to an id useable by game objects. Converts to lowercase and converts ' ' to '_'.
  • A.serializeArray() {Array} array Array Convert an array of arbitrary strings to an array of IDs useable by game objects. Convert to lowercase and convert ' ' to '_'.
  • A.stringToArray() {String} string String Convert a string into an array containing the string.