Creating new unit statuses and getting them to show

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.
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Creating new unit statuses and getting them to show

Post by silene »

It has been mentioned in the Lua changes for 1.9.4, but it might be worth advertising, especially since it doesn't really require any Lua knowledge.

Let's suppose you want to define an "entangled" status (that is, an "entangled=yes" line inside the [status] tag of unit). How do you get it to show up in the sidebar next to the invisible, slowed, poisoned statuses? By copy-pasting the code below in a scenario and modifying its literal strings.

Code: Select all

[event]
    name=preload
    first_time_only=no
    [lua]
        code=<<
            local _ = wesnoth.textdomain "mydomain"
            local old_unit_status = wesnoth.theme_items.unit_status
            function wesnoth.theme_items.unit_status()
                local u = wesnoth.get_displayed_unit()
                if not u then return {} end
                local s = old_unit_status()
                if u.status.entangled then
                    table.insert(s, { "element", {
                        image = "entangled.png",
                        tooltip = _"entangled: This unit is entangled. It cannot move but it can still attack."
                    } })
                end
                return s
            end
        >>
    [/lua]
[/event]
The things to modify above are:
  • the domain of your addon ("mydomain"), assuming that you are using translations. Otherwise just remove the underscore in the tooltip line.
  • the name of the status (u.status.entangled). Note that if the attribute happens to be inside [variables], so be it: u.variables.whatever.
  • the path to the image ("entangled.png").
  • the tooltip of the status ("entangled: This unit ...").
Unit statuses are not the only part of the user interface you can change. Every item tag that appears in data/themes/default.cfg can be modified. And if you are using a custom theme for your addon, you can add whatever theme item you wish and get it to display custom content.

Ever wanted to display the equipment carried by units in the sidebar for a Wesnoth RPG? In 1.9.4, you can.[/advertisement]
User avatar
StDrake
Posts: 996
Joined: July 21st, 2009, 6:50 am

Re: Creating new unit statuses and getting them to show

Post by StDrake »

that's nice, will save some writing in wml to make special images to show a unit status..but can this be used also to simplify defining what that status affects and pershaps a default way of getting rid of it?
That would really cut a few lines off the macro files, when the whole thing left would be to just write the event for applying a status, without tediously repeating all its effects and undoing them one by one in another
Like cats? I've made a whole faction of them to kick ass with!
Don't like cats? I've made a whole faction of them to kick their asses! So everyone's happy :)
Felinian faction is part of the Beyond Southern Hells era
kitties need sprites! art topic here
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Creating new unit statuses and getting them to show

Post by zookeeper »

:shock: Cool. Something I've always wanted.
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: Creating new unit statuses and getting them to show

Post by bigkahuna »

silene wrote:Unit statuses are not the only part of the user interface you can change. Every item tag that appears in data/themes/default.cfg can be modified. And if you are using a custom theme for your addon, you can add whatever theme item you wish and get it to display custom content.
Wow :shock: This is a really neat feature. One thing I don't understand though -

In the above example of Lua, you've given us the needed attributes (fill-in-the-blanks if you will). But if we want to add/alter other themes, how would we find the needed attributes? For example -

When I look in the themes/default.cfg , all I see is this -

Code: Select all

            [unit_status]
                id=unit-status
                font_size={DEFAULT_FONT_SMALL}
                ref=unit-xp
                rect="=+1,+3,=,+16"
                xanchor=right
                yanchor=fixed
            [/unit_status]
and nothing about the image path, tooltips, or name of status. Where are all the other attributes/aspects of the different themes defined? I cannot find it in any of the files.
Check out my campaign Sweet Revenge!
Join the new R2D forum!
User avatar
StDrake
Posts: 996
Joined: July 21st, 2009, 6:50 am

Re: Creating new unit statuses and getting them to show

Post by StDrake »

the status name gets bashed in as a varible..or rather container variable name, and from what i see the image and tooltip somehows are qualities of "element"..just where does this "element" come from?
Like cats? I've made a whole faction of them to kick ass with!
Don't like cats? I've made a whole faction of them to kick their asses! So everyone's happy :)
Felinian faction is part of the Beyond Southern Hells era
kitties need sprites! art topic here
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Creating new unit statuses and getting them to show

Post by silene »

StDrake wrote:but can this be used also to simplify defining what that status affects and pershaps a default way of getting rid of it?
That's orthogonal. The only thing that was missing for custom statuses was to display them the same way hardcoded ones are; so that's what I added. Everything else could already be done in WML and doesn't require any specific engine support.
bigkahuna wrote:When I look in the themes/default.cfg , all I see is this and nothing about the image path, tooltips, or name of status. Where are all the other attributes/aspects of the different themes defined? I cannot find it in any of the files.
The .cfg theme files only define which boxes are displayed, what their name is, and where they are displayed. They sometimes also contain static parts of the interface, e.g. the village icon. But they do not define the dynamic content. That was up to the C++ engine, and now to the LuaWML engine too. For instance, display of unit statuses is done by reports.cpp/generate_reports.cpp and its code is similar to the code I posted (except it is C++ instead of Lua).
StDrake wrote:the status name gets bashed in as a varible..or rather container variable name, and from what i see the image and tooltip somehows are qualities of "element"..just where does this "element" come from?
That's just the way it is called, it doesn't come from anywhere in particular. For instance, assume you have an invisible slowed unit. Then the generator for unit statuses will create the following WML container on-the-fly and the engine will display it:

Code: Select all

#textdomain wesnoth
[element]
  image="misc/invisible.png"
  tooltip=_"invisible: "+_"This unit is invisible. It cannot be seen or attacked by enemy units."
[/element]
[element]
  image="misc/slowed.png"
  tooltip=_"slowed: "+_"This unit has been slowed. It will only deal half its normal damage when attacking and its movement cost is doubled."
[/element]
(Note that text can be displayed by using text= instead of image= inside [element]; it just happens that statuses only use icons.)

In the case of the Lua code I posted, the generator just calls the old generator that handles the default status and gets its result (which is a WML container like the one above) then it adds a new [element] tag to it if the unit has the given status. This new result is then returned to the engine, which displays all the elements one after the other in the box defined by the theme.
User avatar
Reepurr
Posts: 1088
Joined: August 29th, 2010, 5:38 pm

Re: Creating new unit statuses and getting them to show

Post by Reepurr »

Awesome! But...

I already know you can implement your own statuses with their own names from my experiments with A New Land. What I don't understand is how you say what they do.

While I'm on the subject, where do you find the LUA hardcode for specials tags, so you would be able to modify them?
"What do you mean, "a dwarvish dragonguard with marksman is overpowered"?"

Story of a Drake Outcast | The Nonsense Era
Played HttT-Underground Channels? Thought it was rubbish? Help us develop it here!
User avatar
pauxlo
Posts: 1047
Joined: September 19th, 2006, 8:54 pm

Re: Creating new unit statuses and getting them to show

Post by pauxlo »

silene wrote:It has been mentioned in the Lua changes for 1.9.4, but it might be worth advertising, especially since it doesn't really require any Lua knowledge.

Let's suppose you want to define an "entangled" status (that is, an "entangled=yes" line inside the [status] tag of unit). How do you get it to show up in the sidebar next to the invisible, slowed, poisoned statuses? By copy-pasting the code below in a scenario and modifying its literal strings.

Code: Select all

[event]
    name=preload
    first_time_only=no
    [lua]
        code=<< ... >>
    [/lua]
[/event]
The things to modify above are:
  • the domain of your addon ("mydomain"), assuming that you are using translations. Otherwise just remove the underscore in the tooltip line.
  • the name of the status (u.status.entangled). Note that if the attribute happens to be inside [variables], so be it: u.variables.whatever.
  • the path to the image ("entangled.png").
  • the tooltip of the status ("entangled: This unit ...").
Wouldn't it be better to define a WML tag which does this?
Maybe usable like this:

Code: Select all

[event]
    name=preload
    first_time_only=no
    [add_status_gui]
       variable="u.status.entangled"
       image="entangled.png"
       tooltip=_ "entangled: This unit is entangled. It cannot move but it can still attack."
    [/add_status_gui]
[/event]
(or with a text=... key for text statuses.) IIRC then we even don't need a special textdomain parameter, since the #textdomain setting from the WML preprocessor applies.
User avatar
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: Creating new unit statuses and getting them to show

Post by Astoria »

*appear from the shadows*

Code: Select all

    [lua]
        code=<<
            local old_unit_status = wesnoth.theme_items.unit_status
            function wesnoth.theme_items.unit_status()
                local u = wesnoth.get_displayed_unit()
                if not u then return {} end
                local s = old_unit_status()
                if u.status.cfg.status then
                    table.insert(s, { "element", {
                        image = "cfg.image",
                        tooltip = _"cfg.tooltip"
                    } })
                end
                return s
            end
            wesnoth.register_wml_action("add_status_gui", cfg)
        >>
    [/lua]
*disappear into the shadows*
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Brilliand
Posts: 80
Joined: July 11th, 2009, 12:15 am

Re: Creating new unit statuses and getting them to show

Post by Brilliand »

LightFighter wrote:*appear from the shadows*

Code: Select all

    [lua]
        code=<<
            local old_unit_status = wesnoth.theme_items.unit_status
            function wesnoth.theme_items.unit_status()
                local u = wesnoth.get_displayed_unit()
                if not u then return {} end
                local s = old_unit_status()
                if u.status.cfg.status then
                    table.insert(s, { "element", {
                        image = "cfg.image",
                        tooltip = _"cfg.tooltip"
                    } })
                end
                return s
            end
            wesnoth.register_wml_action("add_status_gui", cfg)
        >>
    [/lua]
*disappear into the shadows*
Wouldn't that need to be...

Code: Select all

                        image = cfg.image,
                        tooltip = cfg.tooltip
You are a Dark Adept: you dimmerse yourself in the dark arts...potentially with great rewards....
-JW's personality quiz
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: Creating new unit statuses and getting them to show

Post by bigkahuna »

Brilliand wrote:Wouldn't that need to be [...]
Well, the tooltip is supposed to be translateable. And the quotes around them are supposed to be there.
Check out my campaign Sweet Revenge!
Join the new R2D forum!
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Creating new unit statuses and getting them to show

Post by Anonymissimus »

"cfg.image" is considered a string by lua in any case so it looks for a file named "cfg.image". Brilliand is right.
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
User avatar
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: Creating new unit statuses and getting them to show

Post by Astoria »

Anonymissimus wrote:"cfg.image" is considered a string by lua in any case so it looks for a file named "cfg.image". Brilliand is right.
*facepalm* how could I forget...
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: Creating new unit statuses and getting them to show

Post by bigkahuna »

1.9.4 is out! Excited, I looked through the changelog expecting to find this.
silene wrote:Ever wanted to display the equipment carried by units in the sidebar for a Wesnoth RPG? In 1.9.4, you can.
I didn't. Is there something I missed, is it a hidden update, or was it forgotten?
Check out my campaign Sweet Revenge!
Join the new R2D forum!
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Creating new unit statuses and getting them to show

Post by silene »

bigkahuna wrote:Is there something I missed, is it a hidden update, or was it forgotten?
It is in 1.9.4 and mentioned here: http://forums.wesnoth.org/viewtopic.php?f=58&t=31022.
Post Reply