What is the syntax for variables on units?

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
Rubric
Posts: 13
Joined: December 31st, 2012, 4:31 am

What is the syntax for variables on units?

Post by Rubric »

I have an enemy who is supposed to make a different comment every time he kills a unit. I'm using a variable to track his number of kills, and then a switch statement to pick a sentence for him to say, based on the number of kills.

I want the variable stored on the unit, because there will be multiple units with different things to say.

The code I'm using is below, but doesn't work. He always says the first sentence. The variable correctly increments to 1, but it never increments to 2, no matter how many kills he gets. So, it's not saving the variable, I guess.

Code: Select all

[event]
	name=die
	[filter]
		side=1
	[/filter]
	[filter_second]
		id=Wizard
	[/filter_second]

	first_time_only=no

	[set_variable]
		name=second_unit.variables.EnemiesKilled
		add=1
	[/set_variable]

		[switch]
			variable=second_unit.variables.EnemiesKilled
			[case]
			   value=1
		      [message]
			      speaker=second_unit
			      message=_"first sentence"
		      [/message]
			[/case]
			[case]
				value=2
				[message]
			      speaker=second_unit
			      message=_"second sentence"
				[/message]
			[/case]
			[case]
				value=3
				[message]
			      speaker=second_unit
			      message=_"third sentence"
				[/message]
			[/case]
		[/switch]
[/event]
Thanks for any help.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: What is the syntax for variables on units?

Post by zookeeper »

You never unstore second_unit, so incrementing the variable never gets reflected in the actual unit on the map.
Rubric
Posts: 13
Joined: December 31st, 2012, 4:31 am

Re: What is the syntax for variables on units?

Post by Rubric »

Thanks.

I added this after the [/set_variable] tag and now it works:

Code: Select all

[unstore_unit]
	variable=second_unit
[/unstore_unit]
I didn't realize you could unstore a unit without storing it first. For some reason, when I tried storing the unit, making the change, and then unstoring it, it didn't work. The variable didn't get incremented at all, so guy never spoke. Anyway, it works now, so thanks again.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: What is the syntax for variables on units?

Post by zookeeper »

Rubric wrote:I didn't realize you could unstore a unit without storing it first.
Well, you can't. It's just that unit and second_unit are automatically stored by the game (when applicable) for your convenience so that you don't have to do it manually everywhere.
Rubric
Posts: 13
Joined: December 31st, 2012, 4:31 am

Re: What is the syntax for variables on units?

Post by Rubric »

zookeeper wrote:Well, you can't. It's just that unit and second_unit are automatically stored by the game (when applicable) for your convenience so that you don't have to do it manually everywhere.
Ah okay, it's all clear to me now. Thanks again.


I macro'd the above code to make it easier to use for different characters. Here it is for anyone interested.

Code: Select all



#define BRAG_ON_KILL WHOBRAGS_ID BRAG1 BRAG2 BRAG3
#
#	Allow a character with id of WHOBRAGS_ID to say messages when he gets a kill.  The messages are BRAG1, BRAG2, etc. and
#		will be said in that order, one message per kill.
#
	[event]
		name=die
		[filter_second]
			id={WHOBRAGS_ID}
		[/filter_second]

		first_time_only=no

		[set_variable]
			name=second_unit.variables.EnemiesKilled
			add=1
		[/set_variable]
		[unstore_unit]
			variable=second_unit
		[/unstore_unit]

			[switch]
				variable=second_unit.variables.EnemiesKilled
				[case]
				value=1
		         [message]
		   	      speaker=second_unit
		   	      message={BRAG1}
		         [/message]
				[/case]
				[case]
					value=2
					[message]
			         speaker=second_unit
			         message={BRAG2}
					[/message]
				[/case]
				[case]
					value=3
					[message]
			         speaker=second_unit
			         message={BRAG3}
					[/message]
				[/case]

			[/switch]
	[/event]
#enddef

Then call it in the scenario with stuff like this:

{BRAG_ON_KILL "Bad Guy" _"I got a kill!" _"Ha! I got another one!" _"I'm awesome."}

{BRAG_ON_KILL "Wizard" _"This is the power of Light!" _"Tremble, forces of darkness!" _"The power of Light prevails!"}
Post Reply