Convoluted Conditions

by Arundel

Back to Mechanic's Corner.

Arundel2005-07-14 22:18:34
I have an alias for PsiSense set so that if I simply type the alias (psense), it will turn it on, but it will turn off if I type "off" after it. The catch is that I added a few conditions so that it will automatically select the substratus channel if my "channel" variable is set to super.

CODE

#if (%1="off") {#if (@channel="super") {psi sub psisense off} {psi @channel psisense on}} {#if (@channel="super") {psi sub psisense on} {psi @channel psisense on}}


If anyone could tell me what's wrong with that code, I'd really appreciate it. It's all the ons and offs.

EDIT: This is zMUD code, by the way.
Unknown2005-07-14 23:07:03
Ok, number of things.
First off, "=" doesn't work as a valid compare when using strings.
Secondly, alll that #if stuff is complicated and ugly. Harder to read and follow the flow.

Here's some clean code.

CODE
psi %if(%pos(super,@channel),substratus,@channel) psisense %if(%pos(off,%1),off,on)
Unknown2005-07-14 23:18:01
CODE

#if (%1="off")
{
  #if (@channel="super") {psi sub psisense off} {psi @channel psisense on}
}
{
  #if (@channel="super") {psi sub psisense on} {psi @channel psisense on}
}


I didn't understand from your post exactly what the problem is, but I'll wager that is always does 'psi sub psisense off' regardless of the variables. As Visaeris said, you can't use '=' to compare. What you're doing in this code segment is say "if you can put the value 'super' in the variable 'channel', then do..." What you want to do is use '==' which is the "do this if %1 is equal to %2"

And the formating of your code is merely a personal preference. You can use Visaeris' code, and it probably works (it looks like it should), but the #if isn't inherently wrong either. "Clean code" is also determined by clarity.
Unknown2005-07-14 23:30:26
There is no "==" operator in zmud scripting language. You use "=".
Unknown2005-07-14 23:32:25
QUOTE(Kashim @ Jul 14 2005, 04:30 PM)
There is no "==" operator in zmud scripting language. You use "=".
152563



unless it's a string at which point you use.. "=~"

Like I said tongue.gif
Unknown2005-07-14 23:35:46
If you say so... that would mean there's no exact comparison operator for strings, which would be weird.
QUOTE
v1 =~ v2  true if the string v1 matches the pattern in v2

Unknown2005-07-14 23:43:11
Meh, I wasn't sure if it was zMUD that didn't have '==' or MUSHclient.
Anyway, what exactly is the problem you're having?
Arundel2005-07-14 23:48:13
Well, I don't understand Visaeris' code at all, since I've never used %if and all that. Care to explain what the syntax you're using, especially the commas, does?

So I can't use the "=" to check for strings?

EDIT: Well, I guess the zMUD help index is a good place to start. It looks a lot cleaner, that's for sure.
Unknown2005-07-15 01:15:34
QUOTE(Arundel @ Jul 14 2005, 04:48 PM)
Well, I don't understand Visaeris' code at all, since I've never used %if and all that. Care to explain what the syntax you're using, especially the commas, does?

So I can't use the "=" to check for strings?
152569



I've never had success with = checking strings, but eh.

With regards to %if, start up zMUD and type "#help %if"

It's pretty simple.

%if(evaluation,stuff if true, stuff if false)

The commas seperate the parameters.
Arundel2005-07-15 02:41:44
Ah, it makes sense now. It's pretty much the same thing, just cleaner?

EDIT: Is it possible to search for a wild card in %pos or otherwise?
Unknown2005-07-15 11:52:19
I'm seeing some misinformation here, which I would like to clear up now.

1. You can use = or == interchangeably when doing comparisons in zMUD.

2. You can use = to compare strings, but you need to have the proper quoting. %1 = off works the way "%1" = "off" works. If one or both sides has spaces or special characters in it, you need the double quotes around them, so it's always a good idea to include them when evaluating strings.

3. The =~ operator is used to match zMUD pattern symbols as wildcards, and works in much the same way that triggers evaluate text from the game.

4. The #IF command and %if function are similar except in where they can be called and what they return. Functions (prefixed with %) return values. Commands never return values, but can only be called separately (i.e., not inline) unless you want to use %exec. In the case of %if, it will return either the second or third parameter based on the value of the expression in the first.

If you want to search a string for a wildcard, either use the =~ syntax, the %regex function, or the %match function (which is equivalent to =~, basically).