Skip to content

OSL ‐ Subroutines

Mistium edited this page Jun 12, 2024 · 8 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" "input1" "input2"
  // 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.

These cannot be run from inside of imported files

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

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

Making a simple text splitter

method "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
endmethod

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

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 | OTS | ICN | OASM

Clone this wiki locally