Scripting Tutorial-07
Appearance
Scripting Lesson #6 - Held August 25th, 2018 at Sandbox Plaza by Fu Barr
<source lang="lsl"> /*
Data - some examples
(c)2018 Fu Barr @OSgrid
- /
// Data types: integer, float, key, string, vector (rotation) integer DEBUG = TRUE;
float seconds = 5.0; key uuid;
string name; vector colour = <1, 1, 1>; // <r, g, b>
// Data structure: list list config;
default {
on_rez(integer r){
llResetScript(); // Health and Safety regulations demand this. }
state_entry() {
llOwnerSay("running: " + llGetScriptName());
// 0. now that we're running, get some relevant data
uuid = llGetOwner();
name = llGetDisplayName(uuid);
//state data_dump; }
}
state data_dump {
state_entry() {
if(DEBUG) {
string msg1 = "integer: " + (string) DEBUG;
msg1 += ", float: " + (string) seconds;
msg1 += ", uuid: " + uuid;
msg1 += ", name: " + name;
msg1 += ", colour: " + (string) colour;
string msg2 = "integer: " + (string) DEBUG
+ ", float: " + (string) seconds
+ ", uuid: " + uuid
+ ", name: " + name
+ ", colour: " + (string) colour;
llOwnerSay("msg1: " + msg1);
llOwnerSay("msg2: " + msg2);
}
//state list_dump; }
}
state list_dump {
state_entry() {
// 0. Copy the data values from above into the empty list and show them
config = [DEBUG, seconds, uuid, name, colour];
string dump = llDumpList2String(config, ", ");
llOwnerSay("list dump: " + dump);
//state list_operations;
}
}
state list_operations {
state_entry() {
// 0. setup loop variables and loop through list
integer i = 0;
integer list_end = llGetListLength(config);
while (i < list_end) {
llOwnerSay("list at index(" + (string) i + ") " + llList2String(config, i));
i++;
}
// 1. copy original data into new list and add more data to end of new list
list config2 = config;
config2 += "another string";
config2 += 3.14;
i = 0;
list_end = llGetListLength(config2);
while (i < list_end) {
llOwnerSay("list at index(" + (string) i + ") " + llList2String(config2, i));
i++;
}
// 2. retrieve item at index(2) and replace with new data
key item = llList2Key(config2, 2);
config2 = llListReplaceList(config2, [llGetKey(), llGetTexture(0)], 2, 2);
i = 0;
list_end = llGetListLength(config2);
while (i < list_end) {
llOwnerSay("list at index(" + (string) i + ") " + llList2String(config2, i));
i++;
}
// 3. re-insert old item 2 at position 4
config2 = llListInsertList(config2, item, 4);
i = 0;
list_end = llGetListLength(config2);
while (i < list_end) {
llOwnerSay("list at index(" + (string) i + ") " + llList2String(config2, i));
i++;
}
//state map; }
}
state map {
state_entry() {
/*
A map is a classic datastructure where data values are 'mapped' on to named keys.
Values can be anything, keys need to be unique.
Typically, a map looks like this:
key1 = valueX
key2 = valueY
key3 = ValueZ
...
*/
// 0. Add a 2nd list of setting names to setup the keys for the existing config data list,
// then loop the two lists to show the mapping.
list settings = ["DEBUG", "seconds", "uuid", "name", "colour"];
integer i = 0;
integer list_end = llGetListLength(settings);
while (i < list_end) {
llOwnerSay("map key:value pair " + llList2String(settings, i) + ":" + llList2String(config, i));
i++;
}
// 1. Find index of a particular setting and then print its current value
string setting = "colour";
i = llListFindList(settings, [setting]);
llOwnerSay("map lookup result: " + setting + ":" + llList2String(config, i));
// 2. Now for something different: setting up a single strided list to use as the map.
list strided_list = [];
i = 0;
while(i < list_end) {
strided_list += llList2String(settings, i);
strided_list += llList2List(config, i, i);
i++;
}
llOwnerSay("map strided list: " + llDumpList2String(strided_list, ", "));
// 3. pick a key:value pair from the new strided list-based map
llOwnerSay("map strided list lookup result: " + setting + ":" +
llList2String(strided_list, llListFindList(strided_list, [setting]) + 1)
);
}
} </source>