Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Notecard Reader Advanced_Note_Card_reader  

Advanced_Note_Card_reader

The script

Category: Notecard Reader
By : Lear Cale
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Advanced_Note_Card_reader
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Advanced_Note_Card_reader_1.lsl
Get file # 2. Advanced_Note_Card_reader_2.lsl
1 // Multi-file configuration, v1a
2 // Lear Cale, 2007
3 // Public Domain. Feel free to delete header comments.
4
5 // This script reads all configuration notecards that match a given suffix,
6 // storing the settings in global variables. It rereads the notecards
7 // on any inventory change.
8
9 // It does not require a script reset on any event, but one is done for
10 // owner change on general principles. If your object needs not to be
11 // reset on owner change, you may delete those lines from the code.
12
13 // ==============================================================
14 // Places where you change the code are marked with "%%%"
15 // ==============================================================
16
17
18 // Example config variables -- replace these with meaningful stuff! %%%
19
20 // For this example, we allow the user to configure any number of "foos",
21 // with an integer parameter for each foo. There's also a float "bar"
22 // parameter.
23
24 // Replace these lines with your globals and try to save: you'll get
25 // errors in the places you need to edit.
26
27 list Foos; // %%% all foos configured
28 list FooVals; // %%% value for each foo configured
29 float Bar; // %%% a configurable parameter
30
31
32 // %%% Static parameters for reading card config: you may change these, but don't have to.
33 integer ConfigRequired = FALSE; // whether to fail if no config cards
34 string ConfigNotecardSuffix = ".cfg"; // string identifying config notecards
35 float ConfigTimeout = 60.0; // seconds to wait for slow server
36
37
38 // Globals for reading card config
39 integer ConfigLineIndex; // line number in notecard we're reading
40 key ConfigRequestID; // request we're waiting for
41 list ConfigCards; // list of names of config notecards
42 string ConfigCardName; // name of card being read
43 integer ConfigCardIndex; // index of next card to read
44
45 integer Debug; // Whether to print debug text
46
47
48 config_init()
49 {
50 // Initialize all configurable global variables here %%%
51 // Don't initialize your configurable globals at their
52 // declarations (above). Doing it here provides default
53 // behavior if the user deletes a config line from a notecard
54 // rather than retaining the previous value.
55 Foos = []; // %%%
56 Bar = 20.0; // %%%
57 }
58
59 // print the configuration, handy for debugging.
60
61 config_dump()
62 {
63 // Replace this example code with your config %%%.
64 say("Foos: " + llList2CSV(Foos)); // %%%
65 say("FooVals: " + llList2CSV(FooVals)); // %%%
66 say("Bar: " + (string) Bar); // %%%
67 }
68
69
70 // Example notecard line parser.
71 // This function is called for each configuration line.
72 // It treats all config notecards the same.
73 //
74 // For this example (which you can modify to suit your purposes)
75 // Comments lines begin with a slash
76 // Each line begins with a command followed by optional arguments
77 // An Equals-sign ("=") separates command and arguments (and args from each other)
78 // Unrecognized commands are ignored -- good for forwards-backwards notecard compatibility
79 // cardName and lineNum are in case you want to print error messages.
80 // This example doesn't allow spaces around the "=". If you want to allow
81 // and ignore them, use this instead of ["="] in llParsStringKeepNulls().
82 // [" = ", " = ", " = ", " = ", " =", "= ", "="]
83 //
84 // Example config file contents:
85 // foo=Lowell George=1979
86 // foo=Freddy Murcury=1991
87 // foo=Robert Johnson=1938
88 // bar=1.5
89
90 config_parse(string str, string cardName, integer lineNum)
91 {
92 str = llStringTrim(str, STRING_TRIM_HEAD); // delete leading spaces
93
94 // lines beginning with slash are comments -- ignore them
95 if(llGetSubString(str,0,0) == "/") {
96 return;
97 }
98
99 list ldata = llParseStringKeepNulls(str, ["="], [""]);
100 string cmd = llList2String(ldata,0);
101 string arg1 = llList2String(ldata,1);
102 string arg2 = llList2String(ldata,2);
103 // %%% Add more lines such as the above as needed for more arguments.
104
105 // %%% Process example commands -- replace this code with meaningful stuff! %%%
106 if(cmd == "foo") {
107 // another Foo configured: remember it
108 Foos += [arg1];
109 FooVals += [(integer) arg2];
110 } else if(cmd == "bar") {
111 Bar = (float) arg1;
112 }
113
114 // this one is a good one to keep
115 else if(cmd == "debug") {
116 Debug = (integer) arg1;
117 }
118 }
119
120
121 // Post-process any config, if necessary
122 config_done() {
123 if(Debug) {
124 config_dump();
125 }
126 say("Config done");
127 }
128
129
130 // ==== Utilities ====
131
132 // Say something, in this case to owner (%%% modify to whisper or whatever)
133
134 say(string str)
135 {
136 llOwnerSay(str);
137 }
138
139
140 // Say something if debug is enabled
141 debug(string str)
142 {
143 if(Debug) {
144 say(llGetScriptName() + ": " + str);
145 }
146 }
147
148 // Get the next notecard name, and
149 // return TRUE if there is one
150
151 integer next_card()
152 {
153 if(ConfigCardIndex >= llGetListLength(ConfigCards)) {
154 ConfigCards = [];
155 return (FALSE);
156 }
157
158 ConfigLineIndex = 0;
159 ConfigCardName = llList2String(ConfigCards, ConfigCardIndex);
160 ConfigCardIndex++;
161 ConfigRequestID = llGetNotecardLine(ConfigCardName, ConfigLineIndex);
162 say("Reading " + ConfigCardName);
163 return (TRUE);
164 }
165
166 // BEGIN STATES
167
168 // Default state can do any init you need that doesn't require configuration.
169
170 default
171 {
172 state_entry() {
173 llSetText("", <1.0,1.0,1.0>, 1.0);
174 state s_config;
175 }
176 }
177
178 // This state is only used to get into s_config, because going from
179 // s_config to s_config won't redo it's state_entry. But we might
180 // not want to redo anything we might have put in default state entry.
181
182 state s_reconfig
183 {
184 state_entry() {
185 state s_config;
186 }
187 }
188
189 // Read card config
190 // Multiple notecard version - read all cards with the given extension
191
192 state s_config
193 {
194 state_entry() {
195 config_init();
196
197 string item;
198 ConfigCards = [];
200 while(n-- > 0) {
202 // Note: for simplicity, read cards with the "suffix" anywhere in the name
203 if(llSubStringIndex(item, ConfigNotecardSuffix) != -1) {
204 ConfigCards += [item];
205 }
206 }
207
208 ConfigCardIndex = 0;
209 if(next_card()) {
210 llSetTimerEvent(ConfigTimeout);
211 } else if(ConfigRequired) {
212 say("Configuration notecard missing.");
213 state s_configRetry;
214 } else {
215 state s_active;
216 }
217 }
218
219 dataserver(key query_id, string data) {
220 if(query_id == ConfigRequestID) {
221 if(data == EOF) {
222 if(! next_card()) {
223 config_done();
224 state s_active;
225 }
226 } else {
227 config_parse(data, ConfigCardName, ConfigLineIndex);
228 ConfigRequestID = llGetNotecardLine(ConfigCardName, ++ConfigLineIndex);
229 llSetTimerEvent(ConfigTimeout);
230 }
231 }
232 }
233
234 timer() {
235 say("Dataserver time out: touch to retry");
236 state s_configRetry;
237 }
238
239 on_rez(integer num) { state s_reconfig; }
240
241 changed(integer change) {
242 if(change & CHANGED_OWNER) { llResetScript(); }
243 if(change & CHANGED_INVENTORY) { state s_reconfig; }
244 }
245
246 state_exit() {
248 }
249 }
250
251 state s_configRetry
252 {
253 touch_start(integer tot) {
254 if(llDetectedKey(0) == llGetOwner()) {
255 state s_config;
256 }
257 }
258
259 changed(integer change) {
260 if(change & CHANGED_OWNER) { llResetScript(); }
261 if(change & CHANGED_INVENTORY) { state s_config; }
262 }
263 }
264
265 // State to go into if notecard is required but missing.
266 // You can delete this and the code above that refers to it,
267 // or just set ConfigurationRequired to FALSE.
268 state s_unconfigured
269 {
270 state_entry() {
271 llSetText("Configuration missing", <1.0,1.0,1.0>, 1.0);
272 }
273
274 changed(integer change) {
275 if(change & CHANGED_OWNER) { llResetScript(); }
276 if(change & CHANGED_INVENTORY) { state s_reconfig; }
277 }
278
279 state_exit() {
280 llSetText("", <1.0,1.0,1.0>, 1.0);
281 }
282 }
283
284
285 // The fun starts here!
286
287 state s_active
288 {
289
290 // %%% Your code goes here!
291
292 // Every state should usually have this, or something like it.
293 changed(integer change) {
294 if(change & CHANGED_OWNER) { llResetScript(); }
295 if(change & CHANGED_INVENTORY) { state s_reconfig; }
296 }
297 }

Advanced_Note_Card_reader

States:

Category: Notecard Reader
By : Lear Cale
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 // Default state can do any init you need that doesn't require configuration.
2
3 default
4 {
6 llSetText("", <1.0,1.0,1.0>, 1.0);
7 state s_config;
8 }
9 }
10
11 // This state is only used to get into s_config, because going from
12 // s_config to s_config won't redo it's state_entry. But we might
13 // not want to redo anything we might have put in default state entry.
14
15 state s_reconfig
16 {
17 state_entry() {
18 state s_config;
19 }
20 }
21
22 // Read card config
23 // Multiple notecard version - read all cards with the given extension
24
25 state s_config
26 {
27 state_entry() {
28 config_init();
29
30 string item;
31 ConfigCards = [];
33 while(n-- > 0) {
35 // Note: for simplicity, read cards with the "suffix" anywhere in the name
36 if(llSubStringIndex(item, ConfigNotecardSuffix) != -1) {
37 ConfigCards += [item];
38 }
39 }
40
41 ConfigCardIndex = 0;
42 if(next_card()) {
43 llSetTimerEvent(ConfigTimeout);
44 } else if(ConfigRequired) {
45 say("Configuration notecard missing.");
46 state s_configRetry;
47 } else {
48 state s_active;
49 }
50 }
51
52 dataserver(key query_id, string data) {
53 if(query_id == ConfigRequestID) {
54 if(data == EOF) {
55 if(! next_card()) {
56 config_done();
57 state s_active;
58 }
59 } else {
60 config_parse(data, ConfigCardName, ConfigLineIndex);
61 ConfigRequestID = llGetNotecardLine(ConfigCardName, ++ConfigLineIndex);
62 llSetTimerEvent(ConfigTimeout);
63 }
64 }
65 }
66
67 timer() {
68 say("Dataserver time out: touch to retry");
69 state s_configRetry;
70 }
71
72 on_rez(integer num) { state s_reconfig; }
73
74 changed(integer change) {
75 if(change & CHANGED_OWNER) { llResetScript(); }
76 if(change & CHANGED_INVENTORY) { state s_reconfig; }
77 }
78
79 state_exit() {
81 }
82 }
83
84 state s_configRetry
85 {
86 touch_start(integer tot) {
87 if(llDetectedKey(0) == llGetOwner()) {
88 state s_config;
89 }
90 }
91
92 changed(integer change) {
93 if(change & CHANGED_OWNER) { llResetScript(); }
94 if(change & CHANGED_INVENTORY) { state s_config; }
95 }
96 }
97
98 // State to go into if notecard is required but missing.
99 // You can delete this and the code above that refers to it,
100 // or just set ConfigurationRequired to FALSE.
101 state s_unconfigured
102 {
103 state_entry() {
104 llSetText("Configuration missing", <1.0,1.0,1.0>, 1.0);
105 }
106
107 changed(integer change) {
108 if(change & CHANGED_OWNER) { llResetScript(); }
109 if(change & CHANGED_INVENTORY) { state s_reconfig; }
110 }
111
112 state_exit() {
113 llSetText("", <1.0,1.0,1.0>, 1.0);
114 }
115 }
116
117
118 // The fun starts here!
119
120 state s_active
121 {
122
123 // %%% Your code goes here!
124
125 // Every state should usually have this, or something like it.
126 changed(integer change) {
127 if(change & CHANGED_OWNER) { llResetScript(); }
128 if(change & CHANGED_INVENTORY) { state s_reconfig; }
129 }
130 }

Back to the Best Free Tools in Second Life and OpenSim.