Problem with FOREACH and/or [unit] tag in event.

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
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Problem with FOREACH and/or [unit] tag in event.

Post by Nyanyanyan »

Hi again. I'm trying to make an ability that spawns a goblin every time a turn starts while the unit with the ability is standing next to another goblin.
It works fine as long as there is only one unit with said ability, but if there are multiple units with that ability, all the goblins will spawn from ONE of the units with the ability, not not one from EACH. This is the code I came up with:

Code: Select all

#define ABILITY_SPAWNGOBLIN
[dummy]
	id=spawngoblin
	name= _ "spawngoblin"
	name_inactive= _ "spawngoblin"
	description= _ "Spawns goblins."  
[/dummy]
[/abilities]
[event]
name=side turn
first_time_only=no
id=getturneventspawn
[store_unit]
    [filter]
        ability=spawngoblin
    [/filter]
    variable=spawngoblinunit
[/store_unit]
[/event]
[event] 
name=side $spawngoblinunit.side turn
first_time_only=no 
[store_unit]
    [filter]
        ability=spawngoblin
	    [filter_adjacent]
            type=type=Direwolf Rider,Goblin Impaler,Goblin Knight,Goblin Pillager,Goblin Rouser,Goblin Spearman,Wolf Rider,Goblin King,Goblin Emperor
		[/filter_adjacent]
    [/filter]
    variable=nyaa
[/store_unit]
	[unit]
		type=Goblin Spearman
		x=$nyaa.x
		y=$nyaa.y
		side=$nyaa.side
	[/unit]
[/event]
[+abilities]
#enddef
Now I tried to remedy this by using the FOREACH macro to go through them one by one and determine whether or not they should spawn a goblin and then do it, but now every unit with the ability spawns the number of goblins that there are units with the ability in total on the field. (So with 2 Spawners I would have 4 Goblins and with 3 spawners, I would have 9 Goblins in total.)
This is what I used:

Code: Select all

#define ABILITY_SPAWNGOBLIN
[dummy]
	id=spawngoblin
	name= _ "spawngoblin"
	name_inactive= _ "spawngoblin"
	description= _ "Spawns goblins."  
[/dummy]
[/abilities]
[event]
name=side turn
first_time_only=no
id=getturneventspawn
[store_unit]
    [filter]
        ability=spawngoblin
    [/filter]
    variable=spawngoblinunit
[/store_unit]
[/event]

[event] 
name=side $spawngoblinunit.side turn
first_time_only=no 
	{FOREACH spawngoblinunit i}
[store_unit]
    [filter]
        x=$spawngoblinunit[$i].x
		y=$spawngoblinunit[$i].y
	    [filter_adjacent]
            type=type=Direwolf Rider,Goblin Impaler,Goblin Knight,Goblin Pillager,Goblin Rouser,Goblin Spearman,Wolf Rider,Goblin King,Goblin Emperor
		[/filter_adjacent]
    [/filter]
    variable=nyaa[$i]
[/store_unit]
		
	[unit]
		type=Goblin Spearman
		x=$nyaa[$i].x
		y=$nyaa[$i].y
		side=$nyaa[$i].side
	[/unit]
	{NEXT i}
[/event]

[+abilities]
#enddef
Now my question is: Why does this happen? The way I read it the latter code should go through the spawners one by one and spawn one goblin for each at the location of that spawner.
What am I missing?
Thanks in advance for the help.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Problem with FOREACH and/or [unit] tag in event.

Post by Ravana »

You only need 1 event.

"type=type="

[store_unit] should be done once only.

Your event has no id, so it can fire multiple times at once.
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Problem with FOREACH and/or [unit] tag in event.

Post by Pentarctagon »

For:
Ravana wrote: October 25th, 2018, 5:20 am Your event has no id, so it can fire multiple times at once.
in particular, additionally see: https://wiki.wesnoth.org/EventWML#A_Trap_for_the_Unwary
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Nyanyanyan
Posts: 73
Joined: May 11th, 2018, 10:40 pm

Re: Problem with FOREACH and/or [unit] tag in event.

Post by Nyanyanyan »

Thanks, that did the trick.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.
Post Reply