WFL formula error FIXED

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
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

WFL formula error FIXED

Post by Spannerbag »

Edit: Subject changed.

Hi,
I was changing working code (as you I do) and managed to generate a WFL error when the macro containing the WFL is called (i.e. the error appears on screen during gameplay).

The error is:

Code: Select all

error scripting/lua: Formula error in formula:1
In formula + 
Error: Illegal unary operator: '+'
stack traceback:
	[C]: in field 'compile_formula'
	lua/wml/set_variable.lua:160: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:37: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:19: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:206: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:5: in function <lua/wml-flow.lua:4>
	[C]: in ?
	[C]: in field 'fire'
	lua/wml-tags.lua:151: in local 'cmd'
	lua/wml-utils.lua:145: in field 'handle_event_commands'
	lua/wml-flow.lua:5: in function <lua/wml-flow.lua:4>
	[C]: in ?
So I replaced the WFL with WML and it works fine.

Code: Select all

             {VARIABLE    extra_fog_x $this_item.x}
             {VARIABLE_OP extra_fog_x add $fog_range}
             {VARIABLE_OP extra_fog_x add 2}
#            [set_variable]
#              name=extra_fog_x
#              formula="$this_item.x + $fog_range + 2"
#            [/set_variable]
Can WFL handle 2 variables?
I suspect it cannot because previously the WFL still had 3 terms but $fog_range was an integer constant: {FOG_RANGE} and it worked OK then, adding the second $variable seemed to consufe it...

I :inspected $fog_range and it has the expected (correct) value - actually same as {FOG_RANGE}.

Happy to use WML, just curious what I'm doing wrong?

Cheers!
-- Spannerbag
Last edited by Spannerbag on July 15th, 2024, 7:38 pm, edited 1 time in total.
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: WFL formula error

Post by white_haired_uncle »

"Unexpected unary operator +" means you've got a + sign without two arguments. Like " + 3" instead of "x + 3".

Probably because $fog_range is nil, and maybe $this_item is as well.

I suck at WFL, but don't you have to use $ for variable substitution? Looks to me like you're trying to use WML variables in a WFL.

value = $($this_item.x + $fog_range + 2) # MAYBE???
Speak softly, and carry Doombringer.
User avatar
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: WFL formula error

Post by Spannerbag »

white_haired_uncle wrote: July 15th, 2024, 7:25 pm ...
foo = $($this_item.x + $fog_range + 2) # MAYBE???
Ok, will try that ...
...
Yep, fixed!
Thanks!
(Should've thought of that myself :doh: )
Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Soliton
Site Administrator
Posts: 1732
Joined: April 5th, 2005, 3:25 pm
Location: #wesnoth-mp

Re: WFL formula error FIXED

Post by Soliton »

In formula="$this_item.x + $fog_range + 2" there is no formula variable at all. There are two WML variables that get expanded into the formula. If that results in an invalid formula then you'll get an error.

For a key that is specifically documented to contain a formula you don't need any other syntax. The key=$(formula) syntax should work for any key that is not documented to contain a formula but supports variables. (https://wiki.wesnoth.org/SyntaxWML#Spec ... ute_Values)
"If gameplay requires it, they can be made to live on Venus." -- scott
User avatar
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: WFL formula error FIXED

Post by Spannerbag »

@Soliton thanks for taking the time to explain, much appreciated.
Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: WFL formula error FIXED

Post by Celtic_Minstrel »

This is weird. Both of the following should work exactly the same:

Code: Select all

[set_variable]
	name=extra_fog_x
	formula="$this_item.x + $fog_range + 2"
[/set_variable]
[set_variable]
	name=extra_fog_x
	value = "$($this_item.x + $fog_range + 2)"
[/set_variable]
There's no actual advantage to using the first form in this scenario (at least in theory, its main advantage would be when the variable is a container variable, though I seem to recall it not working at all for that case). But in principal there is no significant difference in what's happening between the two cases:
  1. First, substitute in the $fog_range variable. If the variable is not defined, that means deleting it to produce $this_item.x + + 2.
  2. Next, substitute in $this_item.x. If $this_item is not defined, that means deleting it to produce + + 2.
  3. Finally, evaluating the formula, which will give an error if it discovers invalid syntax.
And this is most definitely wrong, so I hope you didn't omit those quotes:

Code: Select all

[set_variable]
	name=extra_fog_x
	value = $($this_item.x + $fog_range + 2)
[/set_variable]
Here the + means WML concatenation, so if it's working when you do it like that, that probably means that both $this_item and fog_range are undefined and the resulting value is always 2.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 761
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: WFL formula error FIXED

Post by Spannerbag »

Celtic_Minstrel wrote: July 22nd, 2024, 1:25 pm This is weird...
I agree.

Just tested the following code:

Code: Select all

# Test formula alternatives
  [event]
    name=side 1 turn 1
    {VARIABLE v1 1}
    {VARIABLE v2 2}
{DEBUG_MSG (_"v1=$v1 and v2=$v2, about to compare formulas for v1+v2+2")}
    [set_variable]
      name=fa
      formula="$v1 + $v2 + 2"
    [/set_variable]
{DEBUG_MSG (_"Formula ""v1+v2+2"" produced ""$fa""")}
    [set_variable]
      name=fb
      formula="$($v1 + $v2 + 2)"
    [/set_variable]
{DEBUG_MSG (_"Formula with dollarsign and brackets produced ""$fb""")}
    [if]
      {VARIABLE_CONDITIONAL fa equals $fb}
      {VARIABLE_CONDITIONAL fa numerical_equals $fb}
    [then]
{DEBUG_MSG (_"Match")}
    [/then]
    [else]
{DEBUG_MSG (_"Ooops")}
    [/else]
    [/if]
  [/event]
$fa=$fb=5 and "Match" reported so there was something about my code in the original setting that was different/broken.

The scenario code is inside a macro and executes several nested events deep.
It's also inside a foreach loop and $this_item is, I guess, a container variable?


I could try the old format again in the "live" code and see if I still get the same issue, but I can't do it right now as I'm busy making a mess of writing another scenario that uses the same logic and I'm not in a position to quickly playtest this right now. Will be much easier when this scenario is completed and thoroughly playtested (so hopefully before Christmas :) ).

Thanks for your post, much appreciated.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Ravana
Forum Moderator
Posts: 3313
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: WFL formula error FIXED

Post by Ravana »

Illegal unary operator means value you assume to be present is not present. Can be made null safe by $empty -0 since - is valid both as unary and binary.
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: WFL formula error FIXED

Post by Celtic_Minstrel »

Though that only works if you expect it to be a number.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply