LSL has these casting functions. They are not exposed directly in SLua. Here's what I've been replacing them with:
  • (integer)x
    ->
    math.modf(tonumber(x) or 0)
  • (float)x
    ->
    tonumber(x) or 0
  • (string)x
    ->
    tostring(x)
  • (vector)x
    ->
    tovector(x) or ZERO_VECTOR
  • (rotation)x
    ->
    toquaternion(x) or ZERO_ROTATION
  • (key)x
    ->
    touuid(x) or NULL_KEY
My
(string)
,
(vector)
, and
(rotation)
replacements seem to behave identically in all cases I've thought of. My
(key)
replacement behaves identically in all cases I actually care about. It's behavior doesn't match when the string is not a valid uuid, but, I actually prefer the new behavior, so I'm happy with the replacement.
However, the
(integer)
replacement is a bit cumbersome. Additionally,
(integer)
and
(float)
have some very convenient behavior when you're parsing real notecards that aren't trivial to replicate using the luau standard library. My replacements get close, but don't behave the same in all cases:
default { state_entry() {
llOwnerSay((string)((integer)"0xf")); // 15
llOwnerSay((string)((integer)" 99.5 red baloons")); // 99
llOwnerSay((string)((float)" 99.5 red baloons")); // 99.500000
llOwnerSay((string)((float)" -99e2 red baloons")); // -9900.000000
}}
In SLua, we still have indirect access to this behavior via
ll.List2Integer
and
ll.List2Float
:
print(ll.List2Integer({"0xf"}, 1)) -- 15
print(ll.List2Integer({" 99.5 red baloons"}, 1)) -- 99
print(ll.List2Float({" 99.5 red baloons"}, 1)) -- 99.5
print(ll.List2Float({" -99.5e2 red baloons"}, 1)) -- -9950
print(tonumber("0xf")) -- 15
print(tonumber(" 99.5 red baloons")) -- nil
print(tonumber(" -99.5e2 red baloons")) -- nil
print(tonumber("-99.5e2")) -- -9950
But it would be nice to have a direct conversion, or even better, one that replicated all the oddities of the lsl casts, but returned
nil
rather than
0
on failure. Also, such functions might be necessary for the future LSL -> SLua transpiler