counting unit_type

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
bejacomune
Posts: 11
Joined: February 20th, 2023, 8:38 am

counting unit_type

Post by bejacomune »

Hello. I have a problem with the following code.

Code: Select all

	[if]
		[have_unit]
			type=Siege_2
		[/have_unit]
		[then]
			[store_unit]
				[filter]
					type=Siege_2
				[/filter]
				variable=Count_Siege_2
				kill=no
			[/store_unit]
			[foreach]
				array=Count_Siege_2
				[do]
					{VARIABLE_OP counting add 1}   # counting is assigned to 0 in the prestart
				[/do]
			[/foreach]
		[/then]
	[/if]
	
It sometimes generate the following error

Code: Select all

20230410 19:16:47 error scripting/lua: lua/core/wml.lua:232: bad argument #2 to 'set_variable_local' (WML table or scalar expected, got table)
stack traceback:
	[C]: in function 'wml.set_variable'
	lua/core/wml.lua:232: in metamethod 'newindex'
	lua/wml-utils.lua:47: in field 'write'
	lua/wml-tags.lua:406: in local 'cmd'
	lua/wml-utils.lua:144: in field 'handle_event_commands'
	lua/wml-flow.lua:19: in local 'cmd'
	lua/wml-utils.lua:144: in field 'handle_event_commands'
	lua/wml-flow.lua:5: in function <lua/wml-flow.lua:4>
the purpose of the code is to count how many units of type Siege_2 a side have so if there is another way, tell me about
User avatar
Toranks
Translator
Posts: 168
Joined: October 21st, 2022, 8:59 pm
Location: Sevilla
Contact:

Re: counting unit_type

Post by Toranks »

Try this instead of the [foreach] and without needing to define counting first:

Code: Select all

[set_variable]
  name=counting
  value=$Count_Siege_2.length
[/set_variable]
Last edited by Toranks on April 11th, 2023, 3:52 am, edited 1 time in total.
User avatar
Celtic_Minstrel
Developer
Posts: 2236
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: counting unit_type

Post by Celtic_Minstrel »

Other than the typo, Toranks's code will do what you want.

That aside, this seems like a potential bug in [set_variable]
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply