unit variables not immediately showing [solved]

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
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

unit variables not immediately showing [solved]

Post by The_Gnat »

I have twice encountered i assign a unit a variable (using [modify_unit] and [variables]) and the variable does not appear until the next turn or until i assign it again?????

This may be something standard but it seems like i am doing something wrong, here is an example:

Code: Select all

			[modify_unit]
			     [filter]
				id=$second_unit.id
			     [/filter]
			     [variables]
				 defense=50
			     [/variables]
			[/modify_unit]
		        {MESSAGE_N (Hey look, his defense is $second_unit.variables.defense)}
{MESSAGE_N} = a message by the narrator

Upon attacking i modify the unit to have a new variable and then print the variable. The first time the narrator says "Hey look, his defense is " and the variables value is obviously null because it does not print anything. The second time the unit attacks him However it prints "Hey look, his defense is 50".
Another Example
This might or might not be related:

Code: Select all

    # THERE IS A MESSAGE ONE OPTION LEADS TO THIS COMMAND
		    [command]
			
			[modify_unit]
			     [filter]
				id=$unit.id
			     [/filter]
			     [variables]
				 defense_state=defend
			     [/variables]
			[/modify_unit]

			{GIVE_DEFENDER_BLANK_DEFENSE_ANIM}

		    [/command]

#define GIVE_DEFENDER_BLANK_DEFENSE_ANIM
[if]
    [variable]
	name=unit.variables.defense_state
	equals=defend
    [/variable]
 [then]
    {MESSAGE_N (applying object)}
 [/then]
[/if]
The first message shows up and when the option is choosen nothing happens... but if i rerun the event (i have the event on a right-click-menu option) then it shows the message from the narrator. This seems to be a similar problem: the variable has no value (and therefore does not equal "defend" until the second time it is assigned.
Thank you for your help, this is confusing, maybe i should not be using [modify_unit] for these circumstances.
Last edited by The_Gnat on January 9th, 2017, 2:10 am, edited 1 time in total.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: unit variables not immediately showing

Post by zookeeper »

You're a modifying a unit, and then you're printing the value of an unrelated variable. Modifying a unit doesn't change anything except the actual real unit; the contents of second_unit are still the same.

When you refer to an automatically stored unit variable/container like $second_unit, what happens is that the unit as it exists at that moment gets stored into that variable/container, and that's it. Nothing further automatically happens to it. If you modify the corresponding real unit, that won't be reflected in the variable. Similarly, you can do whatever you want to the variable, but unless you actually use [unstore_unit], none of that will ever be reflected in the corresponding real unit. Variables like $unit and $second_unit are not "pointers" to the real unit, they're simply a copy of the unit data for your convenience, so that you don't need to manually [store_unit] the unit every time you want to access their contents.
User avatar
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: unit variables not immediately showing

Post by The_Gnat »

zookeeper wrote:You're a modifying a unit, and then you're printing the value of an unrelated variable. Modifying a unit doesn't change anything except the actual real unit; the contents of second_unit are still the same.
Ahh... i understand i would have to restore the variable in order to see it because the stored unit second_unit has not been modified yet. :D
zookeeper wrote:When you refer to an automatically stored unit variable/container like $second_unit, what happens is that the unit as it exists at that moment gets stored into that variable/container, and that's it. Nothing further automatically happens to it. If you modify the corresponding real unit, that won't be reflected in the variable. Similarly, you can do whatever you want to the variable, but unless you actually use [unstore_unit], none of that will ever be reflected in the corresponding real unit. Variables like $unit and $second_unit are not "pointers" to the real unit, they're simply a copy of the unit data for your convenience, so that you don't need to manually [store_unit] the unit every time you want to access their contents.
Thank you very much, :) i knew that but somehow overlooked it ;)

That solves my problem, [modify_unit] in and $second_unit are not related and that is the problem i was having! Thank you! :D

EDIT: what i ended up doing is restoring second_unit and unit once i had made a modification
Post Reply