[closed] How wesnoth.set_variable works?

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

Moderator: Forum Moderators

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

[closed] How wesnoth.set_variable works?

Post by ZombieKnight »

Hi
This code:

Code: Select all

wesnoth.set_variable(wml.variables["my_unit_stored[0].attack[1].damage[0]"],1)
gives me this error:

Code: Select all

20240202 14:58:21 info deprecation: wesnoth.set_variable has been deprecated indefinitely.;  (Note: You should use wml.variables instead in new code)
20240202 14:58:21 error scripting/lua: lua/core/_initial.lua:50: bad argument #1 to 'elem' (string expected, got nil)
stack traceback:
        [C]: in function 'wml.set_variable'
        lua/core/_initial.lua:50: in field 'set_variable'
        ~add-ons/Bandits_from_Brown_Hills/lua/print_data.lua:74: in main chunk
        [C]: in field 'dofile'
        lua/package.lua:53: in field 'require'
        [string "wesnoth.require '~add-ons/Bandits_from_Brown_..."]:1: in local 'bytecode'
        lua/wml-tags.lua:265: in local 'cmd'
        lua/wml-utils.lua:144: in field 'handle_event_commands'
        lua/wml-flow.lua:5: in function <lua/wml-flow.lua:4>
^C
I'm sure wml.variables["my_unit_stored[0].attack[1]"] exists
Last edited by ZombieKnight on April 20th, 2024, 6:04 am, edited 1 time in total.
I had saurian in profile before, but I've merged my discord profile with forum one...
User avatar
Celtic_Minstrel
Developer
Posts: 2238
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: How wesnoth.set_variable works?

Post by Celtic_Minstrel »

You seem to be extremely confused here. The syntax wml.variables[...] and the syntax wesnoth.set_variable are completely orthogonal; the former replaces the latter.

The code you're looking for is this:

Code: Select all

wml.variables["my_unit_stored[0].attack[1].damage[0]"] = 1
However, that won't actually work either, as you can't assign a scalar to a container. You need to add .something onto the end of your variable path, or remove [0].

For what it's worth, if you had been using set_variable, the first parameter would've been just the variable path.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply