Selecting and Moving Only One Unit At a Time

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
Pewskeepski
Posts: 378
Joined: November 17th, 2010, 6:24 pm
Location: An icy dungeon beneath Antarctica

Selecting and Moving Only One Unit At a Time

Post by Pewskeepski »

I have a scenario in which there are Galleon units scattered about the map, all on deep water hexes (Wo). What I want is for every new turn, the game to pick a random galleon unit and then assign a random "goto" target for it. The "goto targets" in question will all be shallow water hexes (Ww) with an adjacent beach hex (Ds), similar to the scenario Black Flag in Son of the Black Eye, I believe.

I already know I can use [store_locations] for the targets, and {MODIFY_UNIT (goto_y/x)} to get the galleon units to go there. So my question in a nutshell is this: how should I go about making it select only one galleon unit and only one target to move the unit to? I want the cycle to then repeat every new turn, each time selecting a new galleon unit and a new target for said unit to go to.

If someone could at least point me in the right direction for this, I'd be mighty grateful. I'm currently using Wesnoth 1.15.10 :)
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
User avatar
Ravana
Forum Moderator
Posts: 2934
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Selecting and Moving Only One Unit At a Time

Post by Ravana »

You could add unit status or variable to those galleons which have already started moving. And when target is set, remove that one from targets array.
User avatar
Pewskeepski
Posts: 378
Joined: November 17th, 2010, 6:24 pm
Location: An icy dungeon beneath Antarctica

Re: Selecting and Moving Only One Unit At a Time

Post by Pewskeepski »

But is there a way to select 1 Galleon unit to assign such things, without applying it to every unit of that type? All the units are stationary, and I want 1 to be selected every turn, but the rest to remain as they are. I don't want the next Galleon to start moving after the previous has landed, I want it to start moving in the next turn.

I guess my biggest obstical right now is figuring out how to pick out 1 unit without effecting all the others :hmm:
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
User avatar
Ravana
Forum Moderator
Posts: 2934
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Selecting and Moving Only One Unit At a Time

Post by Ravana »

Store units in array, and pick one of them by index.
User avatar
Pewskeepski
Posts: 378
Joined: November 17th, 2010, 6:24 pm
Location: An icy dungeon beneath Antarctica

Re: Selecting and Moving Only One Unit At a Time

Post by Pewskeepski »

Okay, could someone please explain how to make an array work more specifically? Try as I have, I can't seem to figure it out :augh:

Again, I want all of a certain unit type to be stored, then every new turn, ONE is selected to be modified into a "goto_y/x" location, which will also be a singularly selected coordinate from a stored array of certain locations. I feel like, if I figure one out how to select one unit at a time, selecting the locations will be done the same way.
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Selecting and Moving Only One Unit At a Time

Post by vghetto »

Show what you have so far. I might be able to help you better.

More generically, as Ravana has said.
Store units in array, ...

Code: Select all

[store_unit]
variable=unit_array
[filter]
type=Galleon
[/filter]
[/store_unit]
... and pick one of them by index.

Code: Select all

{RANDOM 0.."$($unit_array.length-1)"}
The variable random will have the index of the unit

Code: Select all

{VARIABLE unit_array[$random].goto_x 10}
{VARIABLE unit_array[$random].goto_y 10}
Unstore the modifed unit to the map

Code: Select all

[unstore_unit]
variable=unit_array[$random]
[/unstore_unit]
It's something like that.
User avatar
Pewskeepski
Posts: 378
Joined: November 17th, 2010, 6:24 pm
Location: An icy dungeon beneath Antarctica

Re: Selecting and Moving Only One Unit At a Time

Post by Pewskeepski »

vghetto wrote: April 27th, 2021, 7:19 am Show what you have so far. I might be able to help you better.

More generically, as Ravana has said.

...code stuff...

It's something like that.
Ah-ha! That seems to be working, thank you! The missing piece for me was this:

Code: Select all

{RANDOM 0.."$($unit_array.length-1)"}
The "-1" after length. Does that number signify how many units it selects from the array?

Also, I was trying to use {MODIFY_UNIT}/[modify_unit] but couldn't figure out how to make it identify the unit by it's variable. Now I realize you can just use {VARIABLE} macro/tag, as you did here:

Code: Select all

{VARIABLE unit_array[$random].goto_x 10}
{VARIABLE unit_array[$random].goto_y 10}
If I have any more questions, I'll ask, but it seems to be working for now :)
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2337
Joined: May 24th, 2010, 5:26 pm
Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
Contact:

Re: Selecting and Moving Only One Unit At a Time

Post by Lord-Knightmare »

Does that number signify how many units it selects from the array?
The array is zero-index. Zero-indexed arrays are used in a lot of programming languages and it might as well be taken as common practice. However, there are some programming languages that use 1-indexed arrays as well.
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Selecting and Moving Only One Unit At a Time

Post by vghetto »

Pewskeepski wrote: April 27th, 2021, 10:08 pm The "-1" after length. Does that number signify how many units it selects from the array?
No, it means subtract 1 from the length to get the index value.
When you have 1 unit, the array length will be one and the index of that unit will be 0.

It might be worthwhile to check that unit_array.length is greater than 0 before doing any operations..

Code: Select all

[if]
{VARIABLE_CONDITIONAL unit_array.length greater_than 0}
[then]
    # Do the stuff above
    {RANDOM 0.."$($unit_array.length-1)"}
    {VARIABLE unit_array[$random].goto_x 10}
    {VARIABLE unit_array[$random].goto_y 10}
    [unstore_unit]
        variable=unit_array[$random]
    [/unstore_unit]
    
    # etc ...
[/then]
[/if]
Post Reply