-
-
Notifications
You must be signed in to change notification settings - Fork 17
OSL ‐ Subroutines
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
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
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"
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"]
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.
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.
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.
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
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"
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!"
)
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]
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.
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
-
"pressed"
: Runs the event if a key is pressed. -
"=="
: Runs the event ifvalue1
is the same asvalue2
. -
"!="
: Runs the event ifvalue1
is different fromvalue2
.
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 is a web desktop gui with a self contained file system, programming languages, internet system and a whole lot of stuff an os should be able to do Use originOS here