Problem iterating over units
Moderator: Forum Moderators
Forum rules
- Please use [code] BBCode tags in your posts for embedding WML snippets.
- To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Problem iterating over units
I'm trying to make a new ability: "miner", basically if the unit is in a mountain or hill it produces gold (I plan to make the ability more complex), but I am having two problems.
In order to make this ability I have to make two things:
1) Make a dummy ability:
2) And make an event every turn which: a) Get all the units from the map which have the "miner" ability, are from your side and are in a mountain or hill, b) Iterate over them to give you gold.
But I am having two problems. The minor problem is that I don't know how to check if the [dummyminer] is active or not, so I have to use [filter_location] again . The mayor problem is that FILTER is giving me too much units or the FOREACH is broken.... I test this in a map with only 3 units (my leader, enemy leader and my unit with {ABILITY_MINE 4}), but instead of get 4 gold in my turn, the game freeze about 10 seconds and it give me around 250.000 gold, so it's iterating over +50.000 units... is my filter wrong?
In order to make this ability I have to make two things:
1) Make a dummy ability:
Code: Select all
#define ABILITY_MINE Gold
[dummyminer]
value={Gold}
id=miner
name= _ "miner +{Gold}"
female_name= _ "female^miner + {Gold}"
description= _ "This unit is able to mine {Gold} gold if at the end of the turn it didn't use its attack from Rubbles (50%), Stones (25%), Hills (100%), Mountains (150%), Cave Walls (75%), Cave Villages (125%) and Dwarven Villages (75%). Each terrain has it own cold income multiplier."
[filter]
[filter_location]
terrain=Mm,Md,Ms,Hh,Hhd,Hd,Ha
[/filter_location]
[/filter]
[/dummyminer]
#enddef
Code: Select all
#define MINER_EVENT
[event]
name=side turn
first_time_only=no
[store_unit]
[filter]
side=$side_number
ability=miner
[filter_location]
terrain=Mm,Md,Ms,Hh,Hhd,Hd,Ha
[/filter_location]
[/filter]
variable=units
[/store_unit]
{FOREACH units index}
[gold]
amount=$units[$index].abilities.dummyminer.value
side=$side_number
[/gold]
{NEXT unit}
[/event]
#enddef
Last edited by Enderlook on January 24th, 2018, 7:40 pm, edited 2 times in total.
Re: Problem iterating over units
You need to change index in each loop iteration, instead of unit.
Re: Problem iterating over units
Oh thanks, I hadn't notice that silly error . But the minor problem is still there, Does someone know how to check if my [dummyminer] (or miner) is active or inactive.
Re: Problem iterating over units
Whether ability is active is basically irrelevant - only useful if you want to show different ability name/description then.
Re: Problem iterating over units
Ok, thanks. Is possible to have several "actives"? I mean, diferent descriptions (e.g: if it's in a mountain, if it's in a hill, if it's...)
Re: Problem iterating over units
Only by modifying user interface with Lua. Better create different ability for each description.
Re: Problem iterating over units
Ok, Thanks!
- Celtic_Minstrel
- Developer
- Posts: 2211
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Problem iterating over units
In 1.13 you should be able to check if the ability is active with
(Also, personally I'd change "dummyminer" to just "miner" everywhere, but that doesn't really matter.)
ability_type_active=dummyminer
in your unit filter.(Also, personally I'd change "dummyminer" to just "miner" everywhere, but that doesn't really matter.)