Conditional Actions in custom WML tag
Moderator: Forum Moderators
Conditional Actions in custom WML tag
How can I allow using conditional actions like if, for, command inside my custom wml tag and it subtags?
- Celtic_Minstrel
- Developer
- Posts: 2211
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Conditional Actions in custom WML tag
If you want to be run arbitrary ActionWML contained in the "actions" subtag of your tag, then something like this:
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
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
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
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
handle_event_commands
function with some logic of your own.Re: Conditional Actions in custom WML tag
As I want to run conditionals for my own subtags, not wml actions, the last solution will be my solution.
- Celtic_Minstrel
- Developer
- Posts: 2211
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Conditional Actions in custom WML tag
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