Error in else-if code - TADS3.1

Every time I start to really get going on my story, I hit a bump in the road. I added the ‘else if’ statement to the code below and now I get compile errors. I checked the code for some amount of time and checked the manuals but can’t figure it how to fix the problem. Wouldn’t mind some help.

RonG


+ring: Wearable 'platinum ring' 'platinum ring'
 "Faint cryptic runes are inscribed around the band of the platinum ring."  
 
 dobjFor(Wear){
  action(){
   inherited();
   "You feel a strange magical power coursing through your body!
    And, you hear the faint echo of the word GLOW surrounding you.";
   gActor.makeWizard();
  }
 }

 dobjFor(Doff){
  action(){
   inherited();
   "You shudder as the power of the ring and the magic of the GLOW chant leave
    you";
   gActor.makeNormal();
  } 
 }
;

  DefineIAction(Glow);
  VerbRule(Glow)
    'glow'
  :GlowAction
  verbPhrase='glow/glowing'
;
  modify GlowAction
   execAction(){
    if(gActor.powerLevel!=wizard)
     reportFailure('At this time, you do not have the power of a Wizard.');
    else if(gActor.powerLevel=wizard)
     "The blue, radiant light quickly fades from your body.";
     gActor.brightness=0;
    else{
     "You chant the GLOW spell!
      Your body begins to glow with a bright, radiating blue light!";  
      gActor.brightness=10;
      }
     }
;  

“if” only controls a single instruction if you omit the braces. This is a good reason to always use braces. When you write:

if (blah)
  something;
  something;
else
  something;

What the compiler does is:

if (blach)
  something;
something;
else
  something;

Which doesn’t make sense.

Also, you used a “=” instead of a “==”, which compiles but it’s not what you mean.

To put Nikos’s observation in a slightly different way … T3 doesn’t care about indentation. (Unlike Inform 7, which does.) However, Workbench does automatic indentation, which can give you some good guidance on how the compiler is going to interpret your code.

If you only want one statement after an if-test, you can do this:

if (someCondition == true) "That's certainly true. "; else "That's not true at all. ";

What you can’t do is:

if (someCondition == true) "That's certainly true -- and worth awarding a point. "; someAchievement.awardPointsOnce(); else "That's not true at all, so no points for you, bucko! ";

You would have to do it this way:

if (someCondition == true) { "That's certainly true -- and worth awarding a point. "; someAchievement.awardPointsOnce(); } else "That's not true at all, so no points for you, bucko! ";

But again, what matters is the brackets, not the indentation.

I looked at all the posts but still don’t understand this. The statement I added to the original of this was the two lines of the code that begin with ‘else if’. before that, all the story compiled and ran with no problems; including the wizard section. I added the ‘else if’ part so that the actor could turn off the glowing, if he desired for some reason. As you can see, I made your suggestions but I no doubt did it all incorrectly :unamused: :unamused:

modify GlowAction
   execAction()
    if(gActor.powerLevel!==wizard){
     reportFailure('At this time, you do not have the power of a Wizard.');}
    else if(gActor.powerLevel==wizard){
     "The blue, radiant light quickly fades from your body.";
     gActor.brightness=0;}
    else{
     "You chant the GLOW spell!
      Your body begins to glow with a bright, radiating blue light!";  
      gActor.brightness=10;
;      

You’re missing a } for your final else block.

Edit: And the operator for “not equal to” is != (one exclamation mark and one equals sign). See chapter 3.4 of Learning TADS 3 for a full explanation.

It seems you don’t grasp the basics yet. Like the syntax of functions/methods and “if” statements. For example your execAction()'s code is not inside braces. Also, your code’s formatting is very chaotic and unreadable, which will make things very difficult for you. A good way to write that code is:

modify GlowAction
    execAction()
    {
        if (gActor.powerLevel != wizard) {
            reportFailure('At this time, you do not have the power of a Wizard.');
        } else if (gActor.powerLevel == wizard) {
            "The blue, radiant light quickly fades from your body.";
            gActor.brightness = 0;
        } else {
            "You chant the GLOW spell!
            Your body begins to glow with a bright, radiating blue light!";
            gActor.brightness = 10;
    }
;

And now it’s very obvious that the final “else” is missing the closing brace.

Another thing you’re missing is putting a space after a text sentence ends. For example, instead of:

"The blue, radiant light quickly fades from your body."

You should write:

"The blue, radiant light quickly fades from your body. "

This is needed in order to avoid sentences running together.

Also, that final else will never happen. If gActor.powerLevel is wizard, the second code block is executed - if it isn’t, we get the first. How are you planning to trigger the third?

Well, I’m stumped. That’s not difficult for me. I made all the suggestions I received and now I receive 109 errors, yes 109, when I compile my story. If I comment out the entire ‘ring’ section then the program compiles with no errors and and runs as expected. I still think that the problem is in the else-if section.

Well, for one thing, you’re using the comparison operator (==) in a couple of places where you want the assignment operator (=).

Edit: However, that won’t cause compilation errors. Such a statement will simply evaluate as true or nil, but will have no effect on the rest of the code. It’s no different than writing this line:

1;

Looking through your code, I don’t immediately spot any syntax errors. What you’ve posted should compile.

However, as pointed out earlier in this thread, that final else block will never be reached, because the two previous tests take care of all of the possible cases. [[ It looks to me as if you’re trying to use that final block to create an Unglow action. If so, you need to start by figuring out what command the player will use to stop glowing, and then implement that separately.]] Edit: No, I see it now. You need a separate test to determine if the player is currently glowing. You’re trying to test powerlevel again, and that won’t tell you what you need to know.

I would also point out that you don’t need to use modify to create the code for execAction(). You can put that within the definition of DefineIAction(Glow), before the semicolon.

RealNC;

You are correct about my lack of TADS basics and my formatting of the sections of my code. However, despite my dementia [Look it up.], I continue to strive to learn TADS. I spend eight to ten hours a day(often longer) working on it. I will make the formatting changes you suggested.

I really am not looking for pity or criticism , so, please give me just a little bit of slack.

RonG

I can’t speak for Nikos, but unless I’m missing something, all he said was, “It seems you don’t grasp the basics yet.” That could be read as criticism, but I suspect he meant it simply as an observation. Possibly an observation tinged with impatience – but if you’re working at it for hours every day and running afoul of such basic syntax errors, I would suggest that you might find it useful to slow down and become more methodical. Check each line after you write it to make sure the syntax is correct, and check each code block to spot logic errors and structural errors.

I would second Nikos’s observation about indenting code. Workbench does this for you automatically. Are you using a Mac? If so, you’ll need to indent manually – but it’s still worth doing!

Another thing I would suggest is, when you get a bunch of compilation errors, try to narrow it down. Comment out a small block of code rather than a large one. Also, try compiling after you add or change just a few lines. This will make it easier to find your errors.

Since this appears to be one of the things that is causing RonG recurring trouble, here’s a slightly expanded explanation:
When what you mean is “tell me whether or not these things are equal,” such as in a conditional (“if”/“else if”) statement, you need to use ==.
The opposite of that question, “tell me whether or not these things are not equal” is represented with !=.

When what you mean is “make this thing equal this other thing,” such as in some code that you intend to have an effect on the future state of things, you need to use =. There’s no such thing as the opposite of that, since of course “make this thing not equal to this other thing” could have so many different results that the computer has no idea which one you could mean. (“Make brightness not ‘ten’? Uh, how about ‘fifteen’? or ‘orange’?”)

Here’s a linguistic trick. Get in the habit, when typing ==, of saying to yourself “is equivalent to” or “is the same as” rather than “is equal to.” This makes it easier for your brain to track what you’re doing.

As noted earlier in the thread (in an edit), those mistakes won’t cause compilation errors. I’m not seeing any syntax errors in Ron’s latest code, but I’m probably missing something obvious.

Likewise, I mentally say “is now” when I type “=” (not that I use TADS, but I do use Processing occasionally).

Ron, your latest code is missing a close brace before the final semicolon. (Please use the code tag instead of the quote tag.)

modify GlowAction
    execAction() {
        if (gActor.powerLevel != wizard) {
            reportFailure('At this time, you do not have the power of a Wizard.');
        }
        else if (gActor.brightness != 0) {
            "The blue, radiant light quickly fades from your body.";
            gActor.brightness = 0;
        }
        else {
            "You chant the GLOW spell!
            Your body begins to glow with a bright, radiating blue light!"; 
            gActor.brightness = 10;
        }
    }
;

As others have said, it’s absolutely critical that you format consistently so that you can spot mis-matched braces.

Okay guys. I’m going to start all over again with this ring stuff. Look closely at this code and you will see that there is no way to turn off the actor’s glow without taking the ring off his finger. I’m looking for a way to turn off the radiance (glow) just by saying the word GLOW again. In other words, I hoped to turn the actor’s radiance both on and off by using the word GLOW. I thought I might do that by modify the if-else statements but I just can’t figure it out. By the way, this all works as shown below except for the problem described. Any ideas out there?

RonG

+ring: Wearable 'platinum ring' 'platinum ring'
 "Faint cryptic runes are inscribed around the band of the platinum ring."  
  dobjFor(Wear){
  action(){
   inherited();
   "You feel a strange magical power coursing through your body!
    And, you hear the faint echo of the word GLOW surrounding you.";
   gActor.makeWizard();
  }
 }

 dobjFor(Doff){
  action(){
   inherited();
   "You shudder as the power of the ring and the magic of the GLOW chant leave
    you";
   gActor.makeNormal();
  } 
 }
;

  DefineIAction(Glow);
  VerbRule(Glow)
    'glow'
  :GlowAction
  verbPhrase='glow/glowing'
;
 
 modify GlowAction
 execAction(){
   if(gActor.powerLevel!=wizard)
     reportFailure('At this time, you do not have the power of a Wizard.');
   else{
    "You chant the GLOW spell!
     Your body begins to glow with a bright, radiating blue light! ";  
     gActor.brightness=10;
  }    
 }
; 

Ron - see the code excerpt I posted immediately before your last message. It changes the actor’s brightness to 0 if it is something greater than 0, which should do what you want.

Oops, my reply was not meant to criticize. Sorry it came out that way.

As others mentioned, letting the editor do the formatting is very helpful. You can help the editor along with it by making more use of spaces and newlines. For example, don’t write “if(foo==bar){”, but instead “if (foo == bar) {” then hit ENTER, and place the closing brace on its own line. Another good rule to follow is to place the opening brace of methods and functions on its own line (unlike if-statements):

someFunction()
{
    if (foo == bar) {
        // ...
    }
}

I use this myself and it’s a very good way to avoid confusion over what’s a method/function and what’s code inside of that function. It will help in finding out where the next function begins in a long piece of code. Don’t try to make the code too compact. It hinders readability. And always use braces, even when an if-statement only contains one line of code.

Wow! This series of posts has turned into a novel.

RealNC;

No harm done. Looking forward to continuing our relationship. All of you guys have been a ton of help.

bcressey;

Am I correct in assuming that you want me to use the section of code you included in that last post of yours?

Sorry, but I don’t remember anything about the difference between close braces, code tags or quote tags. Could someone show them to me and explain which is which? My memory of those went flying (along with other things) when I turned seventy years of age.

I always assumed that the number of open braces should always equal the number of close braces in a section of code. Is that correct?

Yes, please try it.

Quote and code tags are for posting here. Code tags preserve indentation and quote tags do not, so it’s important to use code tags when posting fragments of your source code.

That’s correct. If you miss a brace you will often get lots of compiler errors - tens or hundreds depending on where in the file it happens.