Store unit problem

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
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Store unit problem

Post by Gwledig »

Hi I am trying to remove a trait from filtered units in an event, the only snag is my [store] or [unstore] wml seems to be wrong, I am trying to save the matching units into a variable (remtrait) then use this to [unstore] ie save the matching units with the trait removed...

I get an error in console saying the variable remtrait doesn't exist...

Code: Select all

#remove the trait matching the variable unitid on all units
[store_unit]
	[filter]
		side=$side_number
		[filter_wml]
   		 [modifications]
       		 [trait]
                  id=$unitid 
     		  [/trait]
   		 [/modifications]
		[/filter_wml]
	[/filter]
	variable=remtrait
	kill=yes
[/store_unit]
{CLEAR_VARIABLE remtrait.modifications.trait} 
[unstore_unit]
variable=remtrait
[/unstore_unit]
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
User avatar
octalot
General Code Maintainer
Posts: 783
Joined: July 17th, 2010, 7:40 pm
Location: Austria

Re: Store unit problem

Post by octalot »

Is the error that it doesn't contain unit data? If the filter doesn't match any units then I'd expect the "doesn't contain unit data" error.

If the filter is correct, then I'd expect more than one unit to be stored in remtrait. That's not a problem, as it's an array, but it means you should have a [for] or [foreach] loop around both the CLEAR_VARIABLE and the unstore; this has the side-effect of handling zero units being in the array for you. There's a good example in data/campaigns/Dead_Water/scenarios/05_Tirigaz.cfg
User avatar
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Re: Store unit problem

Post by Gwledig »

Thanks octalot
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
User avatar
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Re: Store unit problem

Post by Gwledig »

The below seems to work, as long as kill unit is turned off.. otherwise the last unit spawned is removed off map and not re-built, I wonder if this is because I don't use standard types but use type of fog clearer with original unit settings... I had to add a while loop otherwise not all units with matching filter (trait with ID of spawning unit) were affected... Hopefully the below is OK now..

Code: Select all

#remove the trait matching the variable unitid on all units
[while]
[have_unit]
	side=$side_number
	[filter_wml]
	[modifications]
	[trait]
	id=$unitid 
	[/trait]
	[/modifications]
	[/filter_wml]
[/have_unit]
[do]
[store_unit]
	[filter]
		side=$side_number
		[filter_wml]
   		 [modifications]
       		 [trait]
                  id=$unitid 
     		  [/trait]
   		 [/modifications]
		[/filter_wml]
	[/filter]
variable=remtrait
#kill=yes
[/store_unit]
[foreach]
array=remtrait
[do]
[clear_variable]
name=remtrait.modifications.trait
[/clear_variable]
[unstore_unit]
variable=remtrait
[/unstore_unit]
[/do]
[/foreach]
[/do]
[/while]
{CLEAR_VARIABLE unitid}
[/event]
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
User avatar
octalot
General Code Maintainer
Posts: 783
Joined: July 17th, 2010, 7:40 pm
Location: Austria

Re: Store unit problem

Post by octalot »

Inside your [foreach] loop, you should be accessing this_item - using remtrait there will only access the first item of the array.
User avatar
beetlenaut
Developer
Posts: 2814
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Store unit problem

Post by beetlenaut »

Gwledig wrote: August 22nd, 2019, 10:20 pm otherwise the last unit spawned is removed off map and not re-built
No, your units are not getting unstored properly because you are using an array with [unstore_unit]. The wiki is clear about not doing that. You should be using "this_item" inside the foreach loop to access each single item in the array. This affects the clear_variable as well, so that is probably not working correctly either. Are you not using :inspect to make sure that your units are getting modified properly?

I did look at your code this time, but it was very hard to understand without proper indenting. I had to run it through wmlindent to be able to read it easily. I'm sure you know how to indent code by now, so do it next time. It was also confusing that you have a variable called unitid that is the id of a trait, not a unit.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Re: Store unit problem

Post by Gwledig »

Well I messed around with this, removing the surrounding [while] and tried to get this working with this_item, I have searched across the wiki and can't find any examples of this, so I have been using trial and error, at the moment nothing is happening and units just get removed from the map...

I can't see anything odd about the units modified in the [while] version, despite the kill= being disabled, the trait is removed from all matching units, I'm assuming the unit is overwritten or the specific trait is simply removed leaving the original unit details intact, the overlay and all details, name, description etc. are retained as far as I can see, at the moment the [while] approach is the only one resulting in no log errors and the behavior I'm trying to reach.

This is where I am up to having removed the [while] condition & relying on foreach with this_unit-
if I remove array= this result in an error saying foreach is missing array=

Code: Select all

#remove the trait matching the variable unitid on all units

[store_unit]
	[filter]
		side=$side_number
		[filter_wml]
   		 [modifications]
       		 [trait]
                  id=$unitid 
     		  [/trait]
   		 [/modifications]
		[/filter_wml]
	[/filter]
variable=this_item
kill=yes
[/store_unit]
[foreach]
	array=this_item
	[do]
		[clear_variable]
			name=remtrait.modifications.trait
		[/clear_variable]
		[unstore_unit]
			variable=this_item
		[/unstore_unit]
	[/do]
[/foreach]

{CLEAR_VARIABLE unitid}
[/event]
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
User avatar
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Re: Store unit problem

Post by Gwledig »

Yep the units are updated as expected using [while] the stats and everything look fine, the trait is removed as expected e.g. in the example below the firing unit moved, resulting in the trait (containing the id of the firing unit) being removed, but shots (units) fired/spawned before moving again have the trait (id of spawning unit):

Code: Select all

(27,10) 
id="607313218" (Fog Clearer)
L1; 0/1 XP; 1/1 HP

(26,8) 
id="118196648" (Fog Clearer)
L1; 0/1 XP; 1/1 HP
	Dwarvish Thunderer-1

(28,8) 
id="61465358" (Fog Clearer)
L1; 0/1 XP; 1/1 HP
	Dwarvish Thunderer-1
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
User avatar
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Re: Store unit problem

Post by Gwledig »

OkI have realised that foreach is redundant, because I am doing a [while] condition which loops through each matching unit and then removes the trait, the removal of kill seems necessary, it seems impossible to remove the unit and rebuild the projectile units, I don't know why this happens, but could be perhaps because of the unusual type (fog clearer?), I also have to retain [unstore] unit for this to work, so it must be appending (ie removing the trait) on the unit being looped..

Code: Select all

#remove the trait matching the variable unitid on all units
[while]
[have_unit]
	side=$side_number
	[filter_wml]
	[modifications]
	[trait]
	id=$unitid 
	[/trait]
	[/modifications]
	[/filter_wml]
[/have_unit]
[do]
[store_unit]
	[filter]
		side=$side_number
		[filter_wml]
   		 [modifications]
       		 [trait]
                  id=$unitid 
     		  [/trait]
   		 [/modifications]
		[/filter_wml]
	[/filter]
variable=remtrait
#kill=yes
[/store_unit]

[clear_variable]
name=remtrait.modifications.trait
[/clear_variable]
[unstore_unit]
variable=remtrait
[/unstore_unit]

[/do]
[/while]
{CLEAR_VARIABLE unitid}
[/event]
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
User avatar
octalot
General Code Maintainer
Posts: 783
Joined: July 17th, 2010, 7:40 pm
Location: Austria

Re: Store unit problem

Post by octalot »

Gwledig wrote: August 23rd, 2019, 10:51 am

Code: Select all

[foreach]
	array=this_item
	[do]
		[clear_variable]
			name=remtrait.modifications.trait
		[/clear_variable]
		[unstore_unit]
			variable=this_item
		[/unstore_unit]
	[/do]
[/foreach]
The clear_variable also needs to use this_item, otherwise each go through the loop will remove the trait from the same unit. And the array= should still be remtrait. There's a good example in data/campaigns/Dead_Water/scenarios/05_Tirigaz.cfg.

As beetlenaut requested, please sort out your indentation.
User avatar
Gwledig
Posts: 568
Joined: March 30th, 2009, 5:10 pm
Location: UK

Re: Store unit problem

Post by Gwledig »

Thanks Bettlenaught and Octalot, I have gone for the [while] loop removing foreach, I will get around to using wmlindend for all WML/mods shortly.
Maintainer of Conquest (Original Gameplay), Conquest+, Conquest+ Space/Ranged, Chaoz Battle of the Wizards, Lazersquad (squad game), WesCraft (building MP game)
Post Reply