Changing the direction a unit is facing

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.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Changing the direction a unit is facing

Post by Helmet »

Hi. I need a unit to change their facing without moving into a new hex. The unit needs to remain where they are, but facing sw instead of se. It's for a cut-scene between two units speaking to one another.

I even tried [redraw][/redraw], to force the screen to update the units, thinking that might help. But no luck.

I'm using version 1.15.6. Thanks for your help.

Code: Select all

[event]
	name=turn 3
	[unit]
		side=1
		type="Elvish Shaman"
		id="Fira"
		name= _ "Fira"    		
		x,y=1,5
		facing=se
	[/unit]
	[message]
		speaker=Fira
		message= _ "Hello? Anybody here?"
	[/message]	
	[modify_unit]
		[filter]
			id=MyLeader
		[/filter]
		facing=sw #   the unit turns to face the person who just arrived
	[/modify_unit]
	[redraw][/redraw]	
	[message]
		speaker=MyLeader
		message= _ "Welcome."
	[/message]		
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
Pilauli
Posts: 115
Joined: August 18th, 2020, 12:56 pm

Re: Changing the direction a unit is facing

Post by Pilauli »

Odd. Based on this snippet in the thread https://forums.wesnoth.org/viewtopic.php?f=21&t=53463, I would have expected that to work.

Code: Select all

	[modify_unit]
		[filter]
			x,y=$x2,$y2
		[/filter]
		facing=$-unit.facing
	[/modify_unit]
Try changing the sw temporarily to $-unit.facing, and see whether it works to do that. If that makes the unit turn completely around, then the "sw" doesn't work (I don't know why it wouldn't work, but you never know...), whereas if it does nothing, then the rest of the snippet doesn't work.

The other thing I can think of that you might try is if your leader has some animation that could be interpreted as surprise, waving, or snapping to attention, then you could try using [animate_unit] (https://wiki.wesnoth.org/InterfaceActio ... te_unit.5D) to play that animation, to force it to reconsider what it looks like. I don't know if it would work or not, but you could try it.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Changing the direction a unit is facing

Post by Helmet »

Pilauli wrote: November 12th, 2020, 6:38 pm Try...
Duuude! Your code worked! $- ..?? How did you learn that stuff? :shock:

I'm glad that you think my code should've worked. I suspect there are bugs in 1.15.6 related to facing. In 1.15.5, I noticed two facing-related bugs.

1) A unit created in [side] would face SE, regardless of the facing= (fixed in 1.15.6).
2) A newly recruited unit would always face SE, not a random direction (not fixed yet).

Mainline has a macro that changes a unit's facing: {MODIFY_UNIT id=MyLeader facing sw} . It works in add-ons, too. But I try to not use macros until I've examined the code myself, and I couldn't find the macro's code within the labyrinth of mainline folders.

I was toying with animating the unit turning his head, but decided against it. I have too many sprites that still need attack animations.

Thanks!
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
Pilauli
Posts: 115
Joined: August 18th, 2020, 12:56 pm

Re: Changing the direction a unit is facing

Post by Pilauli »

Well, I remembered testing out the moving-through-units code, and noticing that it was very neat when the unit turned around, so I tracked it down in the WML workshop again.

The MODIFY_UNIT macro is in data/core/macros/utils.cfg, if you want to look at it.

Edit: ah, I should note that $-unit.facing seems to turn the unit completely around. So if it was facing SE, now it faces NW, if it was facing N, now it faces S, and so on. This means it will only work in a limited set of circumstances where the unit was facing partially sideways (se, sw, ne, nw) and only has south/southeast-facing graphics. That way, even if it thinks it's now facing nw, in practice, it appears to look sw, which is a horizontal flip of se, and so on. So it's a bit of a hacky and finicky solution, but it's okay so long as it works in your case.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Changing the direction a unit is facing

Post by Helmet »

I actually had checked that file. I assumed it wasn't the one I was looking for because I didn't see the word facing anywhere. Oops. I now realize my mistake.

Here's the macro, should anyone need it.

Code: Select all

#define MODIFY_UNIT FILTER VAR VALUE
    # Alters a unit variable (such as unit.x, unit.type,
    # unit.side), handling all the storing and unstoring.
    #
    # Example that flips all spearmen to side 2:
    #! {MODIFY_UNIT type=Spearman side 2}
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        variable=MODIFY_UNIT_store
        kill=yes
    [/store_unit]

    [foreach]
        array=MODIFY_UNIT_store
        [do]
            [set_variable]
                name=this_item.{VAR}
                value={VALUE}
            [/set_variable]

            [unstore_unit]
                variable=this_item
                find_vacant=no
            [/unstore_unit]
        [/do]
    [/foreach]

    {CLEAR_VARIABLE MODIFY_UNIT_store}
#enddef
Last edited by Helmet on November 12th, 2020, 10:19 pm, edited 2 times in total.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
Pilauli
Posts: 115
Joined: August 18th, 2020, 12:56 pm

Re: Changing the direction a unit is facing

Post by Pilauli »

Well, it changes whatever "var" is to the value of "value". So when you write facing and sw, it changes facing to sw. It's a very open-ended macro!

(Hey, does the modify_unit macro actually work? If so, then the fact that it works but regular facing=sw doesn't work would probably be very useful information when reporting it as an issue.)
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Changing the direction a unit is facing

Post by Helmet »

Pilauli wrote: November 12th, 2020, 10:17 pm Well, it changes whatever "var" is to the value of "value". So when you write facing and sw, it changes facing to sw. It's a very open-ended macro!

(Hey, does the modify_unit macro actually work? If so, then the fact that it works but regular facing=sw doesn't work would probably be very useful information when reporting it as an issue.)
Yes, it works. I reported the bug about my facing code not working a few minutes ago. I'll add that information to the report.
Pilauli wrote: November 12th, 2020, 10:00 pm...I should note that $-unit.facing seems to turn the unit completely around.
Thanks for explaining. I was wondering if it mirrored the unit's direction.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Celtic_Minstrel
Developer
Posts: 2195
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Changing the direction a unit is facing

Post by Celtic_Minstrel »

Pilauli wrote: November 12th, 2020, 10:00 pm Edit: ah, I should note that $-unit.facing seems to turn the unit completely around.
What the heck? That should never work. It's supposed to be -$unit.facing, not $-unit.facing. The latter shouldn't even be recognized as a variable substitution.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Changing the direction a unit is facing

Post by Helmet »

Celtic_Minstrel wrote: November 14th, 2020, 8:14 pm What the heck? That should never work. It's supposed to be -$unit.facing, not $-unit.facing. The latter shouldn't even be recognized as a variable substitution.
Hm. I just tested them both, and they both work. The -$ version makes a little more sense, now that you mention it. It's like the minus sign is saying, "Flip the following variable," instead of, "A variable is forthcoming, flip it, and here it is."
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Celtic_Minstrel
Developer
Posts: 2195
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Changing the direction a unit is facing

Post by Celtic_Minstrel »

I just tested the WMl variable interpolation system on the latest master, and $-unit.facing does not do anything. I suspect the only reason it appears to work is because the engine sees an invalid direction and replaces it with a default value that coincidentally matches what you want to see.

Side note: If you want to turn the unit relative to its current direction, you can also use $unit.facing:cw (for a clock-wise turn) or $unit.facing:ccw (for a counter-clockwise turn).
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Changing the direction a unit is facing

Post by Helmet »

Celtic_Minstrel wrote: November 15th, 2020, 12:41 am Side note: If you want to turn the unit relative to its current direction, you can also use $unit.facing:cw (for a clock-wise turn) or $unit.facing:ccw (for a counter-clockwise turn).
I just tested them and they flip a unit to the opposite direction. Thanks.

I don't understand how they work, though.

I used debug/inspect to check the variables before and after. When I used $unit.facing:cw on a unit, ne became sw. I expected ne to become se when the direction rotated one hex-side clockwise.

So I assumed the direction must rotate 3 hex-sides clockwise. So I put two $unit.facing:cw back to back, expecting the unit to return to it's original direction. But it didn't. Geez, it seems that every time I learn one new thing about WML, two or more questions arise from the darkness!
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Celtic_Minstrel
Developer
Posts: 2195
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Changing the direction a unit is facing

Post by Celtic_Minstrel »

Helmet wrote: November 15th, 2020, 2:25 am When I used $unit.facing:cw on a unit, […snip…]. I expected ne to become se when the direction rotated one hex-side clockwise.
This is what's supposed to happen. If it's not working, either there's a bug somewhere or something changes the unit's direction between when you saw that it's ne and when you modified its direction.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: Changing the direction a unit is facing

Post by Helmet »

Oops, I made a mistake. I neglected to comment-out this macro: {MODIFY_UNIT id=MyLeader facing sw} and it turned the unit sw. I happen to use the old TextWrangler/BBCode syntax highlighter you made. I love it, but if there is one feature I wish it had, it would be that macros had a color other than light purple, the same color used for comments. Sometimes my macros are hard to notice amidst comments.

Anyhow...I commented-out the macro and retested facing=$unit.facing:cw. It turns out that it doesn't work at all. I reported the bug.

Side note: Is there a way for me to make macros appear green (or some other color) in the syntax highlighter you made? I have no idea where to start. I'd also like to add [elseif] to the recognized list of tags.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Changing the direction a unit is facing

Post by gfgtdf »

[modify_unit] facing has problems in 1.15 see https://github.com/wesnoth/wesnoth/issues/4978
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
Celtic_Minstrel
Developer
Posts: 2195
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Changing the direction a unit is facing

Post by Celtic_Minstrel »

Helmet wrote: November 15th, 2020, 1:34 pm I reported the bug.
Where?
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply