[solved] Global variables begginer guide

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

Moderator: Forum Moderators

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

[solved] Global variables begginer guide

Post by ZombieKnight »

Hi
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]
Any idea why In the persist/Bandits_from_Brown_Hills.cfg is only this:?
(from my first experiment)

Code: Select all

[variables]
	game=0
[/variables]
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)
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
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Global varialbes begginer guide

Post by Spannerbag »

ZombieKnight wrote: May 4th, 2024, 3:38 pm Hi
I'm working with persistent variables in Wesnoth for the very first time...
Same here, used them in an update to one of my campaigns. :?

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
Hope this helps.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
ZombieKnight
Posts: 243
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global varialbes begginer guide

Post by ZombieKnight »

Thanks for tips.

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
User avatar
Ravana
Forum Moderator
Posts: 3063
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Global variables begginer guide

Post by Ravana »

Easy way is to just save the number as another variable.
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Global varialbes begginer guide

Post by Spannerbag »

Sorry to be dim, but are you saying that this code:
ZombieKnight wrote: May 4th, 2024, 7:11 pm

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]
produced this output:

Code: Select all

[variables]
	game=0
[/variables]
in 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]
in persist/Bandits_from_Brown_Hills.cfg.


ZombieKnight 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]
Is [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. :doh:

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
ZombieKnight
Posts: 243
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global varialbes begginer guide

Post by ZombieKnight »

Spannerbag wrote: May 5th, 2024, 12:26 am Sorry to be dim, but are you saying that this code:
ZombieKnight wrote: May 4th, 2024, 7:11 pm

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]
produced this output:

Code: Select all

[variables]
	game=0
[/variables]
No, it does nothing.
Game=0 is from an older test.
You can't put "." Into the global variable name.
Spannerbag wrote: May 5th, 2024, 12:26 am
ZombieKnight 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]
Is [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. :doh:

Cheers!
-- Spannerbag
First I've managed to store it into persistent just as it is shown in my post.
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
User avatar
ZombieKnight
Posts: 243
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global variables begginer guide

Post by ZombieKnight »

Ravana wrote: May 5th, 2024, 12:14 am Easy way is to just save the number as another variable.
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
User avatar
ZombieKnight
Posts: 243
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Global variables begginer guide

Post by ZombieKnight »

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
Post Reply