OSL ‐ Methods - Mistium/Origin-OS GitHub Wiki

About OSL Methods

OSL methods are pieces of code that you append onto data or other methods to modify their output.

For example, "hello".len will not return "hello" anymore but instead, it will return the integer 5.

Key Pressed Methods

Code-Based Inputs:

  • "key".pressed: Returns true if a "key" is currently held down, otherwise returns false.

    • Example: Checks if the space key is currently held down.
      if "space".pressed (
          // Your code here for when the space key is pressed
      )
      
  • "key".onpress: Returns true on the same frame that a "key" is pressed, otherwise returns false.

    • Example: Checks if the 'a' key is pressed on the frame.
      if "a".onpress (
          // Your code here for when the 'a' key is pressed
      )
      

Boolean Methods

  • .not : Flips a boolean value.

    • Example: Flips the boolean value true.

      true.not // Returns false
      
    • Example: Flips the boolean value stored in a variable input_data.

      input_data.not // Returns the opposite of the boolean value stored in input_data
      
  • .contains("string") : Checks if the input contains a specific string.

    • Example: Checks if the string "apple" contains the letter 'p'.
      "apple".contains("p") // Returns true
      
  • .istype("type") : Checks if the input is of a specific data type.

    • Example: Checks if the variable "hello" is of type "string".
      "hello".istype("string") // Returns true
      
  • .is(input) : Compares two values for equality.

    • Example: Compares the string "apple" with the string "orange".
      "apple".is("orange") // Returns false
      
  • .defined : Checks whether the variable with the specified name has any data.

    • Example: Checks if the variable myVar is defined.
      myVar = "Hello World"
      say myVar.defined
      Returns true
      
  • .matchregex("regex") : Returns true or false based on whether the input string matches the input regex.

    • Example: Checks if the string "123" matches the regex pattern for digits.
      "123".matchregex(/\d+/) // Returns true
      

Image Functions

  • url.imageinfo("loaded") : Checks if the input value URL of an image has been loaded or not.

    • Example: Checks if the image at "https://example.com/image.jpg" is loaded.
      "https://example.com/image.jpg".imageinfo("loaded") // Returns true if the image is loaded
      
  • url.imageinfo("width") : Returns the width of the inputed image if it is loaded.

    • Example: Retrieves the width of the image at "https://example.com/image.jpg".
      "https://example.com/image.jpg".imageinfo("width") // Returns the width of the loaded image
      
  • url.imageinfo("height") : Returns the height of the inputed image if it is loaded.

    • Example: Retrieves the height of the image at "https://example.com/image.jpg".
      "https://example.com/image.jpg".imageinfo("height") // Returns the height of the loaded image
      

String Methods

  • .len : Returns the length of the input.

    • Example: Returns the length of a string.
      "hello".len
      // Returns 5
      [50,3].len
      // Returns 2
      
  • .type : Returns the data type of the input.

    • Example: Returns the data type of the string "hello".
      "hello".type
      // Returns "string"
      
  • .contents : If the input is a variable name, it returns the value of that variable; otherwise, it returns the input itself.

    • Example: Returns the value of the variable myVar.

      "myVar".contents
      // Returns the value of the variable myVar
      
    • Example: Returns the value of the variable at index 1.

      1.contents
      // Returns the value of the variable at index 1
      
  • .reverse : Returns the input string or array in reverse.

    • Example: Returns the string "hello" in reverse.
      "hello".reverse
      // Returns "olleh"
      
  • .index() : Returns the index of a string inside another string.

    • Example: Returns the index of "u" in "input".

      "input".index("u")
      // Returns 4
      
    • Example: Returns the index where "np" starts in "input".

      "input".index("np")
      // Returns 2
      
    • Example: Returns 0 if "daf" is not contained in "input".

      "input".index("daf")
      // Returns 0
      
  • .replace(in1,in2) : Returns the input string with occurrences of in1 replaced with in2.

    • Example: Replaces "p" with "b" in the string "apple".
      "apple".replace("p", "b")
      // Returns "abble"
      
  • .regex("in1") : Matches the input string against a regular expression and returns an array of matches.

    • Example: Matches digits in the string "hello123".
      "hello123".regex(/\d+/)
      // Returns ["123"]
      
  • .left(in1) : Gets a substring from the left of the input string.

    • Example: Gets the first 2 characters from the string "apple".
      "apple".left(2)
      // Returns "ap"
      
  • .right(in1) : Gets a substring from the right of the input string.

    • Example: Gets the last 3 characters from the string "apple".
      "apple".right(3)
      // Returns "ple"
      

Number Methods

  • .abs: Returns the absolute value of any input.

    • Example: Returns the absolute value of -5.
      -5.abs
      // Returns 5
      
  • .clamp(number-low, number-high): Clamps the input value between the specified "number-low" and "number-high" values.

    • Example: Clamps 10 between 0 and 5.
      10.clamp(0, 5)
      // Returns 5
      
  • .sqrt: Returns the square root of the input.

    • Example: Returns the square root of 25.
      25.sqrt
      // Returns 5
      
  • .sign: Returns the sign of the input.

    • Example: Returns the sign of 100.

      100.sign
      // Returns "+"
      
    • Example: Returns the sign of -40.

      -40.sign
      // Returns "-"
      

Trigonometric Methods:

  • .sin: Calculates the sine of the input in degrees.

    • Example: Calculates the sine of 90 degrees.
      90.sin
      // Returns 1
      
  • .cos: Calculates the cosine of the input in degrees.

    • Example: Calculates the cosine of 0 degrees.
      0.cos
      // Returns 1
      
  • .tan: Calculates the tangent of the input in degrees.

    • Example: Calculates the tangent of 45 degrees.
      45.tan
      // Returns 1
      

Rounding Methods:

  • .round: Rounds a number to the nearest integer.

    • Example: Rounds 3.6 to the nearest integer.
      3.6.round
      // Returns 4
      
  • .ceiling: Rounds a number to the nearest and largest integer.

    • Example: Rounds 3.2 to the nearest and largest integer.
      3.2.ceiling
      // Returns 4
      
  • .floor: Rounds a number to the nearest and smallest integer.

    • Example: Rounds 3.8 to the nearest and smallest integer.
      3.8.floor
      // Returns 3
      

Arithmetic Operations:

  • .multiply(int): Multiplies a number by the specified integer.

    • Example: Multiplies 2.5 by 2.
      2.5.multiply(2)
      // Returns 5
      
  • .divide(int): Divides a number by the specified integer.

    • Example: Divides 10 by 2.
      10.divide(2)
      // Returns 5
      
  • .add(int): Adds an integer to the number.

    • Example: Adds 2 to 3.
      3.add(2)
      // Returns 5
      
  • .minus(int): Subtracts an integer from the number.

    • Example: Subtracts 2 from 7.
      7.minus(2)
      // Returns 5
      

Other

  • .dist(,): Finds the distance between two (x, y) coordinate pairs.

    • Example: Calculates the Euclidean distance between two points represented by (x1, y1) and (x2, y2).
      [10,5,10,0].dist()
      // Returns 5
      
      or
      [10,5].dist(10,0)
      // Returns 5
      
  • .getcol: Gets the color at a specified (x, y) coordinate within the current frame.

    • Example: Retrieves the color at the specified (x, y) coordinate within the current frame.
      [100,50].getcol
      // Returns the color at x=100, y=50 within the current frame.
      
  • .new(""): Creates some data with a specific type.

    • "Array" : Creates an array with a specified length, filled with empty strings.
      "Array".new(5)
      // Returns ["","","","",""]
      
    • "String" : Creates a string with a specified length, filled with spaces.
      "String".new(5)
      // Returns "     "
      
    • "Vector" : Creates a vector (array) with a specified length, filled with zeros.
      "Vector".new(3)
      // Returns [0,0,0]
      
  • .getall(""): Extracts data.

    • "Letters" : Extracts individual letters from a string.
      "hello!".getall("letters")
      // Returns ["h","e","l","l","o","!"]
      
    • "extract-letters" : Extracts only letters from a string, removing any non-letter characters.
      "123, how are you? 8".getall("extract-letters")
      // Returns "howareyou"
      
    • "extract-numbers" : Extracts only numbers from a string, concatenating them together.
      "123, how are you? 8".getall("extract-numbers")
      // Returns 1238
      
    • "extract-special" : Extracts special characters from a string.
      "123, how are you? 8".getall("extract-special")
      // Returns ",   ? "
      
    • "keys" : Extracts keys from an object (dictionary).
      {"hello":"world"}.getall("keys")
      // Returns ["hello"]
      
    • "values" : Extracts values from an object (dictionary).
      {"hello":"world"}.getall("values")
      // Returns ["world"]
      
    • "datas" : Extracts key-value pairs from an object (dictionary) as arrays.
      {"hello":"world","test":"value"}.getall("datas")
      // Returns ["hello","world"],["test","value"](/Mistium/Origin-OS/wiki/"hello","world"],["test","value")
      
    • "get a list of keys inside a json array": Extracts a specific property value from objects within a JSON array.
      [{"id":12},{"id":24}].getall("id")
      // Returns [12,24]
      
  • .to("") : Converts data to different formats.

    • "upper" : Converts the input string to uppercase.

      "apple".to("upper")
      // Returns "APPLE"
      
    • "lower" : Converts the input string to lowercase.

      "APPLE".to("lower")
      // Returns "apple"
      
    • "title" : Converts the input string to title case.

      "apples are great".to("title")
      // Returns "Apples Are Great"
      
    • "mixed" : Converts the input string to mixed case.

      "APPLES ARE GREAT".to("mixed")
      // Returns "ApPlEs aRe gReAt"
      
    • "binary" : Converts text to binary.

      "apple".to("binary")
      // Returns binary representation
      
    • "base64" : Converts text to base64.

      "apple".to("base64")
      // Returns base64 representation
      
    • "unicode" : Converts characters to Unicode IDs.

      "A".to("unicode")
      // Returns Unicode ID
      
    • "md5"/"sha1"/"sha256"/"sha512" : Hashes the input.

      "apple".to("md5")
      // Returns MD5 hash
      
    • "lz_raw" : Compresses the text with LZ compression.

      "apple".to("lz_raw")
      // Returns compressed text
      
  • .from("") : Converts data from different formats.

    • "binary" : Converts binary to text.

      "01100001".from("binary")
      // Returns "a"
      
    • "base64" : Converts base64 to text.

      "YXBwbGU=".from("base64") // Returns "apple"
      
    • "unicode" : Converts Unicode IDs to characters.

      "65".from("unicode")
      // Returns "A"
      
    • "lz_raw" : Decompresses LZ compressed text to plain text.

      compressedText.from("lz_raw")
      // Returns decompressed text
      

Array Functions

Input Functions

  • .ask : Asks the user for text input (pauses the script until the user enters a value).

    • Example: Prompts the user to enter their name and assigns the input to the variable userInput.
      userInput = "Enter your name: ".ask