Trouble with helper.set_variable_array example on wiki

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Trouble with helper.set_variable_array example on wiki

Post by Pentarctagon »

The wiki has a simple example of how to use helper.set_variable_array(). However, the following code based on that example doesn't work:

Code: Select all

[event]
  name="turn 1"
  
  [lua]
    code=<<
      local t1 = 1
      local t2 = 2
      local t3 = 3
      helper.set_variable_array("target", { t1, t2, t3 })
    >>
  [/lua]
  [message]
    message="$target[1]"
  [/message]
[/event]
The message displayed is blank, and the following errors are logged:

Code: Select all

20170429 17:27:12 warning scripting/lua: Cannot resolve variablename 'target[0]' for writing. when attempting to write a 'number'
20170429 17:27:12 warning scripting/lua: Cannot resolve variablename 'target[1]' for writing. when attempting to write a 'number'
20170429 17:27:12 warning scripting/lua: Cannot resolve variablename 'target[2]' for writing. when attempting to write a 'number'
What's going on here?
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Trouble with helper.set_variable_array example on wiki

Post by gfgtdf »

uhm well how would you expect how the variables wml look after it ?
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Trouble with helper.set_variable_array example on wiki

Post by Pentarctagon »

I would expect it to look like the example on the wiki says it should look.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Trouble with helper.set_variable_array example on wiki

Post by gfgtdf »

no, i meant please post the concrete wml code, for example of an equivalent [set_variables][literal]
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Trouble with helper.set_variable_array example on wiki

Post by Pentarctagon »

Well, it would probably be something like:

Code: Select all

[set_variables]
  name=target
  [value]
    1
  [/value]
  [value]
    2
  [/value]
  [value]
    3
  [/value]
[/set_variables]
which I'm pretty sure isn't valid, but IIRC Wesnoth still allows doing something like this(though it warns about it in the log):

Code: Select all

{VARIABLE target[0] 1}
Either way though, the following still doesn't work, even though it doesn't cause any errors to appear in the log:

Code: Select all

[event]
  name="turn 1"
  
  [lua]
    code=<<
      local t1 = 1
      local t2 = 2
      local t3 = 3
      helper.set_variable_array("target", { t=t1, t=t2, t=t3 })
    >>
  [/lua]
  [message]
    message="$target[1].t"
  [/message]
[/event]
In this case I would expect the WML array to look like this:

Code: Select all

[set_variables]
  name=target
  [value]
    t=1
  [/value]
  [value]
    t=2
  [/value]
  [value]
    t=3
  [/value]
[/set_variables]
but instead the message is still blank, and there's also no "target" WML variable at all when looking at it through :inspect.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Trouble with helper.set_variable_array example on wiki

Post by gfgtdf »

Pentarctagon wrote: but IIRC Wesnoth still allows doing something like this(though it warns about it in the log):

Code: Select all

{VARIABLE target[0] 1}
quite possible that support for this was removed in wesnoth 1.13 . But even if it wasn't evenrythign that generated wanrings is bad code and hsoul be fixed.
Pentarctagon wrote:
Either way though, the following still doesn't work, even though it doesn't cause any errors to appear in the log:
but instead the message is still blank, and there's also no "target" WML variable at all when looking at it through :inspect.
yes this code { t=t1, t=t2, t=t3 } is basically equivalent to { t=t3 } since you are just overwriting the same 't' value of that lua table 3 times.

helper.set_variable_array expects an array (that is: a table with ascenting integer keys) of wml tables, so the correct code is helper.set_variable_array("target", { {t=t1}, {t=t2}, {t=t3} }), where { {t=t1}, {t=t2}, {t=t3} } is an array and {t=t3} etc. are wml tables each.
Also note that { t=t1, t=t2, t=t3 } has no integer keyssa at all (the only key is t) so the array is empty from helper.set_variable_arrays point of view
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Trouble with helper.set_variable_array example on wiki

Post by Pentarctagon »

Ah, alright then, thanks.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
Post Reply