Dynamic alocation of 2D 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
Elrood
Posts: 17
Joined: September 27th, 2007, 5:44 pm

Dynamic alocation of 2D array?

Post by Elrood »

I have a problem with allocating 2D array.
First dimension allocates right, second dimension is nonexisting inside save game in [variables] tag
My current code is (well, different names for variables):

Code: Select all

[set_variables]
      name=array
[/set_variables]
{REPEAT_SAFE $columns iterator_0 (
      [set_variables]
            name=array.x[$iterator_0]
            mode=insert
      [/set_variables]
)}
{REPEAT_SAFE $columns iterator_0 (
      {REPEAT_SAFE $rows iterator_1 (
            [set_variables]
                  name=array.x[$iterator_0].y[$iterator_1]
                  mode=insert
            [/set_variables]
      )}
)}
Macros are ok, checked them.
I do realize i can emulate n'thD array as 1D array with some operations ;) , but still 2D would be nicer and easier to debug.
Anyone can help with that?
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Dynamic alocation of 2D array?

Post by Sapient »

Array indexes are created automatically as needed. So there is no need to pre-populate an array. Thus, typical WML style would be to leave it uninitialized until such an index is needed to store actual data. However, one could do this very easily if it were desired.

Code: Select all

[set_variable]
  name=big_empty_array.x[$max_columns].waste_space
  value="yes"
[/set_variable]
[clear_variable]
  name=big_empty_array.x[$max_columns].waste_space
[/clear_variable]
{FOREACH big_empty_array.x column}
  [set_variable]
    name=big_empty_array.x[$column].y[$max_rows].waste_space
    value="yes"
  [/set_variable]
  [clear_variable]
    name=big_empty_array.x[$column].y[$max_rows].waste_space
  [/clear_variable]
{NEXT column}

As for your code, the engine should probably be complaining that you are attempting to set a variable without supplying any value. Fore example, [value][/value].

Other than that, mode=insert is not intended to insert in front of a non-existant array index. It is mentioned in the WML Reference not to use it for that purpose.
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
Elrood
Posts: 17
Joined: September 27th, 2007, 5:44 pm

Re: Dynamic alocation of 2D array?

Post by Elrood »

Thank you very much :)
Post Reply