Boggled trying to make a Toggle

Posted by Falken on Thu 08 May 2008 05:30 AM — 6 posts, 21,496 views.

#0
Hi Nick and Co.,

First, thanks for coding a great freeware application.

I've been a lurker for a while now, and can't seem to uncover what I'm doing wrong. I'm a MC newbie, but know how to make basic aliases and triggers, and I understand (at least I thought I did) basic control.

I'm not using functions or a script file since that's over my head for now. I realise that what I want to do is easily handled with multiple aliases (like "seekon" and "seekoff"), but I'm trying to condense and thought I'd try using IF THEN ELSE.

1. I know there has to be a sexier way to do this.

2. I can't get the trigger to work.

3. Even though I don't know very much, I'm providing examples of my work--it's better than saying "Make me a trigger and some aliases that do..." right?



What I want:

Trigger
Called "seeking" that highlights output from the mud--the contents of up to three variables: seek1, seek2, seek3

Alias(es)
Called "seek" to toggle it off/on.

Enter "seek" and up to three arguments to store strings in the variables: seek1, seek2, seek3. Doing so enables the "seeking" trigger by default.


What I've tried:

The trigger:

<triggers>
  <trigger
   custom_colour="17"
   enabled="y"
   match="@seeking|@seeking2|@seeking3"
   name="seeklite"
   sequence="100"
   other_text_colour="deeppink"
   other_back_colour="#000057"
  >
  </trigger>
</triggers>


I made four aliases that match like this...


^seek (.*?) (.*?) (.*?)$
^seek (.*?) (.*?)$
^seek (.*?)$
^seek$


The first three capture and store the variables seek1, seek2, and seek3. They also SetVariable("seektoggle",1)

The last is supposed to toggle the trigger. I always get my fail message when I try the command.


<aliases>
  <alias
   match="^seek$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

local seekcheck

seekcheck = GetVariable("seektoggle")

if "seekcheck" == 1

  then SetVariable("seektoggle",0)
  EnableTrigger("seeklite",false)

  Note""
  Note" Seeking Disabled "
  Note""

elseif "seekcheck" == 0

  then SetVariable("seektoggle",1)
  EnableTrigger("seeklite")

  Note""
  Note" Seeking Enabled "
  Note""

else 

  Note" Trigger Failed "

end
</send>
  </alias>
</aliases>

Australia Forum Administrator #1
Quote:

I'm providing examples of my work--it's better than saying "Make me a trigger and some aliases that do..." right?


Much better. :)

Quote:


seekcheck = GetVariable("seektoggle")

if "seekcheck" == 1

  then 



Your basic problem is here. You are comparing a string "seekcheck" to be equal to 1, which it never will be (it has the value "seekcheck").

You mean:


seekcheck = GetVariable("seektoggle")

if seekcheck == 1  then 


As a style thing, I like the "then" on the same line as the "if" personally.

That way, if you want to add more things to happen if the "if" is true, you don't have to start moving the word "then" around.
#2
Thanks Nick,

Great style tip, that makes things much easier.

It's still failing after applying that fix.
#3
Looking through the implemented help for "GetVariable" I found the error. What a relief!

Quote:


Variables are stored as strings, so GetVariable returns a string, not a number. If the variable is supposed to be storing a numeric quantity (a count of mobs, hit points, or experience, for example) then you will probably need to convert it back to a number if it is going to be correctly processed inside a script. For example:

Lua: x = tonumber (GetVariable ("x"))



Also, I realized that using a local variable was unnecessary and sloppy.


seektoggle = tonumber (GetVariable ("seektoggle"))

if seektoggle == 1  then 
  
  SetVariable("seektoggle",0)
  EnableTrigger("seeklite",false)

  Note""
  Note" Seeking Enabled "
  Note""

elseif seektoggle == 0 then

  SetVariable("seektoggle",1)
  EnableTrigger("seeklite")

  Note""
  Note" Seeking Disabled "
  Note""

end
Amended on Thu 08 May 2008 08:23 PM by Falken
Australia Forum Administrator #4
Glad it works. As another style issue, try putting spaces after keywords, eventually it will bite you if you don't.

eg.


SetVariable ("seektoggle", 1)
EnableTrigger ("seeklite")

Note ""
Note " Seeking Disabled "
Note ""


That also makes it easier to read.

Is this seektoggle variable local to one session? That is, is it relevant between games (like if you log out, and connect again tomorrow)? If not, you don't need to use MUSHclient variables at all, that would look neater. For example:


if seektoggle then 
  
  seektoggle = false
  EnableTrigger ("seeklite", false)

  Note ""
  Note " Seeking Enabled "
  Note ""

else

  seektoggle = true
  EnableTrigger ("seeklite")

  Note ""
  Note " Seeking Disabled "
  Note ""

end


And also in other places, simply assign true or false to seektoggle. This is using the Lua variable, which is probably all you need.

You also don't need the elseif, after all if seektoggle is not true, it must be false, right? There is no third case.

Once you get this far you can neaten it even further:


seektoggle = not seektoggle  -- toggle it
EnableTrigger ("seeklite", seektoggle)

Note ""

if seektoggle then
  Note " Seeking Enabled "
else
  Note " Seeking Disabled "
end -- if

Note ""


See how that reduces a lot of the repetition in the script?
#5
Nick,

So generous to share those techniques--thanks, thanks, and thanks again. That's a lot sexier than my cluttered attempts.

But, I expected nothing less--I'm not even in your league. That last part of code is so compact and neat. I love it.

The end result here is I learned how to properly use BOOLEAN which I couldn't quite understand. Originally I wanted to use "true" and "false" instead of variables but in the aliases I kept getting type-match errors.

Now I'm feeling compulsive about cleaning up/rearranging all the other triggers/aliases I have.



Derek Falken