[solved] Updating side variables from LUA script.

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

Moderator: Forum Moderators

Post Reply
Sventimir
Posts: 10
Joined: November 11th, 2020, 9:01 am

[solved] Updating side variables from LUA script.

Post by Sventimir »

In Wesnoth 1.18.2, in my scenario WML, for some sides I have the following variables section:

Code: Select all

    [variables]
        [tactics]
            [tactic]
                role=assassin
                count=0
                weigth=3
            [/tactic]
            [tactic]
                role=hunter
                count=0
                weight=2
            [/tactic]
            [tactic] # no particular role
                count=0
                weight=1
            [/tactic]
            total_counts=0
            total_weights=6
        [/tactics]
    [/variables]

    [/tactics]
When I'm trying to access these variable from a LUA script, this seems to work, although not exactly like the documentation describes it. https://wiki.wesnoth.org/VariablesWML#T ... les.5D_tag states that:
An array variable with given name foo is assigned using several [foo] tags, where the first tag describes foo[0], the second foo[1], ...
However, when I access:

Code: Select all

wesnoth.sides[4].variables.tactics
,
I get:

Code: Select all

{{'tactic', {role='assassin',count=0,weight=3}},{'tactic', {role=hunter,count=0,weight=2}},{'tactic',{weight=1,count=0}},total_counts=0,total_weights=6}
I believe this is the right representation of the variables' WML, but reading the documentation I was expecting to get:

Code: Select all

{{role='assassin',count=0,weight=3},{role=hunter,count=0,weight=2},{count=0,weight=1}}
If the output of the query is correct, I believe the documentation should be made more clear on what user should expect.

The real problem, however, is that I cannot find a way to update any of the variables (either from LUA console or through a script). The values seem to be read-only for LUA. I can update them the deprecated way, using wesnoth.set_side_variable function, in which case the array seems to be addressed like the documentation describes, i.e. I can access the first tactic's counter with "tactics.tactic[0].count".

So to sum up, the new way of accessing these variables seems to either be buggy or insufficiently documented. Can someone shed some light on the matter, please?
Last edited by Sventimir on July 23rd, 2024, 2:23 pm, edited 1 time in total.
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Updating side variables from LUA script.

Post by Celtic_Minstrel »

The new way of accessing them is documented on the wiki here. You can also use the functions in the wml module on them now, which wasn't possible with the old way.

For example:

Code: Select all

local my_side = wesnoth.sides[1]
print(my_side.variables["tactics.tactic[0]"])
-- prints {role = "assassin", count = 0, weight = 3}
print(wml.array_access.get("tactics", my_side))
-- prints {{role='assassin',count=0,weight=3},{role=hunter,count=0,weight=2},{count=0,weight=1}}
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Sventimir
Posts: 10
Joined: November 11th, 2020, 9:01 am

Re: Updating side variables from LUA script.

Post by Sventimir »

Thanks for the pointer, I think I've figured it out now.
Post Reply