Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
jcmcbeth
Beginner


Joined: 18 Sep 2008
Posts: 13
Location: Charlotte, NC

PostPosted: Thu Sep 18, 2008 12:35 am   

Parsing Problem with switch statements
 
I noticed a problem with how CMud parses switch statements, I did something like this:

Code:
#SWITCH (@STATE)
(@STATE_A) {
   // some code
}
(@STATE_B) {
   // some code
}
(@STATE_C) {
   // some code
}


I get an error on "(@STATE_A) {" and each other similiar line.

However it works when I do:

Code:
#SWITCH (@STATE) (@STATE_A) {
   // some code
} (@STATE_B) {
   // some code
} (@STATE_C) {
   // some code
}
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Thu Sep 18, 2008 2:57 am   
 
You have your switch statement written incorrectly. The way you have it written is to test the value of a single variable, which in your case is "@STATE". If you want to test multiple variables like you have there it would be as follows:

Code:

#SWITCH (@STATE) {//some code}
  (@STATE_A) {// some code}
  (@STATE_B) {// some code}
  (@STATE_C) {// some code}


To test the value of a single variable it would be like this:

Code:

#SWITCH (@STATE)
  ("A") {// some code}
  ("B") {// some code}
  ("C") {// some code}


or like this

Code:

#SWITCH (@STATE) ("A") {// some code} ("B") {// some code} ("C") {// some code}
Reply with quote
jcmcbeth
Beginner


Joined: 18 Sep 2008
Posts: 13
Location: Charlotte, NC

PostPosted: Thu Sep 18, 2008 4:05 am   
 
Both of my examples are the same but with different spacing, which is the point I was trying to make.
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu Sep 18, 2008 4:59 am   
 
#SWITCH is very sensitive to proper spacing, that's known issue. In your first example the parser has hard time trying to figure out whether (@STATE_A) { belongs to #SWITCH or no? Put every line in #SWITCH (except the first line) 1 level deeper.


Last edited by Arde on Thu Sep 18, 2008 4:35 pm; edited 1 time in total
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Thu Sep 18, 2008 11:21 am   
 
jcmcbeth wrote:
Both of my examples are the same but with different spacing, which is the point I was trying to make.


Yes I KNOW they are the same and they are WRONG regardless.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Thu Sep 18, 2008 4:47 pm   
 
To answer your original question on why this doesn't work:
Code:
#SWITCH (@STATE)
(@STATE_A) {
   // some code
}
(@STATE_B) {
   // some code
}
(@STATE_C) {
   // some code
}

It is because you can't have parentheses () at the beginning of the line. This would work:
Code:
#SWITCH (@STATE)
  (@STATE_A) {
   // some code
}
  (@STATE_B) {
   // some code
}
  (@STATE_C) {
   // some code
}
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Thu Sep 18, 2008 5:55 pm   
 
oldguy2 wrote:
jcmcbeth wrote:
Both of my examples are the same but with different spacing, which is the point I was trying to make.


Yes I KNOW they are the same and they are WRONG regardless.


But not because of the multiple variable thing though. :)
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Thu Sep 18, 2008 7:51 pm   
 
Well whatever. Someone rewrite the help file then. Confused
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Thu Sep 18, 2008 8:06 pm   
 
jcmcbeth, what exactly are you trying to accomplish with this. If I read this correctly, you want the variable STATE to be analyzed, and if it matches STATE_A then do one thing and etc. But you also want to allow for STATE_A to change (hence using a variable reference to it) but no matter what STATE_A is, as long as it matches STATE to execute the associated code.

Is that correct?
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Thu Sep 18, 2008 8:14 pm   
 
Why create multiple variables to contain the same exact value? That's not how you are supposed to use #switch. It will work only if @state_b or @state_b or @state_c match @state, but what's the point of that? What if all 3 match @state? It will only ever fire the commands for @state_a.

Never mind. I was trying to do an example but totally lost track of what I was doing. lol. It was something along the lines of what if @state changed then one of the other states changed but it got confusing and I have a headache.
Reply with quote
jcmcbeth
Beginner


Joined: 18 Sep 2008
Posts: 13
Location: Charlotte, NC

PostPosted: Fri Sep 19, 2008 5:37 pm   
 
Well, I was just creating an example of the parsing problem I was having. However, it it similiar to what i'm doing. I have a variable @state, and I have different states, say, @STATE_MOVING, @STATE_DIGGING, etc. These are constants, they shouldn't change and all have different values. The only variable that will change is @state.

So, I wanted to check if @state is @STATE_MOVING then do something, if it is @STATE_DIGGING do something else, I just wanted my code to be more readable.

If there is a better way of doing constants, let me know.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Fri Sep 19, 2008 7:35 pm   
 
A general basic programming rule is that if the constant could change then it should be defined in one place and referenced elsewhere. The number of places it is referenced also kind of matters, if it just a few and the constant won't change often then it probably easy enough to place a comment at each reference to remind you to change the others.

Each variable lookup has a speed impact, so anytime you can make the constant be a text string or number you will get greater speed.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Dumas
Enchanter


Joined: 11 Feb 2003
Posts: 511
Location: USA

PostPosted: Fri Sep 19, 2008 8:42 pm   
 
The thing about SWITCH is that it isn't normally used to compare different variables, because of what oldguy2 last mentioned. If @STATE is a text string, for example, then this may work best in this example.

Code:
#SWITCH (@STATE)
  ("STATE_MOVING") {// some code}
  ("STATE_DIGGING") {// some code}


I don't see a need to have separate variables for STATE_MOVING, etc. Whenever you enter those states, you just change STATE to equal the state you want and the trigger that fires this code would work.

Of course, there may be a reason you are using variables for each state that I am unaware of.
Reply with quote
jcmcbeth
Beginner


Joined: 18 Sep 2008
Posts: 13
Location: Charlotte, NC

PostPosted: Fri Sep 19, 2008 9:51 pm   
 
To change the state I would just say
Code:
state = @STATE_MOVING;


Yeah, I could have just said
Code:
state = "STATE_DIGGING";


but if you used C++ or other programming languages you wouldn't go

Code:
if (state == "STATE_DIGGING") do something;


You'd say

Code:
const int STATE_DIGGING = 1;

if (state == STATE_DIGGING) do something;


I just took that concept of not using magic numbers and applied it to a cmud script, and while there aren't const in cmud scripts and there really isn't much of a difference between using a variable and a string. At least when I look at my list of variables I can see all list of my possible states, and if I mistype a variable the syntax highlighter will indicate that the variable doesn't exist.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net