[solved] How to move WML table

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

Moderator: Forum Moderators

Post Reply
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

[solved] How to move WML table

Post by ZombieKnight »

Hi
I've got this saved in
Screenshot_20240506_185619.png

Code: Select all

wml.variables["save_data"]
How to move all of its content into wml.variables?
Any ideas?
Last edited by ZombieKnight on May 7th, 2024, 10:20 am, edited 1 time in total.
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
white_haired_uncle
Posts: 1282
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: How to move WML table

Post by white_haired_uncle »

wml.array_access.get
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

Sorry?
I've found the wiki page (https://wiki.wesnoth.org/LuaAPI/wml#wml ... access.get) and I have no idea how to use it for moving wml table right into wml.variables.
Could you explain it a bit more?
It's a function that just returns a value or does it do even something else?
What is this supposed to do:

Code: Select all

function get_recall_list(side)
    wesnoth.fire("store_unit", { x = "recall", variable = "LUA_recall_list" })
    local l = wml.array_access.get "LUA_recall_list"
    wml.variables.LUA_recall_list = nil
    return l
end
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
Ravana
Forum Moderator
Posts: 3064
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: How to move WML table

Post by Ravana »

wml.variables is not real table, so you cant insert another table into it. For regular tables it is just table.insert(destination_table, wml_table).
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

Okay,
So I've got a table of tables... how to put these tables into wml.variables then?
What are you thinking about?
...
Or the first table is on the right place... so can't I just remove those [save_data] tags from it and leave it there?
Last edited by ZombieKnight on May 6th, 2024, 6:59 pm, edited 1 time in total.
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
Ravana
Forum Moderator
Posts: 3064
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: How to move WML table

Post by Ravana »

You cant put them to wml.variables. You can think of wml.variables as function, not as table. It reflects state of wml variables, but itself is not wml variables.
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

Oh...
So where to put them so I can acces them later using
wml.variables[my_var]?
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
Ravana
Forum Moderator
Posts: 3064
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: How to move WML table

Post by Ravana »

Assign them with the API you have available to create wml variables. Including wml.variables if you prefer. Those like set_variable, set_variables, wml.array_access.set, wml.array_variables, wml.variables_proxy.
User avatar
Celtic_Minstrel
Developer
Posts: 2273
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: How to move WML table

Post by Celtic_Minstrel »

Loading it back in is going to be pretty much the same procedure as saving it was. The scalar ones should be easy, literally the exact same loop you used to save but flip the assignment. Container variables will be a bit more tricky. I think something along these lines may work?

Code: Select all

local tags = {}
for index, elem in ipairs(save_data) do
	tags[elem.tag] = true
end
for tag,_ in pairs(tags) do
	wml.array_variables[tag] = wml.child_array(save_data, tag)
end
There might be a more efficient way (as each assignment here is probably looping over all of save_data again), but I think that approach should do the job.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

Ok, I'll try it but... have you seen the code t
for erasing all the container variables?

Code: Select all

for index, elem in ipairs(wml.all_variables) do
wml.variables[""..elem[1].."[0]"] = nil end
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

Celtic_Minstrel wrote: May 6th, 2024, 11:07 pm There might be a more efficient way (as each assignment here is probably looping over all of save_data again), but I think that approach should do the job.
Not sure... you meant wml.variables.save_data?
I extract the data from global using this:

Code: Select all

wesnoth.wml_actions.get_global_variable(
{
namespace="Bandits_from_Brown_Hills",
to_local="save_data",
from_global="save_file",
side=1,
}
)
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

...And I really think your code is really wrong...

Code: Select all

for index, elem in ipairs(wml.variables["save_data"]) do
	std_print(index, elem)
end
returns

Code: Select all

1       table: 0x5596a93b5720
2       table: 0x5596b4cc6200
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
ZombieKnight
Posts: 250
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to move WML table

Post by ZombieKnight »

I have no idea how I made this ...
Ladies and gentlemen, I present you wml.variables replacer:

Code: Select all

wesnoth.wml_actions.get_global_variable(
{
namespace="Bandits_from_Brown_Hills",
to_local="save_data",
from_global="save_file",
side=1,
}
)
for key, value in pairs(wml.variables["save_data"]) do
    if type(value) ~= "table" then
        wml.variables[key] = value
    end
end
for index, elem in ipairs(wml.variables["save_data"]) do
    if elem[1] ~= "save_data" then
    	local i = 0
        while wml.variables[""..elem[1].."["..i.."]"] ~= nil do
            i = i + 1
        end
        wml.variables[""..elem[1].."["..i.."]"] = elem[2]
    end
end
wml.variables["save_data"] = nil

Only units, menu items, events, and sides to go!
Thanks for help so far (and I'd be glad if you'd keep helping me ^^)
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Post Reply