Problem with filtering unit movement points

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.
Post Reply
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Problem with filtering unit movement points

Post by Nyanyanyan »

Hey, I'm trying to make my filter see a unit that has at least 1 movement point, but somehow this doesn't work as I understand it.
Here's my code.

Code: Select all

			
			[have_unit]
				ability=someability
				side=$side_number
				[not]
				moves=0
				[/not]
			[/have_unit]
			
This doesn't find anything, while

Code: Select all

			
			[have_unit]
				ability=someability
				side=$side_number
			[/have_unit]
			
Does, no matter how many movement points the unit in question has.

What am I doing wrong?
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
Pilauli
Posts: 115
Joined: August 18th, 2020, 12:56 pm

Re: Problem with filtering unit movement points

Post by Pilauli »

moves= doesn't actually seem to be a valid key in the standard unit filter. (https://wiki.wesnoth.org/StandardUnitFilter - [have_unit] uses the standard unit filter, and returns true if there is a unit that matches it, so I'm going to be talking about filters through most of this post.)

You know how Wesnoth considers an empty filter to mean "anything goes"? (For example, if you don't write id= in your filter, then it will match units with any ID, rather than hopelessly looking out for units which completely lack IDs.)

Filters also ignore any parts they don't recognize. So Wesnoth evaluates moves=0 as "no idea what that means, so I'll just say true", and then obviously, the opposite of that is false, and so there's a false in your filter, and so it doesn't work.

I'm sure there's some good way to filter for how many moves the unit has left, but the only way I can think of (which is not necessarily good) is to use formulas (https://wiki.wesnoth.org/Wesnoth_Formula_Language - the Standard Unit Filter does at least say that formula= is a valid thing you can write). I can usually eventually get formulas to work, but I haven't tested this, so there could well be some deep flaw in it:

Code: Select all

[have_unit]
    # Various filtery things
    formula=$($self.moves| > 0)
[/have_unit]
I am not actually sure whether a specifically labeled formula area needs the formula-wrapper-thing or not. If the version I just wrote doesn't work, try formula=$self.moves| > 0 instead.

If I've made some terrible mistake, then of course neither of them will work, so I apologize in advance for possibly wasting your time. :)
User avatar
Celtic_Minstrel
Developer
Posts: 2211
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Problem with filtering unit movement points

Post by Celtic_Minstrel »

That's not how the formula language works are all. You're confusing WFL and variable substitution, which are two completely different things.

Variable substitution

Variable substitution is when you write something like $unit.moves in a WML attribute value. This has absolutely nothing to do with the Wesnoth Formula Language. When the game sees these dollar signs, it replaces the variables with the actual value, working its way from right to left (so the right-most variable is substituted first). Variable substitution won't be of any help for your use case.

Wesnoth Formula Language

WFL is a special mini-language for specifying formulas in WML values. You can only use WFL formulas in specific tags and keys that support them; these keys can be of two types. Some are named something like formula and assume that whatever you put in there is definitely a formula. There are also keys that are typically specified as just numbers, but if you wish you can make them a formula by enclosing the whole thing in parentheses. Filters use the first type.

Formula substitution

This is sort of a mixture of the two. The syntax $(stuff here) is a formula substitution, which is a special case of variable substitution that uses a WFL formula to calculate the resulting value. However, this won't be of help for your use case.

-----

Now, for a unit filter, you want just a straight formula. Formulas are usually enclosed in quotes, since they use characters (such as + or # ) that would otherwise have a special meaning to the WML parser. A formula to check if a unit has any moves left would look like this:

Code: Select all

formula="moves > 0"
If you prefer to make the implicit "self" variable clear, you can also write it like this:

Code: Select all

formula="self.moves > 0"
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Re: Problem with filtering unit movement points

Post by Nyanyanyan »

Celtic_Minstrel wrote: October 29th, 2020, 1:07 pm
Now, for a unit filter, you want just a straight formula. Formulas are usually enclosed in quotes, since they use characters (such as + or # ) that would otherwise have a special meaning to the WML parser. A formula to check if a unit has any moves left would look like this:

Code: Select all

formula="moves > 0"
If you prefer to make the implicit "self" variable clear, you can also write it like this:

Code: Select all

formula="self.moves > 0"
Thanks, that worked perfectly. Handy tool to have.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
Post Reply