[closed] remove unit from array
Moderator: Forum Moderators
- ZombieKnight
- Posts: 282
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
[closed] remove unit from array
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?
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...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
Re: remove unit from array
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.
And your version doesnt work because you havent read wml.remove_children documentation.
- ZombieKnight
- Posts: 282
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: remove unit from array
Why does return nil?
Code: Select all
for i, u in ipairs(wml.variables["side_7_retreat_micro_ai"]) do
print(u.id)
end
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
Re: remove unit from array
Because u.id is empty.
- ZombieKnight
- Posts: 282
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: remove unit from array
Solved with for loop.
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
- Spannerbag
- Posts: 562
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: remove unit from array
Just curious, but why lua?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"...
A [for] loop would do that - and for me anyway, it's a lot easier to debug!
Cheers!
-- Spannerbag
- ZombieKnight
- Posts: 282
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: remove unit from array
Jup, [foreach] would work even better, but I want to learn lua better...
Thanks!
Thanks!
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
-
- Posts: 1308
- Joined: August 26th, 2018, 11:46 pm
- Location: A country place, far outside the Wire
Re: remove unit from array
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.ZombieKnight wrote: ↑April 15th, 2024, 6:41 am Jup, [foreach] would work even better, but I want to learn lua better...
Thanks!
Speak softly, and carry Doombringer.
- ZombieKnight
- Posts: 282
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: [closed] remove unit from array
Oh, true
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
- Spannerbag
- Posts: 562
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: remove unit from array
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...
Good luck.
Cheers!
-- Spannerbag
- Celtic_Minstrel
- Developer
- Posts: 2290
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: remove unit from array
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 provideswhite_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.
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.
- ZombieKnight
- Posts: 282
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: [closed] remove unit from array
Jup it doesnt.
Jup I've got to to use reverse loops when removing elements.
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...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills