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

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

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

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

Post by Nyanyanyan » October 25th, 2018, 3:39 am

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.

User avatar
Ravana
Moderator
Posts: 1887
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

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

Post by Ravana » October 25th, 2018, 5:20 am

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
Forum Administrator
Posts: 3623
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Post by Pentarctagon » October 25th, 2018, 6:44 am

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

Nyanyanyan
Posts: 10
Joined: May 11th, 2018, 10:40 pm

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

Post by Nyanyanyan » October 25th, 2018, 5:33 pm

Thanks, that did the trick.

Post Reply