Quantcast
Channel: The Battle for Wesnoth Forums
Viewing all articles
Browse latest Browse all 2284

Lua Labs • Another basic lua query

$
0
0
Hi,
Still playing with the forest animals micro ai and am rather lost after some progress.
The following test setup was merely to check I was capturing correct data and it sent me down a rabbit hole.

Yes, it is basically [store_unit] and I have a cutdown version that works. :shock:
Here though I have a couple of issues.

First, the code:
WML:

Code:

  [event]    name=side 1 turn 1    {GENERIC_UNIT 1 Mage 4 4}    [ftest]      [filter]        side=1        canrecruit=yes      [/filter]    [/ftest]
lua:

Code:

--- Parse filter and return units in array "units"function wesnoth.wml_actions.ftest(cfg)   wml.array_variables.ftest = {}  local units = {}  local unitsm = {}  wesnoth.interface.add_chat_message(string.format("Length 'units' is %d.", #units))  local units = wesnoth.units.find_on_map(cfg.filter)  local unitsm = wesnoth.units.find(cfg.filter)  wesnoth.interface.add_chat_message(string.format("Length 'units' is %d and 'unitsm' is %d.", #units, #unitsm))  wesnoth.interface.add_chat_message(string.format("'units[1,2,3].id'  = %s,%s,%s.", units[1].id, units[2].id, units[3].id))  wesnoth.interface.add_chat_message(string.format("'unitsm[1,2,3].id' = %s,%s,%s.", unitsm[1].id, unitsm[2].id, unitsm[3].id))--  wml.array_variables.ftest = unitsend
Using two types of units.find just to make this code use the same logic as the working code example below.

Output:
luamessage2.jpg
- - -

Issue #1
The [filter] contents are ignored?
(Every unit on the map is returned.)
I've read various code examples but I've struggled to follow the logic as it flows through various files.
When an entire tag contains just a filter things work, as in:

Code:

--- If unit(s) found, assumes all are validfunction wesnoth.wml_actions.get_uxyid(cfg)  local guxyid = wesnoth.units.find(cfg)  local gu = {}  wml.array_variables.gu = {}  if guxyid then    for index, unit in ipairs(guxyid) do      if unit.valid == "map" then        gu[index] = {x=unit.x,y=unit.y,id=unit.id}      elseif unit.valid == "recall" then        gu[index] = {x="recall",y="recall",id=unit.id}      end      wml.array_variables.gu = gu    end  endend
So I'm guessing there's something wrong with my using cfg.filter?

Issue #2
If I enable the WML array assignment wml.array_variables.ftest = units I get an error:
luamessage.jpg
That last line assigning a WML variable is disabled, it complains and I'm guessing the lua table isn't compatible with this assignment.
(Especially as in the working code I built a simple array from scratch.)

I have various speculations on what might be wrong but I'd rather ask someone who actually knows something about lua. :)

I did look for the code for [store_unit] but couldn't find it - if I've missed it, please let me know where it's located.

Any pointers on either or both these issues would be greatly appreciated.

Cheers!
-- Spannerbag

Statistics: Posted by Spannerbag — Today, 11:57 am



Viewing all articles
Browse latest Browse all 2284

Trending Articles