[~solved] Global_variables with Lua

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

Moderator: Forum Moderators

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

[~solved] Global_variables with Lua

Post by ZombieKnight »

Hi,
Any idea how to store Lua variable into global_variables?


Once I'll get wml stored into global variable...
How (in event/function) replace wml of scenario with wml from global variable?
Last edited by ZombieKnight on May 16th, 2024, 7:20 am, edited 1 time in total.
User avatar
Celtic_Minstrel
Developer
Posts: 2340
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Global_variables with Lua

Post by Celtic_Minstrel »

The Lua API to global variables is not finalized, so it might change without warning, but it does exist.

Code: Select all

local GV = wesnoth.experimental.wml.global_vars
local ns = "your_namespace"
-- Set a global variable
GV[ns].varname = "stuff"
GV[ns]["varname"] = "stuff"
-- Read a global variable
print(GV[ns].varname)
-- Clear a global variable (not sure if this one actually works)
GV[ns].varname = nil
That's all equivalent to using the WML tags with immediate=yes. There's no Lua equivalent to immediate=no at the moment.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 337
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global_variables with Lua

Post by ZombieKnight »

Thanks!
User avatar
ZombieKnight
Posts: 337
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global_variables with Lua

Post by ZombieKnight »

...
If I understand right this should create a variable varname and set it to "stuff"

Code: Select all

[lua]
    code=<<
local GV = wesnoth.experimental.wml.global_vars
local ns = "Bandits_from_Brown_Hills"
GV[ns].varname = "stuff"
GV[ns]["varname"] = "stuff"
>>
[/lua]
...
But it does nothing
User avatar
ZombieKnight
Posts: 337
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global_variables with Lua

Post by ZombieKnight »

Using wesnoth.wml_actions ...
Post Reply