[Engine] WML [tunnel] delayed_variable_substitution tag

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

Moderator: Forum Moderators

Forum rules
Before posting a new idea, you must read the following:
Post Reply
User avatar
Samonella
Posts: 381
Joined: January 8th, 2016, 5:41 pm
Location: USA

[Engine] WML [tunnel] delayed_variable_substitution tag

Post by Samonella »

So I was trying to make a map where units can teleport across the east-west border, meaning the world basically wraps around like in a game of civilization or something. I just spent a couple hours figuring out why this didn't work:

Code: Select all

    [event]
        name=prestart
        [store_map_dimensions]
        [/store_map_dimensions]
        [store_locations]
            x=$map_size.width
            terrain=!,*^X*
            include_borders=no
            variable=east_border.locs
        [/store_locations]
        {FOREACH east_border.locs i}
            [tunnel]
                [filter]
                [/filter]
                [source]
                    x=$east_border.locs[$i].x
                    y=$east_border.locs[$i].y
                [/source]
                [target]
                    x=1
                    y=$east_border.locs[$i].y
                [/target]
            [/tunnel]
        {NEXT i}
    [/event]
As you can probably easily tell after reading the name of this topic, it failed because the variables didn't substitute in at the tunnel's creation time. Instead, whenever you move a unit, each tunnel then checks the values of east_border.locs[$i], and, since i didn't even exist at that point, the tunnels just mysteriously didn't work (even though the path-finding algorithm still took an unusually long time).

I tried using $|east_border.locs[$|i].y (syntax that works in nested events to prevent delayed_variable_substitution) but that didn't work, so I eventually had to wrap each [tunnel] in a new event like this:

Code: Select all

    [event]
        name=prestart
        [store_map_dimensions]
        [/store_map_dimensions]
        [store_locations]
            x=$map_size.width
            terrain=!,*^X*
            include_borders=no
            variable=east_border.locs
        [/store_locations]
        {FOREACH east_border.locs i}
            [event]
                name=start
                delayed_variable_substitution=no
                [tunnel]
                    [filter]
                    [/filter]
                    [source]
                        x=$east_border.locs[$i].x
                        y=$east_border.locs[$i].y
                    [/source]
                    [target]
                        x=1
                        y=$east_border.locs[$i].y
                    [/target]
                [/tunnel]
            [/event]
        {NEXT i}
    [/event]
This works just fine, but it's more complicated than seems necessary, and it was a royal pain in the butt to figure out the first time. If [tunnel] had a delayed_variable_substitution tag, not only would it save code like this from pointless nested events, but just seeing it on the wiki would encourage wml authors to think about when variable substitution happens. That would definitely be a good thing, since I'm sure I'm not the only one who frequently makes this kind of mistake.
The last few months have been nothing but one big, painful reminder that TIMTLTW.

Creator of Armory Mod, The Rising Underworld, and Voyage of a Drake: an RPG
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: [Engine] WML [tunnel] delayed_variable_substitution tag

Post by zookeeper »

I'm a bit surprised that that's how it works currently. Seems very unusual, and the fact that it's undocumented would suggest it might even be unintentional. Maybe someone who's more familiar with it will know more.
hvdvyve
Posts: 3
Joined: August 12th, 2017, 8:43 pm

Re: [Engine] WML [tunnel] delayed_variable_substitution tag

Post by hvdvyve »

Could one use this code for north-south as well?
User avatar
Samonella
Posts: 381
Joined: January 8th, 2016, 5:41 pm
Location: USA

Re: [Engine] WML [tunnel] delayed_variable_substitution tag

Post by Samonella »

hvdvyve wrote:Could one use this code for north-south as well?
If you modified it a bit, sure. Though I should warn you that (at least on my slow computer) doing this slowed down the game very significantly: the ai took quite a while to move, and it even took a second or so for the game to show which hexes a unit could move to when I selected it.
The last few months have been nothing but one big, painful reminder that TIMTLTW.

Creator of Armory Mod, The Rising Underworld, and Voyage of a Drake: an RPG
hvdvyve
Posts: 3
Joined: August 12th, 2017, 8:43 pm

Re: [Engine] WML [tunnel] delayed_variable_substitution tag

Post by hvdvyve »

Samonella wrote:
hvdvyve wrote:Could one use this code for north-south as well?
If you modified it a bit, sure. Though I should warn you that (at least on my slow computer) doing this slowed down the game very significantly: the ai took quite a while to move, and it even took a second or so for the game to show which hexes a unit could move to when I selected it.
Ok thx for the code and thx for the warning. If the slowing bothers me I'll take it out afterwards. ^_^
Post Reply