Skip to content

OSL ‐ Subroutines

Mistium edited this page Aug 22, 2024 · 16 revisions

Def Command

Place this code in the setup part of your code before the mainloop label.

These can be run from inside of imported files

def "name" "this.input1,this.input2"
  // ensure the inputs are local to the function using this.var
  // code goes in here

  variable = "data"
  // this variable is global scope

  this.variable = "data"
  // only accessible inside this def
endef

name "hi" 123
// running the def

Local variables

In osl, this is a local object that you can manipulate and add to that has data only accessible inside of the current thread

def "hello"
  this.test_val = "hello world"
  log this
  error this.test_val
  // console.error(test_val)
endef

hello

log this.test_val
// logs "" because test_val is undefined

Custom methods

Place this code in the setup part of your code before the mainloop label.

def "input.method(input2)"
  // run your method code
  return "data"
endef

log "hi".method()
// this passes "hi" and null to the method and then logs "data"

Making a simple text splitter

def "text_data.makeLetters()"
  data = []
  // new array
  i = 0
  loop text_data.len (
    i ++
    // increment counter

    letter = text_data[i]
    // get letter of string

    data = data.append(letter)
    // push letter of string onto data array
  )
  return data
  // return the array
endef

log "hello".makeLetters()
// logs ["h","e","l","l","o"]

Accessing Commands, Functions and Methods

You can view an object of all the commands, functions and methods.

This function can be very useful for viewing functions defined programmatically, or by imported scripts.

Globals()

Custom Functions

In OSL, you can define custom functions to modularize and reuse your code. Functions can take multiple parameters, perform operations, and return results. These functions can be invoked with specific arguments and used throughout your script.

Defining a Custom Function

To define a custom function, use the def keyword followed by the function name and parameters. Inside the function, use this to ensure variables are local to the function context.

Example: Basic Arithmetic Function

Here’s how you can create a function that performs basic arithmetic operations and returns a result:

def "calculate(this.num1, this.num2, this.operation)"
  if this.operation == "add" (
    this.result = this.num1 + this.num2
  )
  if this.operation == "subtract" (
    this.result = this.num1 - this.num2
  )
  if this.operation == "multiply" (
    this.result = this.num1 * this.num2
  )
  if this.operation == "divide" (
    this.result = this.num1 / this.num2
  )
  return this.result
endef

log calculate(10, 5, "add")
// Outputs: 15
log calculate(10, 5, "subtract")
// Outputs: 5
log calculate(10, 5, "multiply")
// Outputs: 50
log calculate(10, 5, "divide")
// Outputs: 2

Example: String Manipulation Function

You can also define functions to perform operations on strings. Here’s an example of a function that reverses a string:

def "reverseString(this.text)"
  this.reversed = ""
  i = this.text.len
  loop this.text.len (
    this.reversed += this.text[i]
    i --
  )
  return this.reversed
endef

log reverseString("hello")  // Outputs: "olleh"

Example: Combining Functions and Events

You can define a function and then trigger it based on an event. Here’s an example where a function is called when a specific key is pressed:

def "greet(this.name)"
  this.message = "Hello," + this.name ++ "!"
  return this.message
endef

if "G".onpress (
  say greet("Alice")  // Outputs: "Hello, Alice!"
)

Example: Complex Data Processing

Custom functions can also handle complex data processing, such as filtering an array:

def "filterEvenNumbers(this.numbers)"
  this.evenNumbers = []
  for i this.numbers.len (
    if this.numbers[i] % 2 == 0 (
      this.evenNumbers = this.evenNumbers.append(this.numbers[i])
    )
  )
  return this.evenNumbers
endef

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
log filterEvenNumbers(data)  // Outputs: [2, 4, 6, 8, 10]

Summary

Custom functions in OSL allow you to encapsulate logic, improve code readability, and reuse code efficiently. You can define functions to perform a wide range of operations, from simple arithmetic to complex data manipulations, and trigger them based on events or direct calls.

Event Command

Place this code in the setup part of your code before the mainloop label.

Events are checked after your script finishes running.

event "value1" "condition" "value2"

endev

Conditions

  • "pressed": Runs the event if a key is pressed.
  • "==": Runs the event if value1 is the same as value2.
  • "!=": Runs the event if value1 is different from value2.

Example Use

def "test_command" "input1,input2"
    // Your custom command logic here
    log input1
    log input2
endef
test_command "hello" 10
event "space" "pressed"
  if touched_ground (
    y_velocity = 30
  )
endev

These commands and structures allow you to define custom commands and event-based logic in your OSL script, providing flexibility and organization in your code.

originOS Wiki

Wiki Views:
:views

OSL | RSH | ICN

Clone this wiki locally