Switch statement with more than one option on a line

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Switch statement with more than one option on a line

Post by xavram »

Is something like this possible? I know that as is, it throws an error.

[switch(spellName), code:
case "Barkskin": {
[h : stateName = "Barkskin2"]
[h, if(theCasterLevel >= 6) : stateName = "Barkskin3"]
[h, if(theCasterLevel >= 9) : stateName = "Barkskin4"]
[h, if(theCasterLevel >= 12) : stateName = "Barkskin5"]
[h : buff = json.set(buff, "Duration", theCasterLevel*100, "State1", stateName)]
};
case "A", "B", "Z": {
[h : buff = json.set(buff, "Duration", theCasterLevel*100, "State1", "ABorZ")]
};

But is it possible to have multiple options in a given case? If so, is there a specific syntax?


xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Switch statement with more than one option on a line

Post by xavram »

I guess i'm not clear where the "code blocks" should be in this situation.

Code as written throws

line 1:9: unexpected char: '['

as error.

Entirely of this test macro looks like...

Code: Select all

[h : spellName = spellName]

[h : buff = json.set("{}", "SpellName", spellName)]

[h : theCasterLevel = 10]

[switch(spellName), code:
case "Barkskin": {
	[h : stateName = "Barkskin2"]
	[h, if(theCasterLevel >= 6) : stateName = "Barkskin3"]
	[h, if(theCasterLevel >= 9) : stateName = "Barkskin4"]
	[h, if(theCasterLevel >= 12) : stateName = "Barkskin5"]
	[h : buff = json.set(buff, "Duration", theCasterLevel*100, "State1", stateName)]		
  };
case "A": {
	[h : buff = json.set(buff, "Duration", theCasterLevel*100, "State1", "ABorZ")]			
};
case "Breath of Life": {
	[h : isCureSpell = 1]
  };
case "Cleanse": {
	[h : isCureSpell = 1]
  };
case "Cure Light Wounds": {
	[h : isCureSpell = 1]
  };
case "Cure Moderate Wounds": {
	[h : isCureSpell = 1]
  };
case "Cure Serious Wounds": {
	[h : isCureSpell = 1]
  };
case "Cure Critical Wounds": {
	[h : isCureSpell = 1]
  };
case "Enlarge Person": {
	[h : buff = json.set(buff, "Duration", theCasterLevel*10, "State1", "EnlargePerson")]	
  };
default : {
    [h : tbd = 5]
  };
]

[broadcast(buff)]
And if its like that (where "A" is its own line, as opposed to something like "A", "B", "C" on that line), then it works fine.

But I was hoping there would be a way to group all the similar spells (mainly the cure ones) on a single line, for readability.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Switch statement with more than one option on a line

Post by aliasmask »

Unfortunately switch statements in java suck. You would need to go with a series of if statements, which is what I usually do.

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Switch statement with more than one option on a line

Post by Full Bleed »

Or you can just give A, B, and Z their own case with the same result in each.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Switch statement with more than one option on a line

Post by aliasmask »

The weird part about java switch statement that it uses regex for the condition, so you could potentially match multiple cases, but it only executes the first or last one (I don't recall). For example:

Code: Select all

[H: condition = "a.*"]
[H, switch(condition):
case "apple": b = "apple";
case "ape": b = "ape";
case "bear": b = "bear";
default: b = "no choice"
]
[r: b]
output is "apple", so it's the first matching. I haven't really found a use for this.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Switch statement with more than one option on a line

Post by wolph42 »

apologies, I misread your post. No that's not possible. I thought you meant the usual use of options e.g. 'h,' within switch. Everything has been said: either use multiple case lines (one for each) or switch to if()

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: Switch statement with more than one option on a line

Post by xavram »

Gotcha...no worries, it can be split into multiple lines...just would have made life a little easier!

Post Reply

Return to “MapTool”