Difference between revisions of "User:SuzannaLinn Resident/LuaMovingTo"

From Second Life Wiki
Jump to navigation Jump to search
Line 109: Line 109:
=== LSL integer ===
=== LSL integer ===


Used as a boolean value, with contants <code>TRUE</code> and <code>FALSE</code> will be a <code>boolean</code>, with values <code>true</code> and <code>false</code>.
Used as a boolean value, with contants <code>TRUE</code> and <code>FALSE</code> is a <code>boolean</code>, with values <code>true</code> and <code>false</code>.


Used as an integer will be a <code>number</code> (which has integer and float values all in the same datatype).
Used as an integer is a <code>number</code> (which has integer and float values all in the same datatype).


SLua adds the type <code>integer</code>, which is the same than the LSL integer, because LL functions that use lists as parameters need the integers to be of type <code>integer</code>, not <code>number</code>.
SLua adds the type <code>integer</code>, which is the same than the LSL integer, because LL functions that use lists as parameters need the integers to be of type <code>integer</code>, not <code>number</code>.
Line 157: Line 157:
=== LSL float ===
=== LSL float ===


It will be a <code>number</code> (floats and integers all go in the datatype <code>number</code>)
It is a <code>number</code> (floats and integers all go in the datatype <code>number</code>)


{| {{KBtable}}
{| {{KBtable}}
Line 187: Line 187:


=== LSL list ===
=== LSL list ===
=== Typecasting ===


== Events ==
== Events ==

Revision as of 03:13, 19 March 2025

Moving from LSL to SLua

In this section, we will learn how to rewrite our LSL scripts in SLua through a two-step process:

  • First, we will translate them into SLua while maintaining a structure as close as possible to the original LSL format (SLua in LSL style).
  • Next, we will refine and improve them to take full advantage of Lua’s capabilities (SLua in Lua style).

To follow this section effectively, a basic understanding of Lua is required, as covered in the previous two sections.


Using SLua in the beta grid

These are the steps to follow before starting to script in SLua:

  • Install the "Second Life Project Lua Editor" viewer, from: https://releasenotes.secondlife.com/viewer.html. This is a different viewer, with a different icon, than the "Second Life Viewer". We can keep our usual viewer for the main grid and use this one for the beta grid.
  • Teleport to one of the four regions that have SLua activated. It doesn't work in the other regions. Sometimes one or two of these regions are offline, try another one if you can't tp or login:
  • Rez an object and add a new script as usual. The script editor has a "compiler" drop-down list, at bottom center. Choose "SLua":
    • LSL Legacy (LSO2) : LSL, with "Mono" unchecked.
    • LSL Mono : LSL, with "Mono" checked (the usual one).
    • LSL 2025 VM : LSL, but compiled into VM Luau instead of VM Mono.
    • SLua


Global namespace

The global namespace is a table with the name "_G".

It contains the global functions and libraries. Each library is a table with its functions.

SLua has added all the LL constants (1002 constants, currently).

The global variables and functions created by the script will also be in this table.

-- list of global functions (SLua)

for key, value in pairs(_G) do
    if type(value) == "function" then
        ll.OwnerSay(key)
    end
end

There are 22 functions that come from Luau, they are documented in https://luau.org/library:

  • type, typeof, tonumber, tostring
  • ipairs, pairs, next, select
  • getmetatable, setmetatable
  • rawget, rawset, rawequal, rawlen
  • assert, error, pcall, xpcall, gcinfo
  • print, unpack, newproxy


And 7 functions added by Slua:

  • integer, uuid, rotation, quaternion
  • tovector, torotation, toquaternion

We will see them in the Datatypes section below.


3 Luau functions are not in SLua:

  • getfenv, setfenv: old Lua functions out of use.
  • require: to import modules with functions, a very useful option, hopefully it will be in SLua in some future, but it's not planned yet.


Libraries

The libraries are in the global namespace as tables.

-- list of global tables (SLua)

for key, value in pairs(_G) do
    if type(value) == "table" then
        ll.OwnerSay(key)
    end
end

There are 10 libraries that come from Luau, with all the same functions, documented in https://luau.org/library:

  • table, string, math, bit32, os, vector (these ones have functions in common with the LSL functions, we will see them below)
  • coroutine, utf8, debug, buffer


SLua has added:

  • ll

with all the functions coming from LSL (523 functions, currently).

The functions have the same name, without the "ll" at the start (because "ll" is now the name of the library).

So llSay becomes the function Say in the library ll and is used as ll.Say.


Datatypes

LSL integer

Used as a boolean value, with contants TRUE and FALSE is a boolean, with values true and false.

Used as an integer is a number (which has integer and float values all in the same datatype).

SLua adds the type integer, which is the same than the LSL integer, because LL functions that use lists as parameters need the integers to be of type integer, not number.

We get an integer value using myInt = integer(42).

// integers (LSL)

integer isOk = TRUE;
integer count = 0;
integer face = 0;



llSetPrimitiveParams([PRIM_GLOW, face, 1]);





llOwnerSay("glowed!");
-- integers (SLua)

local isOk = true
local count = 0
local face = integer(0)

-- there is no type list, LL functions use tables instead of lists
-- tables are enclosed in { and }, instead of [ and ]
ll.SetPrimitiveParams({PRIM_GLOW, face, 1})

-- when a function is called with only one table literal the parentheses can be omitted
ll.SetPrimitiveParams{PRIM_GLOW, face, 1}

-- same with only one string literal
ll.OwnerSay "glowed!"  -- instead of ll.OwnerSay("glowed!")

LSL float

It is a number (floats and integers all go in the datatype number)

// floats (LSL)

float height = 1.65;
-- floats (SLua)

local height = 1.65

LSL string

LSL key

LSL vector

LSL rotation

LSL list

Typecasting

Events

States

The "New Script"

// the "New Script" (LSL)

default
{
    state_entry()
    {
        llSay(0, "Hello, Avatar!");
    }

    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
}

//
-- the "New Script" (SLua - LSL style)



function state_entry()
    ll.Say(0, "Hello, Avatar!")
end


function touch_start(total_number)
   ll.Say(0, "Touched.")
end



state_entry()
-- the "New Script" (SLua - Lua style)








function touch_start(total_number)
   ll.Say(0, "Touched.")
end



ll.Say(0, "Hello, Avatar!")


Operators

If

Loops

Strings

Lists