[solved] Global variables begginer guide
Moderator: Forum Moderators
- ZombieKnight
- Posts: 249
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
[solved] Global variables begginer guide
Hi
I'm working with persistent variables in Wesnoth for the very first time...
Any idea why In the persist/Bandits_from_Brown_Hills.cfg is only this:?
(from my first experiment)
What am I doing bad?
(Jup I know it's WML but I'd like to understand it first, make it in Lua once I'll understand it)
I'm working with persistent variables in Wesnoth for the very first time...
Code: Select all
[set_variable]
name=game_number
value=starting_value_string
[/set_variable]
[set_global_variable]
namespace=Bandits_from_Brown_Hills
from_local=game_number
to_global=loads.1.1.1
side=1
immediate=yes
[/set_global_variable]
(from my first experiment)
Code: Select all
[variables]
game=0
[/variables]
(Jup I know it's WML but I'd like to understand it first, make it in Lua once I'll understand it)
Last edited by ZombieKnight on May 16th, 2024, 7:21 am, edited 2 times 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
Working on campaign Bandits from Brown Hills
- Spannerbag
- Posts: 553
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Global varialbes begginer guide
Same here, used them in an update to one of my campaigns.ZombieKnight wrote: ↑May 4th, 2024, 3:38 pm Hi
I'm working with persistent variables in Wesnoth for the very first time...
Here are macros I used to manipulate them (I didn't need one to clear a persistent variable).
Note that persistent variables need a namespace.
Persistent variables are not set directly but take their value from a local variable.
Similarly, to retrieve (read) a persistent variable the value must first be put into a local variable.
My macros specify the local variable first then the global one.
Contents of file
persistent.cfg
.
Code: Select all
#textdomain wesnoth-LSB
# Persistent variables macros ("side=" not needed for single player campaigns)
# Clear a global variable currently not used
# Set a persistent variable
#
#define LSBGLOBAL_SET LVAR GVAR
[set_global_variable]
namespace=spannerbag_lsb
from_local={LVAR}
to_global={GVAR}
# side=1
immediate=yes
[/set_global_variable]#enddef
# Retrieve a persistent variable
#
#define LSBGLOBAL_RETRIEVE LVAR GVAR
[get_global_variable]
namespace=spannerbag_lsb
from_global={GVAR}
to_local={LVAR}
# side=1
[/get_global_variable]#enddef
Usage examples:
Read a persistent variable into a local variable
{LSBGLOBAL_RETRIEVE rwd_hi_fromglobal rwd_hi_global}
Write/update a persistent variable from a local variable
{LSBGLOBAL_SET rwd_hi rwd_hi_global}
I based these on the helper macros in
core/macros/utils.cfg
:Code: Select all
#define GLOBAL_VARIABLE NAMESPACE LOCAL_VAR_NAME GLOBAL_VAR_NAME SIDE
#Assigns a persistent variable with the contents of a standard variable.
[set_global_variable]
namespace={NAMESPACE}
from_local={LOCAL_VAR_NAME}
to_global={GLOBAL_VAR_NAME}
side={SIDE}
[/set_global_variable]
#enddef
#define VARIABLE_FROM_GLOBAL NAMESPACE GLOBAL_VAR_NAME LOCAL_VAR_NAME SIDE
#Retrieves the contents of a persistent variable and stores them in a standard variable.
[get_global_variable]
namespace={NAMESPACE}
from_global={GLOBAL_VAR_NAME}
to_local={LOCAL_VAR_NAME}
side={SIDE}
#immediate=no
[/get_global_variable]
#enddef
Cheers!
-- Spannerbag
- ZombieKnight
- Posts: 249
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Global varialbes begginer guide
Thanks for tips.
Has to be like this actually:
I have this format of global variables...
How to get the highest number of loads.number?
Any ideas?
Has to be like this actually:
Code: Select all
[set_global_variable]
namespace=Bandits_from_Brown_Hills
from_local=game_number
to_global=loads.1.1.1
side=1
immediate=yes
[/set_global_variable]
I have this format of global variables...
How to get the highest number of loads.number?
Any ideas?
Code: Select all
[loads]
[1]
[1]
[variables]
1=1
2=12
3=8
[/variables]
[/1]
[2]
[variables]
1=1
2=12
3=8
[/variables]
[/2]
[/1]
[2]
[1]
[variables]
1=1
2=12
3=8
4=10
5=2324
[/variables]
[/1]
[/2]
[/loads]
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
Re: Global variables begginer guide
Easy way is to just save the number as another variable.
- Spannerbag
- Posts: 553
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Global varialbes begginer guide
Sorry to be dim, but are you saying that this code:
in
If so then something set a global variable
No idea why, all I can suggest is simplify and try again.
Given the code above, I'd expect to see a statement like:
in
Thought it might've been some savefileWML thing but seemingly not.
Never seen that before - tho' there's lots about WML I don't know...
Assuming that the contents/values in
Of course conditional WML's
If the number of comparisons is high you could consider a bubble sort?
Otherwise the first thing I'd try would be nested
Otherwise if you're considering reading data from a file on disk, no idea, sorry.
WML has limited disk write capabilities, probably deliberately for security reasons.
If you want to store these values in persistent variables then I'd shove them all in a single array for ease of processing when the data is retrieved.
Not sure any of that helps, but as I said I'm not clear what it is you're trying to do.
Cheers!
-- Spannerbag
produced this output:ZombieKnight wrote: ↑May 4th, 2024, 7:11 pmCode: Select all
[set_variable] name=game_number value=starting_value_string [/set_variable] [set_global_variable] namespace=Bandits_from_Brown_Hills from_local=game_number to_global=loads.1.1.1 side=1 immediate=yes [/set_global_variable]
Code: Select all
[variables]
game=0
[/variables]
persist/Bandits_from_Brown_Hills.cfg
?If so then something set a global variable
game
to zero apparently.No idea why, all I can suggest is simplify and try again.
Given the code above, I'd expect to see a statement like:
Code: Select all
[variables]
loads.1.1.1="starting_value_string"
[/variables]
persist/Bandits_from_Brown_Hills.cfg
.IsZombieKnight wrote: ↑May 4th, 2024, 7:11 pm I have this format of global variables...
How to get the highest number of loads.number?
Any ideas?Code: Select all
[loads] [1] [1] [variables] 1=1 2=12 3=8 [/variables] [/1] [2] [variables] 1=1 2=12 3=8 [/variables] [/2] [/1] [2] [1] [variables] 1=1 2=12 3=8 4=10 5=2324 [/variables] [/1] [/2] [/loads]
[loads]
a custom tag?Thought it might've been some savefileWML thing but seemingly not.
Never seen that before - tho' there's lots about WML I don't know...
Assuming that the contents/values in
[loads]
came from or can be stored in an array, then to get the highest number (in WML) I'd use the WFL max function (or something similar in lua if you prefer).Of course conditional WML's
greater_than
is available but the formula is shorter - and there might be clever ways to compare more than 2 values at a time, perhaps - not really thought about it.If the number of comparisons is high you could consider a bubble sort?
Otherwise the first thing I'd try would be nested
[foreach]
statements (one [foreach]
per array dimension) to do a linear pass through all array elements and return the highest (or joint highest if the highest value occurs more than once).Otherwise if you're considering reading data from a file on disk, no idea, sorry.
WML has limited disk write capabilities, probably deliberately for security reasons.
If you want to store these values in persistent variables then I'd shove them all in a single array for ease of processing when the data is retrieved.
Not sure any of that helps, but as I said I'm not clear what it is you're trying to do.
Cheers!
-- Spannerbag
- ZombieKnight
- Posts: 249
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Global varialbes begginer guide
No, it does nothing.Spannerbag wrote: ↑May 5th, 2024, 12:26 am Sorry to be dim, but are you saying that this code:
produced this output:ZombieKnight wrote: ↑May 4th, 2024, 7:11 pmCode: Select all
[set_variable] name=game_number value=starting_value_string [/set_variable] [set_global_variable] namespace=Bandits_from_Brown_Hills from_local=game_number to_global=loads.1.1.1 side=1 immediate=yes [/set_global_variable]
Code: Select all
[variables] game=0 [/variables]
Game=0 is from an older test.
You can't put "." Into the global variable name.
First I've managed to store it into persistent just as it is shown in my post.Spannerbag wrote: ↑May 5th, 2024, 12:26 amIsZombieKnight wrote: ↑May 4th, 2024, 7:11 pm I have this format of global variables...
How to get the highest number of loads.number?
Any ideas?Code: Select all
[loads] [1] [1] [variables] 1=1 2=12 3=8 [/variables] [/1] [2] [variables] 1=1 2=12 3=8 [/variables] [/2] [/1] [2] [1] [variables] 1=1 2=12 3=8 4=10 5=2324 [/variables] [/1] [/2] [/loads]
[loads]
a custom tag?
Thought it might've been some savefileWML thing but seemingly not.
Never seen that before - tho' there's lots about WML I don't know...
Assuming that the contents/values in[loads]
came from or can be stored in an array, then to get the highest number (in WML) I'd use the WFL max function (or something similar in lua if you prefer).
Of course conditional WML'sgreater_than
is available but the formula is shorter - and there might be clever ways to compare more than 2 values at a time, perhaps - not really thought about it.
If the number of comparisons is high you could consider a bubble sort?
Otherwise the first thing I'd try would be nested[foreach]
statements (one[foreach]
per array dimension) to do a linear pass through all array elements and return the highest (or joint highest if the highest value occurs more than once).
Otherwise if you're considering reading data from a file on disk, no idea, sorry.
WML has limited disk write capabilities, probably deliberately for security reasons.
If you want to store these values in persistent variables then I'd shove them all in a single array for ease of processing when the data is retrieved.
Not sure any of that helps, but as I said I'm not clear what it is you're trying to do.
Cheers!
-- Spannerbag
Second Problemm is HOW to read loads.number.1.1?
Any ideas?
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
- ZombieKnight
- Posts: 249
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Global variables begginer guide
True, but I've got to read it anyways... so it won't solve all my problems.
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
- ZombieKnight
- Posts: 249
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: Global variables begginer guide
Solved:
Code: Select all
[while]
[variable]
name=game_number
not_equals=""
[/variable]
[do]
[set_variable]
name=loop_number
add=1
[/set_variable]
[get_global_variable]
namespace=Bandits_from_Brown_Hills.loads.$loop_number|.1
from_global=1
to_local=game_number
side=1
[/get_global_variable]
[/do]
[/while]
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills