Copying array contents to another array

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
daniel.santos
Posts: 54
Joined: May 22nd, 2008, 2:45 am

Copying array contents to another array

Post by daniel.santos »

Hello again!

I guess this is my first time to get serious with WML and Wesnoth, after all of these years. I tend to make assumptions based upon other languages I've programmed in and that's biting me in the back side! I've searched and read a lot and cannot find a working solution.

I have an array whose elements are container WML "objects" (I think of them as "objects", structs, composite values, whatever). I just want to move or copy them from one array object to another, but nothing I do works. The only thing that comes close is wrong, and that's

Code: Select all

[foreach]
    array=useable_spells
    variable=spell
    [do]
        [set_variables]
            name=use_cast_menu
            mode=append
            [value]
                image=$spell.image
                label="$spell.label"
                description="$spell.description"
                command="$spell.command"
            [/value]
        [/set_variables]
    [/do]
[/foreach]
This is wrong because it can't copy child containers. I just want the objects I have in useable_spells moved or copied into use_cast_menu.

Of course, I'm later adding this array as options to a message command, so I can see that every thing else is working when I use the (incorrect) mechanism above.

Code: Select all

[message]
    speaker=narrator
    message= _ "Use which?"
    [option]
        image=check.png
        label=_ "(End)"
        description=_ "uses left"
        [command]
            ... blah blah blah blah
        [/command]
    [/option]
    [insert_tag]
        name=option
        variable=use_cast_menu
    [/insert_tag]
[/message]
... I mean, I can modify the code to only ever insert into use_cast_menu, but that frankly uglifies it.
User avatar
Pentarctagon
Project Manager
Posts: 5593
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Copying array contents to another array

Post by Pentarctagon »

to_variable might work for this?
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
daniel.santos
Posts: 54
Joined: May 22nd, 2008, 2:45 am

Re: Copying array contents to another array

Post by daniel.santos »

Pentarctagon wrote: September 27th, 2023, 1:25 am to_variable might work for this?
Thank you. I've tried SO many variations that I sure would appreciate an example!
daniel.santos
Posts: 54
Joined: May 22nd, 2008, 2:45 am

Re: Copying array contents to another array

Post by daniel.santos »

OK, so this worked!

Code: Select all

[set_variables]
    name=use_cast_menu
    mode=append
    to_variable=useable_spells
[/set_variables]
It sure has me baffled from a linguistic stand point. Shouldn't that be called from_variable?

But thank you for the answer! I knew that I shouldn't have to be looping. This is a strange language for me, and I've programmed in a LOT of strange languages -- think Reverse Polish Notation!
User avatar
Ravana
Forum Moderator
Posts: 3070
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Copying array contents to another array

Post by Ravana »

With other language experience it might be easier to implement some of logic in Lua.
daniel.santos
Posts: 54
Joined: May 22nd, 2008, 2:45 am

Re: Copying array contents to another array

Post by daniel.santos »

Ravana wrote: September 27th, 2023, 4:51 pm With other language experience it might be easier to implement some of logic in Lua.
Yes, indeed! I think I'm starting to learn that! I also suppose I want to get the best feel that I can for how to work with WML. Wesband/Weshack is a huge mod, 77k lines of code! I'm hoping I can refactor and get some control of that beast. To me, the implementation looks terrible (no offense to anyone) but I'm in love with the game design. I just find the WAY they made Wesnoth into an Angband-ish game to be so cool! It's hard to play because of how much you need to click, but that's part of what I'm hoping to improve.

Thanks for the advice! I think it's right on.
Post Reply