sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

how to: safe property accessor chains

Open fhoenig opened this issue 2 years ago • 2 comments
trafficstars

Is there a suggested way to make chains of properties "safe" in a reasonably low overheard way?

example: local a = thing.transform.position.x where thing, transform and position is a user type, but each of them can be optional.

let's say transform does not exist on thing as determined on the C++ side.

fhoenig avatar Sep 18 '23 08:09 fhoenig

You can use something like that:

local thing = {
  transform = {
    position = {
      --x = 1
    }
  }
}

local foo = (((thing or {}).transform or {}).position or {}).x
print(foo)

skaarj1989 avatar Sep 21 '23 19:09 skaarj1989