Converting LUA table to WML array[solved]

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

Moderator: Forum Moderators

Post Reply
User avatar
Inkerrio
Posts: 42
Joined: September 1st, 2021, 8:09 pm

Converting LUA table to WML array[solved]

Post by Inkerrio »

Apparently, you do this through wml.array_access.set, but it doesn't work, not creating new WML variables and not modifying exiting. What am I missing?

Code: Select all

    wesnoth.game_events.on_mouse_move = function(x, y)
	local u = wesnoth.units.get(17, 3)
	local path, cost = wesnoth.paths.find_path(u, {3,20}, { ignore_units = true, viewing_side = 0 })
	for i, loc in ipairs(path) do
    wml.array_access.set("coordinate_list", {x = loc[1], y = loc[2]})
    end
end
Last edited by Inkerrio on December 2nd, 2024, 8:27 pm, edited 2 times in total.
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Converting LUA table to WML array

Post by white_haired_uncle »

Looks like you're creating the same array over and over. I'm guessing you're trying to repeatedly append to coordinate_list, but you're just overwriting it.

Perhaps you could tell us what you're trying to accomplish?

Try this

Code: Select all

wesnoth.game_events.on_mouse_move = function(x, y)
	local u = wesnoth.units.get(17, 3)
	local path, cost = wesnoth.paths.find_path(u, {3,20}, { ignore_units = true, viewing_side = 0 })
        local foo = {}
	for _, loc in pairs(path) do
            table.insert(foo, {x = loc[1], y = loc[2]})
        end 
        wml.array_access.set("coordinate_list", foo)
end
Last edited by white_haired_uncle on November 29th, 2024, 2:28 pm, edited 1 time in total.
Speak softly, and carry Doombringer.
User avatar
Inkerrio
Posts: 42
Joined: September 1st, 2021, 8:09 pm

Re: Converting LUA table to WML array

Post by Inkerrio »

I'm trying to turn LUA table with x y coordinates from wesnoth.paths.find_path into a set of WML arrays. The iteration seems to be working correctly, as I can print these coordinates to console, but I'm definitely missing something when it comes to converting them to WML, as this step isn't described well anywhere
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Converting LUA table to WML array

Post by white_haired_uncle »

You want a set of arrays? Seems odd, but okay. You can't call them all coordinate_list though.

I fixed the example above. It doesn't do exactly what you ask, but I bet if you try it you just might like it.

https://wiki.wesnoth.org/LuaAPI/wml#wml ... access.set
Speak softly, and carry Doombringer.
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Converting LUA table to WML array

Post by Celtic_Minstrel »

Inkerrio wrote: November 29th, 2024, 6:00 am Apparently, you do this through wml.array_access.set, but it doesn't work, not creating new WML variables and not modifying exiting. What am I missing?

Code: Select all

    wesnoth.game_events.on_mouse_move = function(x, y)
	local u = wesnoth.units.get(17, 3)
	local path, cost = wesnoth.paths.find_path(u, {3,20}, { ignore_units = true, viewing_side = 0 })
	for i, loc in ipairs(path) do
    wml.array_access.set("coordinate_list", {x = loc[1], y = loc[2]})
    end
end
That's not wml.array_access.set is for. If you're using a loop like that, you can just write wml.variables['coordinate_list[' .. i .. ']'] = {x = loc[1], y = loc[2]}. What wml.array_access.set does is basically handle that loop for you, so you could probably also write wml.array_access.set("coordinate_list", path) instead of the loop.

Another way to say it is that you only need to use array_access if you have an array in Lua and want to transfer it directly into WML. You don't use it just because you happen to have an array in WML.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Inkerrio
Posts: 42
Joined: September 1st, 2021, 8:09 pm

Re: Converting LUA table to WML array

Post by Inkerrio »

wml.variables['coordinate_list[' .. i .. ']'] = {x = loc[1], y = loc[2]} worked, thank you both!

Solved
Post Reply