Follow-up Comment #2, bug #23841 (project wesnoth):

Ok i made a new (tested) version that makes the custom wml tags also
persistent (so that they won't be lost when reloading):

Lua code:


local helper = wesnoth.require("lua/helper.lua")
local _ = wesnoth.textdomain 'wesnoth'
local T = helper.set_wml_tag_metatable {}

-- a map string -> vconfig
local wml_defined_tags = {}

local add_new_wml_tag = function(tagname, command)
        if type(command) == "table" then
                command = wesnoth.tovconfig(command)
        end
        wml_defined_tags[tagname] = command
        wesnoth.wml_actions[tagname] = function(args)
                local old_args = wesnoth.get_variable("a")
                wesnoth.set_variable("a", args)
                wesnoth.wml_actions.command(command)
                wesnoth.set_variable("a", old_args)
        end
        
end

local old_on_save = wesnoth.game_events.on_save
wesnoth.game_events.on_save = function()
        local cfg = old_on_save()
        for k,v in pairs(wml_defined_tags) do
                table.insert(cfg, T.wml_defined_tag {
                        tagname = k,
                        T.command(helper.literal(v)),
                })
        end
        return cfg
end

local old_on_load = wesnoth.game_events.on_load
wesnoth.game_events.on_load = function(cfg)
        for i = #cfg,1,-1 do
                local v = cfg[i]
                if v[1] == "wml_defined_tag" then
                        local v2 = v[2]
                        add_new_wml_tag(v2.tagname, helper.get_child(v2, 
"command") )
                        table.remove(cfg, i)
                end
        end
        old_on_load(cfg)
end

wesnoth.wml_actions.make_tag = function(cfg)
        local tagname = cfg.tagname or helper.wml_error "[make_tag] missing 
required
tagname= attribute."
        local command = helper.get_child(cfg, "command") or helper.wml_error
"[make_tag] missing required command= child."
        add_new_wml_tag(tagname, command)
end




How to use:


[make_tag]
##      triggers an advancement for the given unit (which shows an advancement
dialog)
        tagname = "advance_unit"
        [command]
                [store_unit]
                        [filter]
                                x = "$a.x"
                                y = "$a.y"
                        [/filter]
                        ## a variable will be cleared automatically
                        variable = "a.unit"
                [/store_unit]
                [set_variable]
                        name = "a.unit.experience"
                        add = "$a.unit.max_experience"
                [/set_variable]
                [unstore_unit]
                        variable = "a.unit"
                        fire_event = yes
                [/unstore_unit]
        [/command]
[/make_tag]

[advance_unit]
        x,y = 16,17
[/advance_unit]


i also renamed 'args' to 'a', first becasue that is easier to type, but also
because short variable names are usually faster than long ones in execution.

    _______________________________________________________

Reply to this item at:

  <http://gna.org/bugs/?23841>

_______________________________________________
  Nachricht gesendet von/durch Gna!
  http://gna.org/


_______________________________________________
Wesnoth-bugs mailing list
Wesnoth-bugs@gna.org
https://mail.gna.org/listinfo/wesnoth-bugs

Reply via email to