Get and Filter Unit Type List

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
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Get and Filter Unit Type List

Post by Ghite »

I need to find all of the unit types which fit a certain filter. (For example filtering level=1 would give a Giant Rat, Vampire Bat, Mudcrawler, Walking Corpse, Peasant, Woodsman, and Ruffian.

Is there an easy (or at-least doable) way to do this?
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Get and Filter Unit Type List

Post by Dugi »

I am not quite sure what do you mean, neither of the units you listed don't have level 1 for example. But if I guessed correctly, it is doable, but it's not easy at all. It is similar to this. Basically, you need to use lua to create a new WML tag that would be able to do it, the lua code would search through all unit_types, check if their level is the level you want and add them to the list if they are. I mean, I might write this code for you, but I want to be sure if it is what you need, and also what should its output be.
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Re: Get and Filter Unit Type List

Post by Ghite »

Okay I meant level 0.
Ghite
Posts: 23
Joined: July 7th, 2013, 11:28 pm

Re: Get and Filter Unit Type List

Post by Ghite »

Yes. It is similar. This would work. Thanks
User avatar
tekelili
Posts: 1038
Joined: August 19th, 2009, 9:28 pm

Re: Get and Filter Unit Type List

Post by tekelili »

I had a similar problem for my scenario, and I did this:

Code: Select all

[variables]
{core/units/bats}
{core/units/drakes}
{core/units/dwarves}
{core/units/elves}
{core/units/goblins}
{core/units/gryphons}
{core/units/humans}
{core/units/merfolk}
{core/units/nagas}
{core/units/ogres}
{core/units/orcs}
{core/units/saurians}
{core/units/trolls}
{core/units/undead}
{core/units/wose}
[/variables]
From here you have stored in the variable unit_type all info you could need to filter over.
Be aware English is not my first language and I could have explained bad myself using wrong or just invented words.
World Conquest II
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Get and Filter Unit Type List

Post by zookeeper »

That works, but I think you should definitely rather take the time to figure out how to do it through Lua, using wesnoth.unit_types or something. Otherwise you'll have at least one copy of every single line of unit type WML bloating up your savefiles forever.

A non-Lua way would be to use [store_unit_type_ids] to get a list of all unit types, then use [set_variables] [split] to turn it into an array so you can iterate through it, create a temporary unit of every single type and check which ones match your filter. But needless to say, that wouldn't be very elegant.
User avatar
tekelili
Posts: 1038
Joined: August 19th, 2009, 9:28 pm

Re: Get and Filter Unit Type List

Post by tekelili »

zookeeper wrote:That works, but I think you should definitely rather take the time to figure out how to do it through Lua, using wesnoth.unit_types or something. Otherwise you'll have at least one copy of every single line of unit type WML bloating up your savefiles forever.
You right, I forgot that I was filtering only over cost and also did this to no bloat savefiles too much:

Code: Select all

{FOREACH unit_type ut_index}
    {VARIABLE wct_unit_type[$ut_index].id $unit_type[$ut_index].id}
    {VARIABLE wct_unit_type[$ut_index].cost $unit_type[$ut_index].cost}
{NEXT ut_index}
    # just save cost info to no bloat savefile
{CLEAR_VARIABLE unit_type}
Be aware English is not my first language and I could have explained bad myself using wrong or just invented words.
World Conquest II
User avatar
Dugi
Posts: 4960
Joined: July 22nd, 2010, 10:29 am
Location: Carpathian Mountains
Contact:

Re: Get and Filter Unit Type List

Post by Dugi »

I think this should do the trick. Include this in a lua tag into _main.cfg or get it loaded another way.

Code: Select all

helper = wesnoth.require "lua/helper.lua"
function wesnoth.wml_actions.store_unit_types_from_level(cfg)
   local var = cfg.variable or "stored_types"
   local level = cfg.level or helper.wml_error "[store_unit_type_from_level] missing required level= attribute."
   local list = {}
   for i,unit in pairs(wesnoth.unit_types) do
      if unit.level == level then
         table.insert(list, {"type" , unit.__cfg })
      end
   end
   wesnoth.set_variable(var, list )
end
Then you can store all unit types of the desired type using this:

Code: Select all

[store_unit_types_from_level]
  variable=level_1_units
  level=1
[/store_unit_types_from_level]
I haven't checked it, so small errors can be there, but I hope there are none, I was careful.
User avatar
trewe
Translator
Posts: 122
Joined: December 24th, 2012, 5:37 pm
Location: Portugal
Contact:

Re: Get and Filter Unit Type List

Post by trewe »

Dugi wrote:I haven't checked it, so small errors can be there, but I hope there are none, I was careful.
The problem with that attempt is that it stores all units known to the engine, e.g. a whole mess of dozens of multiplayer add-on units and those unusable there.

If in other hand it is supposed for SP, than it can be used without problems.
Post Reply