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

From Second Life Wiki
Jump to navigation Jump to search
(9 intermediate revisions by the same user not shown)
Line 5: Line 5:
* Next, we will refine and improve them to take full advantage of Lua’s capabilities (SLua in Lua 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.
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.
* Access the Beta Grid (Aditi), explained in: https://lindenlab.freshdesk.com/support/solutions/articles/31000156725-accessing-aditi
* 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:
** [secondlife://Aditi/secondlife/SLua%20Yardang/241/235/27 SLua Yardang]
** [secondlife://Aditi/secondlife/SLua%20Tombolo/241/235/27 SLua Tombolo]
** [secondlife://Aditi/secondlife/SLua%20Mesa/241/235/27 SLua Mesa]
** [secondlife://Aditi/secondlife/SLua%20Tideland/241/235/27 SLua Tideland]
* 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.
<syntaxhighlight lang="lua" line copy>
-- list of global functions (SLua)
for key, value in pairs(_G) do
    if type(value) == "function" then
        ll.OwnerSay(key)
    end
end
</syntaxhighlight>
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.
<syntaxhighlight lang="lua" line copy>
-- list of global tables (SLua)
for key, value in pairs(_G) do
    if type(value) == "table" then
        ll.OwnerSay(key)
    end
end
</syntaxhighlight>
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 <code>llSay</code> becomes the function <code>Say</code> in the library <code>ll</code> and is used as <code>ll.Say</code>.
== Datatypes ==
=== 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 an integer will be 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>.
We get an integer value using <code>myInt = integer(42)</code>.
{| {{KBtable}}
|
<syntaxhighlight lang="lsl2" line>
// integers (LSL)
integer isOk = TRUE;
integer count = 0;
integer face = 0;
llSetPrimitiveParams([PRIM_GLOW, face, 1]);
llOwnerSay("glowed!");
</syntaxhighlight>
|
<syntaxhighlight lang="lua" line>
-- 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!")
</syntaxhighlight>
|}
=== LSL float ===
It will be a <code>number</code> (floats and integers all go in the datatype <code>number</code>)
{| {{KBtable}}
|
<syntaxhighlight lang="lsl2" line>
// floats (LSL)
float height = 1.65;
</syntaxhighlight>
|
<syntaxhighlight lang="lua" line>
-- floats (SLua)
local height = 1.65
</syntaxhighlight>
|}
=== LSL string ===
=== LSL key ===
=== LSL vector ===
=== LSL rotation ===
=== LSL list ===
== Events ==
== States ==
== The "New Script" ==
{| {{KBtable}}
|
<syntaxhighlight lang="lsl2" line>
// the "New Script" (LSL)
default
{
    state_entry()
    {
        llSay(0, "Hello, Avatar!");
    }
    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
}
//
</syntaxhighlight>
|
<syntaxhighlight lang="lua" line copy>
-- 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()
</syntaxhighlight>
|
<syntaxhighlight lang="lua" line>
-- the "New Script" (SLua - Lua style)
function touch_start(total_number)
  ll.Say(0, "Touched.")
end
ll.Say(0, "Hello, Avatar!")
</syntaxhighlight>
|}
== Operators ==
== If ==
== Loops ==
== Strings ==
== Lists ==

Revision as of 09:25, 17 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 will be a boolean, with values true and false.

Used as an integer will be 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 will be 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

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