variable expansion

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
white_haired_uncle
Posts: 1226
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

variable expansion

Post by white_haired_uncle »

Consider the following, taken out of context:

Code: Select all

        [foreach]
            array=guards
            [do]
                {VARIABLE_OP index rand (0..4)}
                [switch]
                    variable=index
                    [case]
                        value=0
                        [modify_unit]
                            [filter]
                                id=$this_item.id
                            [/filter]
                            [object]
                                id=temp_obj
                                [effect]
                                    apply_to=attack
                                    range=melee
                                    [set_specials]
                                        mode=append
                                        {WEAPON_SPECIAL_UNHOLYBANE}
                                        {WEAPON_SPECIAL_MAGICAL}
                                    [/set_specials]
                                [/effect]
                            [/object]
                        [/modify_unit]
                    [/case]
                    [case]
                        value=1
                        [modify_unit]
                            [filter]
                                id=$this_item.id
                            [/filter]
                            [object]
                                id=temp_obj
                                [effect]
                                    apply_to=attack
                                    range=melee
                                    [set_specials]
                                        mode=append
                                        {WEAPON_SPECIAL_LESSER_BERSERK 5}
                                        {WEAPON_SPECIAL_LEECH}
                                        {WEAPON_SPECIAL_MAGICAL}
                                    [/set_specials]
                                [/effect]
                            [/object]
                        [/modify_unit]
                    [/case]
                    [case]
            		value=2
            ...
                        
As you can see, the whole modify_unit block is pretty redundant, which bothers me (there are a more [case]s, just showing two as an example). I'd like to do something like this (code is wrong, hopefully my intent will be clear):

Code: Select all

      [foreach]
            array=guards
            [do]
                {VARIABLE_OP index rand (0..4)}
                [switch]
                    variable=index
                    [case]
                        value=0
                        [set_variable]
                            name=specials
                            value={WEAPON_SPECIAL_UNHOLYBANE} + {WEAPON_SPECIAL_MAGICAL}
                        [/set_variable]
                    [/case]
                    [case]
                        value=1
                        [set_variable]
                            name=specials
                            value= {WEAPON_SPECIAL_LESSER_BERSERK 5} + {WEAPON_SPECIAL_LEECH} + {WEAPON_SPECIAL_MAGICAL}
                        [/set_variable]
                    [/case]
                [/switch]
                [modify_unit]
                    [filter]
                        id=$this_item.id
                    [/filter]
                    [object]
                        id=temp_obj
                        [effect]
                            apply_to=attack
                            range=melee
                            [set_specials]
                                 mode=append
                                 $specials
                            [/set_specials]
                        [/effect]
                    [/object]
                [/modify_unit]
I've seen a [literal] used and literal= I think, which I always thought would be for cases like that, but when ever I look into it I'm not clear what it does but it doesn't seem quite right.
Speak softly, and carry Doombringer.
User avatar
Spannerbag
Posts: 538
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: variable expansion

Post by Spannerbag »

As you're dealing with one unit, I think you can maybe dispense with [modify_unit] and put the [filter] in [object]?
It would save a bit of space - but it's nothing like what you want to do so maybe something to consider if no-one else suggests anything better?

[insert_tag] might be able to do what you want but I've never used it so don't know for sure...

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Ravana
Forum Moderator
Posts: 3018
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: variable expansion

Post by Ravana »

It is possible to make it shorter but I think it is not worth the effort yet. Your first example looks similar to orocia upgrades before I rewrote them in Lua.

If you want to try then yes, [insert_tag] will solve your use case.
User avatar
Heindal
Posts: 1360
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: variable expansion

Post by Heindal »

Maybe I'm wrong, but as far I remember you can't use numbers as value in strings but strings only? It could be that this info is deprecated already, but in older versions I couldn't use numeric values for whatever reason.
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
User avatar
Celtic_Minstrel
Developer
Posts: 2241
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: variable expansion

Post by Celtic_Minstrel »

You can't use [set_variable] for container variables. It just won't work. There's no way to make your false example work in a similar way. You could maybe work something out with [set_variables] (note the extra S), but I have a feeling it wouldn't be that much less verbose than your first example. You could also maybe work something out with [insert_tag] that'll make your code verbose; I'm not entirely sure what that would end up looking like.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply