WML switch tag

Brainstorm ideas of possible additions to the game. Read this before posting!

Moderators: Forum Moderators, Developers

Forum rules
Before posting a new idea, you must read the following:
User avatar
Baufo
Inactive Developer
Posts: 1115
Joined: January 29th, 2006, 4:53 pm
Location: Vienna, Austria

WML switch tag

Post by Baufo »

I think it would be useful to have a [switch] tag in WML, even it of couse could already be done with if tags.

I could work like that:

Code: Select all

[switch]
  [variable]
    name=variable_name
  [/variable]

  [case]
    value=1
    #do something if variable_name equals 1
  [/case]

  [case]
    value=2
    #do something else if variable_name equals 2
  [/case]

[/switch]
I was working on the proof of one of my poems all the morning, and took out a comma. In the afternoon I put it back again. -- Oscar Wilde

toms
Posts: 1717
Joined: November 6th, 2005, 2:15 pm

Post by toms »

I don't think that's worth the effort, and it is already doable...
First read, then think. Read again, think again. And then post!

NeoPhile
Posts: 155
Joined: July 22nd, 2004, 4:33 am
Location: Halifax, NS, Canada
Contact:

Post by NeoPhile »

A switch is more legible than an if in WML, especially when you take indentation into account. I know I would appreciate it.

Dacyn
Posts: 1855
Joined: May 1st, 2004, 9:34 am
Location: Texas

Post by Dacyn »

don't use the [variable] tag; that has a different meaning and it would be better not to reuse tags... just put 'variable=(variable_name)' inside the [switch] tag.

MadMax
Posts: 1792
Joined: June 6th, 2004, 3:29 pm
Location: Weldyn, Wesnoth

Post by MadMax »

I agree with this, because it would eliminate the need for long chains of nested [if] statements required currently.

But, since [if] statements can do this also, it's a low priority.
"ILLEGITIMIS NON CARBORUNDUM"

Father of Flight to Freedom
http://www.wesnoth.org/wiki/FlightToFreedom

User avatar
turin
Lord of the East
Posts: 11662
Joined: January 11th, 2004, 7:17 pm
Location: Texas
Contact:

Post by turin »

You shouldn't need to nest [if]s to do this currently...
For I am Turin Turambar - Master of Doom, by doom mastered. On permanent Wesbreak. Will not respond to private messages. Sorry!
And I hate stupid people.
The World of Orbivm

User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Post by zookeeper »

turin wrote:You shouldn't need to nest [if]s to do this currently...
?

toms
Posts: 1717
Joined: November 6th, 2005, 2:15 pm

Post by toms »

zookeeper wrote:
turin wrote:You shouldn't need to nest [if]s to do this currently...
?
He means a structure like this is not necessary:

if
then
...
else
if
then
...
else
...
end else
end if
end else
end if
First read, then think. Read again, think again. And then post!

User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Post by zookeeper »

toms wrote:
zookeeper wrote: ?
He means a structure like this is not necessary:

if
then
...
else
if
then
...
else
...
end else
end if
end else
end if
........

User avatar
turin
Lord of the East
Posts: 11662
Joined: January 11th, 2004, 7:17 pm
Location: Texas
Contact:

Post by turin »

Right- you should just do

if
then
endif
if
then
endif
...

Not a particularly profound point, but a notable one.
For I am Turin Turambar - Master of Doom, by doom mastered. On permanent Wesbreak. Will not respond to private messages. Sorry!
And I hate stupid people.
The World of Orbivm

NeoPhile
Posts: 155
Joined: July 22nd, 2004, 4:33 am
Location: Halifax, NS, Canada
Contact:

Post by NeoPhile »

turin wrote:Right- you should just do

if
then
endif
if
then
endif
...

Not a particularly profound point, but a notable one.
That's incompatible with else statements. Not else if, just plain old else. A switch usually allows for a default option (notably absent from the example) that handles the situation elegantly.

User avatar
turin
Lord of the East
Posts: 11662
Joined: January 11th, 2004, 7:17 pm
Location: Texas
Contact:

Post by turin »

NeoPhile wrote:That's incompatible with else statements. Not else if, just plain old else. A switch usually allows for a default option (notably absent from the example) that handles the situation elegantly.
True - but what you can do is at the end have an

Code: Select all

if
  not1
  not2
  not3
  then
endif
Anyway, I don't disagree that a switch would be useful, to a degree. Almost any programming feature would be useful for WML. The question is whether the benefit of having this programming feature outweighs 1. the time spent coding it (a one-time cost), 2. the maintainance of the code (a continuing cost), and 3. the burden on the engine by having this new feature (a cost to the users, not the devs). I don't think it is, because what you want can already be done; all this does is save a few dozen lines of WML, and that only in scenarios that would use switch, which would be few and far between.


BTW, this isn't specific to this item - every new feature should be thought about in terms of whether it is worth doing given the specific values of these three costs.
For I am Turin Turambar - Master of Doom, by doom mastered. On permanent Wesbreak. Will not respond to private messages. Sorry!
And I hate stupid people.
The World of Orbivm

User avatar
Elvish_Pillager
Posts: 8129
Joined: May 28th, 2004, 10:21 am
Location: Everywhere you think, nowhere you can possibly imagine.
Contact:

Post by Elvish_Pillager »

This is doable with macros. You'd do:

Code: Select all

{SWITCH (var-name)}
{CASE (value)}
(commands)
{/CASE}
{CASE (value)}
(commands)
{/CASE}
{DEFAULT}
(commands)
{/DEFAULT}
{/SWITCH}
Unfortuantely, I currently have no time to write the macros myself. Perhaps tomorrow.
It's all fun and games until someone loses a lawsuit. Oh, and by the way, sending me private messages won't work. :/ If you must contact me, there's an e-mail address listed on the website in my profile.

Dacyn
Posts: 1855
Joined: May 1st, 2004, 9:34 am
Location: Texas

Post by Dacyn »

Code: Select all

#define SWITCH VARIABLE
[set_variable]
name=switch
to_variable={VARIABLE}
[/set_variable]
[set_variable]
name=switch_done
value=no
[/set_variable]
#enddef
#define CASE VALUE
[if]
 [variable]
 name=switch
 equals={VALUE}
 [/variable]
 [then]
  [set_variable]
  name=switch_done
  value=yes
  [/set_variable]
#enddef
#define /CASE
 [/then]
[/if]
#enddef
#define DEFAULT
[if]
 [variable]
 name=switch_done
 equals=no
 [/variable]
 [then]
#enddef
#define /DEFAULT
{/CASE}
#enddef
#define /SWITCH
#enddef
Can't be nested, though...

User avatar
turin
Lord of the East
Posts: 11662
Joined: January 11th, 2004, 7:17 pm
Location: Texas
Contact:

Post by turin »

Just label them:

Code: Select all

#define SWITCH NUM VARIABLE
[set_variable]
name=switch{NUM}
to_variable={VARIABLE}
[/set_variable]
[set_variable]
name=switch_done{NUM}
value=no
[/set_variable]
#enddef
#define CASE NUM VALUE
[if]
 [variable]
 name=switch{NUM}
 equals={VALUE}
 [/variable]
 [then]
  [set_variable]
  name=switch_done{NUM}
  value=yes
  [/set_variable]
#enddef
#define /CASE
 [/then]
[/if]
#enddef
#define DEFAULT NUM
[if]
 [variable]
 name=switch_done{NUM}
 equals=no
 [/variable]
 [then]
#enddef
#define /DEFAULT
{/CASE}
#enddef
#define /SWITCH
#enddef
And they would be used like this:

Code: Select all

{SWITCH 1 (var-name)}
{CASE 1 (value)}
  {SWITCH 2 (var-name)}
  {CASE 2 (value)}
  {/CASE}
  {CASE 2 (value)}
  (commands)
  {/CASE}
  {DEFAULT 2}
  (commands)
  {/DEFAULT}
{/SWITCH}
{/CASE}
{CASE 1 (value)}
(commands)
{/CASE}
{DEFAULT 1}
(commands)
{/DEFAULT}
{/SWITCH}
Kind of bulky, but if it's really desired...
For I am Turin Turambar - Master of Doom, by doom mastered. On permanent Wesbreak. Will not respond to private messages. Sorry!
And I hate stupid people.
The World of Orbivm

Post Reply