User:SuzannaLinn Resident/LuaMovingTo: Difference between revisions
No edit summary |
No edit summary |
||
Line 21: | Line 21: | ||
** [secondlife://Aditi/secondlife/SLua%20Tideland/241/235/27 SLua Tideland] | ** [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 " | * 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 | ** LSL Legacy (LSO2) : LSL, with "Mono" unchecked. | ||
** LSL: | ** LSL Mono : LSL, with "Mono" checked (the usual one). | ||
** LSL 2025 VM : LSL, but compiled into VM Luau instead of VM Mono. | |||
** LSL | ** 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 | |||
== Libraries == | == Libraries == | ||
<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> | |||
Line 34: | Line 80: | ||
== Events | == Events == | ||
== States == | |||
== The | == The "New Script" == | ||
{| {{KBtable}} | {| {{KBtable}} |
Revision as of 07:51, 16 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.
- 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:
- 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
Libraries
-- list of global tables (SLua)
for key, value in pairs(_G) do
if type(value) == "table" then
ll.OwnerSay(key)
end
end
Datatypes
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!")
|