Noob scripter Menu question
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
04-26-2007 08:47
I have programmed in Java but have just started scripting in LSL (which has many similarities). I am trying to make a menu driven controller for lighting on my skybox. The controller object shouts on or off commands on a specific channel (NOT public) to objects listening on the same channel. The controller will display a menu when clicked, with buttons for two modes: Auto and Manual. Auto runs a timer event every 5 minutes that turns lighting on or off depending on the sun's height. This part works fine.
Manual mode brings up another menu with two buttons: On and Off. Currently nothing happens when clicking either button on the manual dialog. I also keep track of the lighting state in a global variable called "mode", and update this in each conditional branch. The current mode is displayed on the main menu. When the script is reset, mode is assigned "Manual: Lights Off" by default. When choosing Auto, the timer event is successfully run and the mode is successfully changed - the lights turn on at night and off during the day. When Manual is chosen from the main menu, the manual menu appears, but code execution apparently stops at that point.
I am most likely missing something very simple, or making a dumb mistake. Can anyone see where the problem is? I appreciate any help and apologize if this thread belongs in a different place.
Thanks, Pryme
SEE POST BELOW FOR CODE
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
Sorry about the formatting
04-26-2007 08:55
Apparently the forum removes extra spacing, which makes the code harder to follow.
|
|
RJ Source
Green Sky Labs
Join date: 10 Jan 2007
Posts: 272
|
04-26-2007 09:04
One logic issue which might effect your results: Change: if (message == "ON"  { To else if (message == "ON"  { Otherwise, I believe your last else if will always fire (the timer) when MANUAL is selected.
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
Conditionals
04-26-2007 09:16
Thanks fo replying... unfortunately as I suspected, the lack of formatting is making the flow hard to see. There is an if/else if for the main menu options of "Auto" and "Manual". The Manual dialog and its 2 conditionals are actually nested within the first if. Replacing if (message == "ON"  with else if (message == "ON"  actually gives a syntax error, as it is the first conditon in this nest. I will attach a text file that hopefully shows a clearer format... Thanks again for helping.
|
|
RJ Source
Green Sky Labs
Join date: 10 Jan 2007
Posts: 272
|
04-26-2007 09:16
I think your problem is that you never reset the timer when you go into manual mode. So even though Manual sets mode to ON, the timer immediately fires again at 1 second and sets it to OFF.
You can turn off a timer by passing it 0 seconds.
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
conditionals cont....
04-26-2007 09:21
When the script is reset, it doesn't matter what mode it was in previously, right? It all starts over with state_entry. Clicking Manual and then On or Off shouldn't be affected by the timer event. If the first if conditional is executed, then regardless of whether either nested conditional is chosen (or not), the else if (message == "Auto"  should not execute at all, and therefore never call the timer. Correct?
|
|
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
|
04-26-2007 09:22
Put [ php] and [ /php] (without the extra space) around your code to have the forums format it nicely. float freq = 300.0; // Setting in seconds for timer function to be called. integer chan = 2600; // Channel script shouts on. // (receiving light prims must listen on same channel) string mode = "Manual: Lights OFF"; // Default state of script (light) on rez/reset. key user; // Key of AV that touches switch.
default {
state_entry() { llShout(chan, "Off"); llSay(0, "Lights are currently off. Touch Light Switch for menu."); llListen(1111, "", "", ""); }
touch_start(integer total_number) { user = llDetectedKey(0); llDialog(user,"\nCurrent Mode: " + mode,["Auto", "Manual"],1111); } listen(integer channel, string name, key id, string message) { if (message == "Manual") { llDialog(user, "\nManual Setting",["ON", "OFF"],1111); if (message == "ON") { mode = "Manual: Lights ON"; llShout(chan, "On"); } else if (message == "OFF") { mode = "Manual: Lights OFF"; llShout(chan, "Off"); } } else if (message == "Auto") { llSetTimerEvent(1); } } timer() { vector sun = llGetSunDirection(); if(sun.z > 0) { mode = "Auto: Lights OFF"; llShout(chan, "Off"); } else { mode = "Auto: Lights ON"; llShout(chan, "On"); } llSetTimerEvent(freq); } }
I think the problem is in your listen event handler.. Stripped down a bit, it looks like this.. if (message == "Manual") { if (message == "ON") { } else if (message == "OFF") { }
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!! - Go here: http://jira.secondlife.com/browse/SVC-1224- If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
Thanks Meade
04-26-2007 09:25
Much nicer. Clearly I'm a forum noob also.
Yes, I believe the issue involves listen somehow, as the code seems to stop at that point. I'm not sure what the exact problem is though, or how to address it. I need to have a second menu appear for manual settings, and something must be listening for the response of this second menu. I thought the original llListen would handle this, but apparently not.
I know this is used frequently in SL, as I have several non-mod objects that employ multi-menu options (non-mod scripts, so I can't look at them). There is probably a more efficient way to do this than I am using. I will continue to try alternate methods.
Thanks again for all suggestions (and patience).
|
|
RJ Source
Green Sky Labs
Join date: 10 Jan 2007
Posts: 272
|
04-26-2007 09:35
Looks like Meade found the problem. So redoing your event handler should fix it: listen(integer channel, string name, key id, string message) { if (message == "Manual") { llDialog(user, "\nManual Setting",["ON", "OFF"],1111);} else if (message == "ON") { mode = "Manual: Lights ON"; llShout(chan, "On");} else if (message == "OFF") { mode = "Manual: Lights OFF"; llShout(chan, "Off");} else if (message == "Auto") { llSetTimerEvent(1);} }
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
It Works
04-26-2007 09:43
Thanks guys...
Everything is working as it should. I really appreciate your help. Now I can play with it for a few minutes, just before SL goes down again for system "fixes" : )
~Pryme
|
|
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
|
04-26-2007 09:44
What RJ posted will fix the problem. You're doing this.. if (message == "Manual") { if (message == "On") { } else if (message == "Off") { } }
...but message will never equal both "Manual" and something else at the same time. You should be doing something like this... if (message == "Manual") { } else if (message == "On") { } else if (message == "Off") { }
An alternative that might make things easier would be to have different channels for different dialogs but that's probably overkill in this script.
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!! - Go here: http://jira.secondlife.com/browse/SVC-1224- If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left
|
|
Pryme Numbers
Registered User
Join date: 19 Dec 2006
Posts: 6
|
Thanks for the explanation.
04-26-2007 10:03
It makes perfect sense.
|