[~solved] Global_variables with Lua

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

Moderator: Forum Moderators

Post Reply
User avatar
ZombieKnight
Posts: 248
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.
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
Celtic_Minstrel
Developer
Posts: 2273
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: 248
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global_variables with Lua

Post by ZombieKnight »

Thanks!
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: 248
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
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: 248
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global_variables with Lua

Post by ZombieKnight »

Using wesnoth.wml_actions ...
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