[role] consufion

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
Spannerbag
Posts: 535
Joined: December 18th, 2016, 6:14 pm
Location: Yes

[role] consufion

Post by Spannerbag »

Hi all,

can someone explain the following please - it's probably me not understanding how [role] works... :roll:

What I wanted to do was preferentially assign a [role] to a particular unit (a leader).
Originally I used multiple [have_unit] clauses to test for progressively less strict criteria.
So the first [have_unit] would test for the specific unit I wanted and assign the [role] to that unit if it existed, the next [have_unit] would assign it any side leader and the third to any unit on that side.
It all worked fine. :)

Then I thought I'd be clever :doh:
The thought occurred that I could strip out the conditional logic simply by assigning the [role] in reverse order, reassigning it until the [role] filter failed.

I tested the concept (role reassignment) on my stub (test) campaign but didn't get the results I expected:


Here's the relevant code chunks:
side setup

Code: Select all

  [side]
    team_name=Goodies
    user_team_name= _ "Goodies"
    side=1
    x,y=1,1
    id=Goody
    name=_"Goody"
    type=Spearman
    extra_recruit=Mage
    controller=human
    canrecruit=yes
    recruit=Bowman,Spearman
    gold=100
  [/side]
test WML

Code: Select all

# Turn 1
  [event]
    name=turn 1
#    {DEBUG_MSG (_"Creating Silver Mage")}
    {GENERIC_UNIT 1 (Silver Mage) 4 4}
    {GENERIC_UNIT 1 (Red Mage) 4 6}
#    {DEBUG_MSG (_"Creating second leader")}
    {GENERIC_UNIT 1 Sergeant 4 5}
    [+unit]
      canrecruit=yes
    [/unit]
    [redraw]
    [/redraw]

    [role]			# Mage
      role=testrole
      canrecruit=no
      side=1
      reassign=yes
    [/role]
    [message]
      role=testrole
      message=_"canrecruit=no: It's me!"
    [/message]

    [role]			# Red Mage
      role=testrole
      side=1
      x,y=4,6
      reassign=yes
    [/role]
    [message]
      role=testrole
      message=_"x,y=4,6: It's me!"
    [/message]

    [role]			# Random side 1 unit (not Sergeant)
      role=testrole
      side=1
      [not]
        x,y=4,5
      [/not]
      reassign=yes
    [/role]
    [message]
      role=testrole
      message=_"random not Sergeant: It's me!"
    [/message]

    [role]
      role=testrole
      reassign=yes
      x,y=4,5
    [/role]
    [message]
      role=testrole
      message=_"x,y=4,5: It's me!"
    [/message]

    [role]			# Random side 1 leader
      role=testrole
      canrecruit=yes
      side=1
      reassign=yes
    [/role]
    [message]
      role=testrole
      message=_"canrecruit=yes: It's me!"
    [/message]

    [role]
      role=testrole
      type=Sergeant
      reassign=yes
    [/role]
    [message]
      role=testrole
      message=_"type=Sergeant: It's me!"
    [/message]
  [/event]
I ran the test twice getting identical results both times (I restarted Wesnoth and purged the cache in between).

These were

canrecruit=no
speaker=Silver Mage
as expected

x,y=4,6
Expected Red Mage
but speaker still Silver Mage

random not sergeant
speaker=Goody
Seems OK

4,5 (should be Sergeant)
Expected Sergeant
but speaker still Goody

canrecruit=yes
Still Goody - (could be right or wrong...?)

type=Sergeant
Expected Sergeant
but speaker still Goody

version: Wesnoth 1.16.6 running on Windows 7

It's probably me but can anyone explain why the role isn't working as I'd expected please?

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
beetlenaut
Developer
Posts: 2825
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: [role] consufion

Post by beetlenaut »

Your misunderstanding is that roles are designed to be permanent, so there is no automatic method for removing them. If you used :inspect after your start event, you would see that all three units have the role. They are being picked mostly at random for the messages. You have to remove a role manually it if you want that to happen. (modify_unit should work fine.) There is a "reassign" key, but if you read about it on the wiki, it says that the key only determines whether a search is performed first. It doesn't say anything about removing the role from a unit that already had it. If you view roles as permanent, this action makes sense, though maybe the key should have been named something different.

So, you can move roles around if you want, but you will have to do it carefully. Remember that they are never guaranteed to be assigned at all. Internal unit variables are more direct and specific things, so you may consider using those instead.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Spannerbag
Posts: 535
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: [role] consufion

Post by Spannerbag »

beetlenaut wrote: January 11th, 2023, 6:20 am Your misunderstanding is that roles are designed to be permanent ...
Brilliant! Actually I'd gotten two things wrong; not only the permanence but I also had the idea that a role was always assigned to a single unit :doh:

I'll stick with my existing, if cumbersome, logic that works.

Thanks for the explanation, much appreciated.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply