Skip to content

Optionals

Optionals represent a value which could be nil, and can be applied to any datatype. Optionals are most often used from within a struct, where you might not want or need all fields to exist in the data you're sending.

function light.datatypes.optional

Shared Synchronous
1
2
3
function optional<Inner>(
    inner: Datatype<Inner>
): Datatype<Inner?>

Example

Let's say you want to represent some settings that have a value or be unset. A really simple and efficient way to encode that data is with a struct of optionals.

1
2
3
4
5
6
7
8
9
local ty = light.datatypes

local opt = ty.optional

local settings_packet = {
    field_of_view = opt(ty.range(60, 140)),
    depth_of_field = opt(ty.bool),
    sprint_key = opt(ty.)
}