[closed] remove unit from array

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

Moderator: Forum Moderators

Post Reply
User avatar
ZombieKnight
Posts: 180
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

[closed] remove unit from array

Post by ZombieKnight »

Hi,
I'm trying to remove unit with id=$unit.id from array of units "side_7_retreat_micro_ai"
...
Any idea why my code isn't working?

Code: Select all

[lua]
                code=<<
local u_table = wml.variables["side_7_retreat_micro_ai"]
local u_id = wml.find_child(u_table, {id = wml.variables["unit.id"]})
wml.remove_children(wml.variables["side_7_retreat_micro_ai"], u_id)
>>
            [/lua]
Last edited by ZombieKnight on April 15th, 2024, 6:41 am, edited 1 time in total.
I had saurian in profile before, but I've merged my discord profile with forum one...
User avatar
Ravana
Forum Moderator
Posts: 3018
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: remove unit from array

Post by Ravana »

wml.find_child also gives you index, if u_table only contains tag of that name just use table.remove with that index.

And your version doesnt work because you havent read wml.remove_children documentation.
User avatar
ZombieKnight
Posts: 180
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: remove unit from array

Post by ZombieKnight »

Why does

Code: Select all

for i, u in ipairs(wml.variables["side_7_retreat_micro_ai"]) do
print(u.id)
end
return nil?
I had saurian in profile before, but I've merged my discord profile with forum one...
User avatar
Ravana
Forum Moderator
Posts: 3018
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: remove unit from array

Post by Ravana »

Because u.id is empty.
User avatar
ZombieKnight
Posts: 180
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: remove unit from array

Post by ZombieKnight »

Solved with for loop.
I had saurian in profile before, but I've merged my discord profile with forum one...
User avatar
Spannerbag
Posts: 537
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: remove unit from array

Post by Spannerbag »

ZombieKnight wrote: April 13th, 2024, 7:52 am Hi,
I'm trying to remove unit with id=$unit.id from array of units "side_7_retreat_micro_ai"...
Just curious, but why lua?
A [for] loop would do that - and for me anyway, it's a lot easier to debug!

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, 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
ZombieKnight
Posts: 180
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: remove unit from array

Post by ZombieKnight »

Jup, [foreach] would work even better, but I want to learn lua better...
Thanks!
I had saurian in profile before, but I've merged my discord profile with forum one...
white_haired_uncle
Posts: 1226
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: remove unit from array

Post by white_haired_uncle »

ZombieKnight wrote: April 15th, 2024, 6:41 am Jup, [foreach] would work even better, but I want to learn lua better...
Thanks!
foreach doesn't play well with removing elements of the array it is iterating over. I don't try to remember the rules, I just use for if I'm going to remove anything.
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 180
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: [closed] remove unit from array

Post by ZombieKnight »

Oh, true
I had saurian in profile before, but I've merged my discord profile with forum one...
User avatar
Spannerbag
Posts: 537
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: remove unit from array

Post by Spannerbag »

ZombieKnight wrote: April 15th, 2024, 6:41 am ...I want to learn lua better...
Heh, I am currently going through the exactly same process starting from a position of knowing zero lua.
Note that the wiki isn't quite right so don't make my mistake of spending *hours* debugging something that will never work... :augh:

Good luck.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, 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: 2241
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: remove unit from array

Post by Celtic_Minstrel »

white_haired_uncle wrote: April 15th, 2024, 6:58 am foreach doesn't play well with removing elements of the array it is iterating over. I don't try to remember the rules, I just use for if I'm going to remove anything.
You need to be careful removing elements with for, too – you could end up skipping over the next element when you remove one. The key is to iterate in reverse order. This is a big reason why the for tag provides reverse=yes in the first place when using array=.

That theoretically applies to foreach too. However, I don't think the foreach tag supports removing elements at all.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 180
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: [closed] remove unit from array

Post by ZombieKnight »

Jup it doesnt.
Jup I've got to to use reverse loops when removing elements.
I had saurian in profile before, but I've merged my discord profile with forum one...
Post Reply