Fighting with { and }

Need some help with that macro you're working on or aren't quite sure how to get your macro to do something? Ask here!

Moderator: MacroQuest Developers

MrDoh
Hypocrite fag
Posts: 10
Joined: Tue Oct 28, 2003 4:01 pm

Fighting with { and }

Post by MrDoh » Tue Oct 28, 2003 4:22 pm

Hello,

I'm fairly new to MQ, but I've been programming for a while now.

I can't seem to grasp the use on the brackets {}

Here's what I mean:

/if "$char(state)"=="SIT" /varset state1 sitting
That works

/if "$char(state)"=="SIT" {/varset state1 sitting}
this doesn't seem to??

/if "$char(state)"=="SIT"{/varset state1 sitting}
neither does this??


I've also tried every other incarnation I can think of, but state1 is always empty.

I guess it's obvious that the brackets really aren't needed in that particular spot, but it's preventing me from using the else statement. Else isn't working for me either, but until I can figure out the brackets, I guess it wont. I've made my if/else statements look like the manuals, but still no luck.

Maybe I have the usage all wrong, so if someone could point me in the right direction I would greatly appreciate it.

Thanks,
MrDoh

Scrime
a ghoul
a ghoul
Posts: 86
Joined: Sun Sep 21, 2003 5:48 pm
Contact:

Post by Scrime » Tue Oct 28, 2003 4:28 pm

Try:

Code: Select all

/if "$char(state)"=="SIT" {
  /varset state1 sitting
} else {
  some other code
}
[url=http://www.catb.org/~esr/faqs/smart-questions.html]asking smart questions[/url]

MrDoh
Hypocrite fag
Posts: 10
Joined: Tue Oct 28, 2003 4:01 pm

What I currently have...

Post by MrDoh » Tue Oct 28, 2003 4:48 pm

This is what I currently have:

Code: Select all

/declare state1

/sub main

/if $char(state)=="SIT" {
  /varset state1 "sitting"
} else {
  /if $char(state)=="STAND"
} {
  /varset state1 standing
} else {
  /if $char(state)=="DUCK"
} {
  /varset state1 ducking
} else {
  /if $char(state)=="BIND"
} {
  /varset state1 binding
} else {
  /if $char(state)=="FEIGN"
} {
  /varset state1 feigning
} else {
  /if $char(state)=="DEAD"
} {
  /varset state1 DEAD!!
} else {
  /if $char(state)=="STUN"
} {
  /varset state1 STUNNED!!
}
/echo You are currently @state1
I've also tried it like

Code: Select all

 ...} else /if ... {...}
Any ideas what I have done wrong? Sure doesn't work for me...
(must be something small and stupid...)

Thanks again!

DekeFentle
a lesser mummy
a lesser mummy
Posts: 48
Joined: Wed Oct 22, 2003 1:41 pm

Post by DekeFentle » Tue Oct 28, 2003 5:04 pm

/delcare state1

should be

/delcare state1 global

At least, I think thats part of the problem. Might I also suggest using the ini file feature? I think you may be nesting if's.

Code: Select all

 
/declare state1 global
/declare state2 global 

/sub main

  /varset state1 $char(state)
  /varset state2 $ini(c:\my.ini,@state1,return1)
  /echo "You are currently @state2"
/return
ini file content...
[SIT]
return1=sitting
return2=sits

[STAND]
return1=standing
return2=stands

etc....
Revelation 6:8

User avatar
dont_know_at_all
Developer
Developer
Posts: 5450
Joined: Sun Dec 01, 2002 4:15 am
Location: Florida, USA
Contact:

Re: What I currently have...

Post by dont_know_at_all » Tue Oct 28, 2003 7:16 pm

MrDoh wrote:This is what I currently have:

Code: Select all

all wrong
What character comes immediately after the string "STAND"?

Mckorr
Developer
Developer
Posts: 2326
Joined: Fri Oct 18, 2002 1:16 pm
Location: Texas

Re: What I currently have...

Post by Mckorr » Tue Oct 28, 2003 7:19 pm

dont_know_at_all wrote:
MrDoh wrote:This is what I currently have:

Code: Select all

all wrong
What character comes immediately after the string "STAND"?
Oh my.....

What DKAA is saying is you have mismatched braces.
MQ2: Think of it as Evolution in action.

LordGiddion
a snow griffon
a snow griffon
Posts: 352
Joined: Sat Sep 13, 2003 6:12 pm
Contact:

Post by LordGiddion » Tue Oct 28, 2003 9:07 pm

you can't have a space between } and Else you need it to be }else {
or }else /if
also
/if x==y {
/varset x 3
}else {
/varset y 4
}

the { and } can't be on the same line

Scrime
a ghoul
a ghoul
Posts: 86
Joined: Sun Sep 21, 2003 5:48 pm
Contact:

Post by Scrime » Tue Oct 28, 2003 9:52 pm

LordGiddion wrote: you can't have a space between } and Else you need it to be }else {
Not true. I have macros that work just fine with:

Code: Select all

/if something {
  somecode
} else {
  someothercode
}
[url=http://www.catb.org/~esr/faqs/smart-questions.html]asking smart questions[/url]

MrDoh
Hypocrite fag
Posts: 10
Joined: Tue Oct 28, 2003 4:01 pm

Fixed it

Post by MrDoh » Wed Oct 29, 2003 1:21 pm

Just incase anyone was wondering... I found, and fixed my problem.

It wasn't the {} or else... the problem seems to be that I did my /declares before sub main.

I left it like it was and added a second /declare below sub main and it worked every time. I then removed the /declare above sub main and left the one below it, and everything continues to work.

Everything else is the same... even the nasty looking formatting and {}'s, and it works.

Thanks for the suggestions.

(oh, and the not putting global on the end of the declare in my message was a typo in the message. It was in the real macro.)

Thanks again

Mr. Doh

User avatar
dont_know_at_all
Developer
Developer
Posts: 5450
Joined: Sun Dec 01, 2002 4:15 am
Location: Florida, USA
Contact:

Post by dont_know_at_all » Wed Oct 29, 2003 2:37 pm

If it works for states other than sitting then you have uncovered a bug in the parser. Don't expect it to work after the next source update.

Plazmic
The One
The One
Posts: 800
Joined: Fri Jun 14, 2002 12:31 am
Contact:

Post by Plazmic » Wed Oct 29, 2003 7:59 pm

more like a weakness of the parser

"} {" is probably (I need to look) getting parsed the same as an "} else {" would. if the first char is } we just look for the last char being a { to simulate staying in the same scope.

It should probably make sure an else is in there, and bitch if there isn't.
- Plazmic

User avatar
grimjack
Macro Author
Macro Author
Posts: 525
Joined: Thu Nov 07, 2002 6:51 am
Contact:

Post by grimjack » Thu Oct 30, 2003 7:05 am

Code: Select all

Sub Main
   /declare state1 local
   /if $char(state)=="SIT" {
      /varset state1 "sitting."
   } else /if $char(state)=="STAND" {
      /varset state1 "standing."
   } else /if $char(state)=="DUCK" {
      /varset state1 "ducking."
   } else /if $char(state)=="BIND" {
      /varset state1 "binding."
   } else /if $char(state)=="FEIGN" {
      /varset state1 "feigning."
   } else /if $char(state)=="DEAD" {
      /varset state1 "DEAD!!"
   } else /if $char(state)=="STUN" {
      /varset state1 "STUNNED!!"
   } else /varset state1 "unknown."
   /echo You are currently @state1
/return
or

Code: Select all

Sub Main
   /declare state1 local
   /varset state1 "unknown."
   /if $char(state)=="SIT" /varset state1 "sitting."
   /if $char(state)=="STAND" /varset state1 "standing."
   /if $char(state)=="DUCK" /varset state1 "ducking"
   /if $char(state)=="BIND" /varset state1 "binding."
   /if $char(state)=="FEIGN" /varset state1 "feigning."
   /if $char(state)=="DEAD" /varset state1 "DEAD!!"
   /if $char(state)=="STUN" /varset state1 "STUNNED!!"
   /echo You are currently @state1
/return
Thanks
When they come to me, they're in trouble, or they want some. I bust people out of prison, hunt down vampires, fight alien gods -- All the fun jobs people are too squeamish or too polite to do themselves.

Call me a mercenary. Call me an assassin. Call me a villain. I am all that and more.

My name's John Gaunt, but out on the streets of Cynosure, I am called...
GrimJack