Conditional Actions in custom WML tag

Discussion of Lua and LuaWML support, development, and ideas.

Moderators: Forum Moderators, Developers

Post Reply
Smok
Posts: 52
Joined: June 14th, 2016, 11:52 am

Conditional Actions in custom WML tag

Post by Smok » May 10th, 2017, 7:44 am

How can I allow using conditional actions like if, for, command inside my custom wml tag and it subtags?

User avatar
Celtic_Minstrel
Developer
Posts: 1148
Joined: August 3rd, 2012, 11:26 pm
Contact:

Re: Conditional Actions in custom WML tag

Post by Celtic_Minstrel » May 11th, 2017, 11:18 pm

If you want to be run arbitrary ActionWML contained in the "actions" subtag of your tag, then something like this:

Code: Select all

local helper = wesnoth.require "lua/helper.lua"
function wesnoth.wml_actions.your_tag(cfg)
    wesnoth.wml_actions.command(helper.child(cfg, "actions"))
end
If you want to run any if, for, command subtags and not generic ActionWML, but the contents of those tags are still generic ActionWML, then it'd be a bit harder; you would have to iterate over the config with ipairs(), check the key, and call the appropriate tag if it's one that you want to accept. Don't just use helper.child_range (unless there's only one type of tag you want to execute), because that'll result in executing the out of order. For example, to call all the "if" and "for" subtags of your tag, something like this:

Code: Select all

function wesnoth.wml_actions.your_tag(cfg)
    for i,v in ipairs(cfg) do
        if v[1] == "if" or v[1] == "for" then
            wesnoth.wml_actions[v[1]](v[2])
        end
    end
end
If you want to use conditionals in a different way, such as how animations or the storyscreen use them, then I think you basically need to reimplement the conditional logic for the tags yourself. This is not difficult; you can probably copy the implementations from wml-tags.lua (1.12) or wml-flow.lua (1.13) and just replace any call to the handle_event_commands function with some logic of your own.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

Smok
Posts: 52
Joined: June 14th, 2016, 11:52 am

Re: Conditional Actions in custom WML tag

Post by Smok » May 12th, 2017, 1:00 am

As I want to run conditionals for my own subtags, not wml actions, the last solution will be my solution. :hmm:

User avatar
Celtic_Minstrel
Developer
Posts: 1148
Joined: August 3rd, 2012, 11:26 pm
Contact:

Re: Conditional Actions in custom WML tag

Post by Celtic_Minstrel » May 12th, 2017, 2:41 am

Come to think of it, maybe you could temporarily override the default WML actions table, something like this:

Code: Select all

local save_wml_actions = wesnoth.wml_actions
wesnoth.wml_actions = {}
wesnoth.wml_actions["if"] = save_wml_actions["if"]
wesnoth.wml_actions["for"] = save_wml_actions["for"]
wesnoth.wml_actions.command = save_wml_actions.command
-- Add any other actions you want to allow.
wesnoth.wml_actions.command(cfg)
wesnoth.wml_actions = save_wml_actions
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

Post Reply