Selecting and Moving Only One Unit At a Time
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.
- 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
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![Smile :)](./images/smilies/icon_smile.gif)
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
![Smile :)](./images/smilies/icon_smile.gif)
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
Re: Selecting and Moving Only One Unit At a Time
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.
- 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
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![Pensive :hmm:](./images/smilies/icon_pensive.gif)
I guess my biggest obstical right now is figuring out how to pick out 1 unit without effecting all the others
![Pensive :hmm:](./images/smilies/icon_pensive.gif)
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
Re: Selecting and Moving Only One Unit At a Time
Store units in array, and pick one of them by index.
- 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
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
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.
![Augh :augh:](./images/smilies/icon_augh.gif)
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!)
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
Re: Selecting and Moving Only One Unit At a Time
Show what you have so far. I might be able to help you better.
More generically, as Ravana has said.
The variable random will have the index of the unit
Unstore the modifed unit to the map
It's something like that.
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)"}
Code: Select all
{VARIABLE unit_array[$random].goto_x 10}
{VARIABLE unit_array[$random].goto_y 10}
Code: Select all
[unstore_unit]
variable=unit_array[$random]
[/unstore_unit]
- 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
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)"}
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}
![Smile :)](./images/smilies/icon_smile.gif)
"Everything is better with penguins."
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
Creator of Burning Souls, The Fall of Wesnoth (abandoned) and Adventures of Knighthood (now available on BfW 1.15!)
- Lord-Knightmare
- Discord Moderator
- Posts: 2364
- 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
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.Does that number signify how many units it selects from the array?
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
Re: Selecting and Moving Only One Unit At a Time
No, it means subtract 1 from the length to get the index value.Pewskeepski wrote: ↑April 27th, 2021, 10:08 pm The "-1" after length. Does that number signify how many units it selects from the array?
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]