[solved] unit.__cfg.advances_to

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

[solved] unit.__cfg.advances_to

Post by hermestrismi »

I am surrounding. can someone provide me with a function to check if a unit can advance to another unit or to null?
I tried many different methods but I am stock.
Last edited by hermestrismi on August 27th, 2024, 6:40 am, edited 1 time in total.
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: unit.__cfg.advances_to

Post by Celtic_Minstrel »

You have unit.advances_to (__cfg not needed here, and in fact it makes it worse), which is a list of possible advancements, so you can iterate through that list and compare each element to your reference.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

Celtic_Minstrel wrote: July 20th, 2024, 6:47 am You have unit.advances_to (__cfg not needed here, and in fact it makes it worse), which is a list of possible advancements, so you can iterate through that list and compare each element to your reference.
I already tried that. I use print to check the resulted string and even type(val) to check if it is a string or nil.
when a unit have advances_to "null", I get an empty space which I can't tell if it is a string or not.
Attachments
20240720_080510_mfnr.jpg
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: unit.__cfg.advances_to

Post by white_haired_uncle »

See if this helps get you started

Code: Select all

        wesnoth.interface.add_chat_message(string.format("%s advances to:",unit.name))
        if #unit.advances_to == 0 then
                wesnoth.interface.add_chat_message("Nothing!")
        else
                for _,v in pairs(unit.advances_to) do
                        wesnoth.interface.add_chat_message(string.format("\t%s",v))
                end
        end
That will only handle a single advancement, though. If you need to see if the unit has a path to advance to something that takes more than one advancement you'll have to expand the search, probably using wesnoth.unit_types on the results.
Speak softly, and carry Doombringer.
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

a small workaŕound I found is this (sorry to post a picture instead of code).
If I didn't use else, units with more than one advances will be a match, too.
this way I get units with only "null" as advancement
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

image
Last edited by hermestrismi on July 20th, 2024, 9:51 am, edited 1 time in total.
User avatar
octalot
General Code Maintainer
Posts: 818
Joined: July 17th, 2010, 7:40 pm
Location: Austria

Re: unit.__cfg.advances_to

Post by octalot »

Please use the [code] tag to copy-and-paste instead of pasting a screenshot.
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

octalot wrote: July 20th, 2024, 9:43 am Please use the [code] tag to copy-and-paste instead of pasting a screenshot.
I m sorry for that. I can't plug or send anything directly from the pc for now. I will delete the image and post the code later
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: unit.__cfg.advances_to

Post by white_haired_uncle »

You're trying to use tostring on a table? I don't think that's going to do what you want.

Also, I note you say "advancement = ". "advancement" is singular, which makes me wonder if you are considering the fact that a unit can have multiple advancements.
Speak softly, and carry Doombringer.
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

white_haired_uncle wrote: July 20th, 2024, 9:50 am You're trying to use tostring on a table? I don't think that's going to do what you want.
I was surprised that it worked, too.
before that, I used other methods and it didn't work.
now, I have a table containing only units with advances not null. all what I need now, is comparing the two table (table for all units on map and units with advances_to not null) then extract the rest.
Attachments
17214693174463072328514535532309.jpg
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

Code: Select all

function Books_Inventory_Configuration()
	-- first step, loop over all units of side 1 (units on map) and extract units that have 'advances_to=null' (in the __.cfg files of the type of unit). then build a table from the matching units.
	local eligible_units = {}
	local legal_units = {}
	local inadequate_units = {}
	local value = ""
	-- Loop over all units on side 1 (units on the map)
	local units = wesnoth.units.find_on_map{ side = 1}
	for _, unit in ipairs(units) do
		table.insert(eligible_units, {
			type = unit.type,
			experience = unit.experience,
			id = unit.id,
			name = unit.name,
			advancement = tostring(unit.__cfg.advances_to),
			image = unit.__cfg.image
		})
	end
	for _, eligible_unit in ipairs(eligible_units) do
		value = eligible_unit.advancement
		if value == "" then
		else
		table.insert(inadequate_units, {
			id = eligible_unit.id
		})
		print("id: " .. eligible_unit.id)
		print("--------------------")
		end
	end
end
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

the main problem for my point of view is that 'advances_to=null' give an empty space when use 'unit.advance_to' or 'unit.__cfg.advances_to'. in both cases, that space is not a nil value neither a "null" string nor a 'string'. what is it exactly?
note: I use advancement without 's' because I will use a table of 'advancements' later and was afraid that the two word may cause a conflict in my mind :doh:
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: unit.__cfg.advances_to

Post by white_haired_uncle »

If you just want to check for advances_to==null, #unit.advances_to==0 should do it.

There's a button in the lower left corner of the lua console screen that will allow you to copy to clipboard so you don't have to use photos.
Speak softly, and carry Doombringer.
User avatar
hermestrismi
Posts: 730
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: unit.__cfg.advances_to

Post by hermestrismi »

white_haired_uncle wrote: July 20th, 2024, 10:06 am If you just want to check for advances_to==null, #unit.advances_to==0 should do it.

There's a button in the lower left corner of the lua console screen that will allow you to copy to clipboard so you don't have to use photos.
thanks. my pc is very slow. a pic from the phone is faster for me but I will try to not use them too much
Post Reply