Yet another basic lua query
Moderator: Forum Moderators
- Spannerbag
- Posts: 952
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Yet another basic lua query
Hi,
getting lost in the API.
I have this code (which works fine):
I'd like to also exclude a melee attack with a custom special:
However this special might not be the only attack special, so is there an elegant way to trap this and exclude it same way as "slam" and "petrifyturn"(s)?
Can't use attack name as this special is appended to an existing attack and not all units with this attack will have this custom special.
I'm thinking something like
Would some sort of weapon filter be a better way to go?
Originally I'd thought there'd only be the one exclusion ("slam") but there are a few now...
Any help gratefully received!
Cheers!
getting lost in the API.
I have this code (which works fine):
Code: Select all
function wesnoth.wml_conditionals.ret_check(cfg)
local unit = wesnoth.units.find_on_map(cfg)[1]
local attm = 0 -- Total melee attacks
local attr = 0 -- Total ranged attacks
if not unit then return false end -- No filter match
for i=1,#unit.attacks do
if unit.attacks[i].range=="melee" and unit.attacks[i].name ~= "slam" and not string.match(unit.attacks[i].name, "^petrifyturn") then attm = attm +1 end
if unit.attacks[i].range=="ranged" then attr = attr +1 end
end
...Code: Select all
[dummy_stunmove]
id=stunmove
name= _ "stunmove"
female_name= _ "female^stunmove"
...Can't use attack name as this special is appended to an existing attack and not all units with this attack will have this custom special.
I'm thinking something like
and not unit.attacks[i].specials.dummy_stunmove.id (assuming nonexistent value=false) but really not sure of syntax here.Would some sort of weapon filter be a better way to go?
Originally I'd thought there'd only be the one exclusion ("slam") but there are a few now...
Any help gratefully received!
Cheers!
- Celtic_Minstrel
- Developer
- Posts: 2487
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Yet another basic lua query
That won't work (the specials are exposed to the Lua API, but as an array, not a mapping).Spannerbag wrote: July 28th, 2026, 5:11 pm I'm thinking something likeand not unit.attacks[i].specials.dummy_stunmove.id(assuming nonexistent value=false) but really not sure of syntax here.
I think something like this should work:
Code: Select all
if unit.attacks[i]:matches{special_type = 'dummy_stunmove'} then
Code: Select all
if unit.attacks[i]:matches{special_id = 'stunmove'} then
unit.attacks[i].specials for a match, but I think using matches would be easier.- Spannerbag
- Posts: 952
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Yet another basic lua query
Wow, very helpful, thanks.
So where I now have:
I just append something like:
So new statement is:
Many thanks again for your assistance, saved me ages of thrashing around!
Very much appreciated.
Cheers!
So where I now have:
Code: Select all
if unit.attacks[i].range=="melee"
and unit.attacks[i].name ~= "slam"
and not string.match(unit.attacks[i].name, "^petrifyturn")
then attm = attm +1 endunit.attacks[i]:matches{special_id = 'stunmove'}So new statement is:
Code: Select all
if unit.attacks[i].range=="melee"
and unit.attacks[i].name ~= "slam"
and not string.match(unit.attacks[i].name, "^petrifyturn")
and not unit.attacks[i]:matches{special_id = 'stunmove'} # Exclude "stunmove" WS
then attm = attm +1 endVery much appreciated.
Cheers!