Problem iterating over units

The place to post your WML questions and answers.

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.
Post Reply
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Problem iterating over units

Post by Enderlook »

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:

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
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.

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
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?
Last edited by Enderlook on January 24th, 2018, 7:40 pm, edited 2 times in total.
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Problem iterating over units

Post by Ravana »

You need to change index in each loop iteration, instead of unit.
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Re: Problem iterating over units

Post by Enderlook »

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.
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Problem iterating over units

Post by Ravana »

Whether ability is active is basically irrelevant - only useful if you want to show different ability name/description then.
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Re: Problem iterating over units

Post by Enderlook »

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...)
User avatar
Ravana
Forum Moderator
Posts: 3000
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Problem iterating over units

Post by Ravana »

Only by modifying user interface with Lua. Better create different ability for each description.
Enderlook
Posts: 38
Joined: January 22nd, 2018, 12:23 am

Re: Problem iterating over units

Post by Enderlook »

Ok, Thanks!
User avatar
Celtic_Minstrel
Developer
Posts: 2207
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Problem iterating over units

Post by Celtic_Minstrel »

In 1.13 you should be able to check if the ability is active with ability_type_active=dummyminer in your unit filter.

(Also, personally I'd change "dummyminer" to just "miner" everywhere, but that doesn't really matter.)
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply