Modify Unit from existing unit's modifications

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
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Modify Unit from existing unit's modifications

Post by Ghite »

I have a unit with unknown modifications [store_unit]ed.

I would like to apply said modifications to a different unit.
- modifications=unit.modifications does not work ("Unknown attribute 'modifications' discarded.")

How?
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Modify Unit from existing unit's modifications

Post by 8680 »

I think it might be…

Code: Select all

[set_variables]
    name=other_stored_unit.modifications
    to_variable=unit.modifications
    mode=append ## Or mode=replace, if that’s what you want.
[/set_variables]
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Re: Modify Unit from existing unit's modifications

Post by Ghite »

It seems to work. Thanks.
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Re: Modify Unit from existing unit's modifications

Post by Ghite »

I was mistaken.
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Re: Modify Unit from existing unit's modifications

Post by Ghite »

I know it is being activated, as the code beside it works, which is a change of size.
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Modify Unit from existing unit's modifications

Post by Dugi »

I think that mode=append will not do the trick, it will place it into unit[1].modifications[0] instead of unit[0].modifications[1] you probably intended, neither of those will work because only the second unit variable isn't valid and will not be unstored and second modification array in unit will be discarded when unstored. You'll have to use mode=replace.

However, only some very specific changes to [modifications] will have effect right after unstoring, you'll have to reload its properties. The simplest way to do that is to recreate the unit instead of unstoring it (it will not overwrite the old unit, so make sure to kill it when storing it):

Code: Select all

	[unit]   #This should preserve all the unit's properties that should be preserved
		side=$unit.side
		x=$unit.x
		y=$unit.y
		experience=$unit.experience
		canrecruit=$unit.canrecruit
		variation=$unit.variation
		type=$unit.type
		id=$unit.id
		moves=$unit.moves
		hitpoints=$unit.hitpoints
		gender=$unit.gender
		name=$unit.name
		facing=$unit.facing
		extra_recruit=$unit.extra_recruit
		underlying_id=$unit.underlying_id
		unrenamable=$unit.unrenamable
		overlays=$unit.overlays
		animate=no
		[insert_tag]	
			name=status
			variable=unit.status
		[/insert_tag]
		[insert_tag]	
			name=modifications
			variable=other_stored_unit.modifications  #This also does what the set_variables was supposed to do
		[/insert_tag]
		[insert_tag]
			name=variables
			variable=unit.variables
		[/insert_tag]
	[/unit]
User avatar
trewe
Translator
Posts: 122
Joined: December 24th, 2012, 5:37 pm
Location: Portugal
Contact:

Re: Modify Unit from existing unit's modifications

Post by trewe »

Instead of mode=append you have to use mode=merge.

Appending would create an array of modifications for the unit, being unit.modifications[0] the original and unit.modifications[1] the inherit. But the engine reads only the first.

Of course, special care have be done, not all modifications can be merged.
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Modify Unit from existing unit's modifications

Post by Sapient »

No, mode=append is correct. The problem is because, you are using a hybrid approach which is neither an object nor a direct stat modification. These are the two standard ways of modifying units.

So, for example, either I give a unit an object which increases its max_hitpoints, or I directly edit unit.max_hitpoints and then unstore the unit.

It is possible to use this hybrid approach. However, unless the relevant attributes of the unit are cleared, they will not be recalculated.* To continue my example, I would append a modification that increases max_hitpoints, clear the unit.max_hitpoints (using clear_variable), and then unstore the unit.

Another thing you should keep in mind is that [modifications] come in three varieties: [trait], [object], and [advance]. So you need to specify which ones you want. If the only ones you want are objects, then it may be simplest to add their effects to the next unit by using objects as well.

- - - - -

* Dugi showed an alternate approach. While I suggest clearing from the unit variable those things which should be recalculated, he instead starts with a blank new unit and specifies everything which should not be recalculated. Personally I prefer the former approach because it seems more maintainable and more to the point.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
Post Reply