Topic: get_workers() (wl.game.Player method) should get a table / array of workers like get_buildings

beng
Avatar
Topic Opener
Joined: 2023-07-03, 20:41 UTC+2.0
Posts: 29
OS: Debian 13
Version: 1.21 build 6 i think
Ranking
Pry about Widelands
Location: singapore
Posted at: 2025-12-07, 09:04 UTC+1.0

There are lua functions for the player called get_ships() and get_buildings() that return an array of ships or buildings for that player. However, there are no similar functions to return an array of workers for that player. The get_workers() function returns the number of workers. For scripting purposes it would be helpful if there was a way to get the workers of a particular player as an array so a script can iterate though them to check what they are doing or where they are. For example, in pseudo code:

  • get an array wokers of type scout of a player
  • for each worker in the array, get the location field of the scout and check if it is owned by this player or another player
  • if the scout is inside another player's land, then do some other scripted action
Edited: 2025-12-07, 09:05 UTC+1.0

Top Quote
kaputtnik
Avatar
Joined: 2013-02-18, 20:48 UTC+1.0
Posts: 2769
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: 2025-12-07, 10:19 UTC+1.0

In a game there are many, many, workers and the workers are created on demand (if the corresponding tools are available). For scouts such a function might be sufficient because one usually does not have much workers of type scout, but e.g. for soldiers such an array of soldiers will be outdated right away if the soldiers are in a fight for example. I think it does not make much sense to provide such a function if the resulting array will be outdated soon (does not reflect the actual status of this workers in a game).

You can probably do something like this:

  • get buildings of players scout huts
  • loop over the fields around the scouts hut field with radius x
  • check if the field has bobs
  • check if this bob is type worker and "scout" via the desrc property
  • check fields owner
  • do some stuff

Top Quote
tothxa
Avatar
Joined: 2021-03-24, 12:44 UTC+1.0
Posts: 567
OS: antiX / Debian
Version: some new PR I'm testing
Ranking
One Elder of Players
Posted at: 2025-12-07, 10:20 UTC+1.0

I think that's because you cannot do anything with individual workers (so there are no lua hooks, or even C++, for that either), and that's a core part of the game mechanics.


Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16 UTC+1.0
Posts: 2918
OS: Windows
Version: always the latest
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2025-12-07, 13:12 UTC+1.0

kaputtnik wrote

In a game there are many, many, workers and the workers are created on demand (if the corresponding tools are available). For scouts such a function might be sufficient because one usually does not have much workers of type scout, but e.g. for soldiers such an array of soldiers will be outdated right away if the soldiers are in a fight for example. I think it does not make much sense to provide such a function if the resulting array will be outdated soon (does not reflect the actual status of this workers in a game).

You can probably do something like this:

  • get buildings of players scout huts
  • loop over the fields around the scouts hut field with radius x
  • check if the field has bobs
  • check if this bob is type worker and "scout" via the desrc property
  • check fields owner
  • do some stuff

that is exactly how it is done in Fri02 and Atl02 for example.


Top Quote
beng
Avatar
Topic Opener
Joined: 2023-07-03, 20:41 UTC+2.0
Posts: 29
OS: Debian 13
Version: 1.21 build 6 i think
Ranking
Pry about Widelands
Location: singapore
Posted at: 2025-12-08, 03:23 UTC+1.0

Yes, I saw the code in Atl02 which is why I made this suggestion. It is clunky and inefficient and does not always work (i found this in gameplay where the script would not trigger) perhaps because the scout may be outside the range that is checked or if the game is speeded up it may have gone outside the check range in between the scripted sleep periods or whatever. It would be better if a script could simply get an array of all the player's scouts (or hunters etc) and then iterate through them to check and so some stuff.


Top Quote