Painting sections of the map using arrays

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
WedgeR8
Posts: 7
Joined: December 27th, 2018, 11:09 am

Painting sections of the map using arrays

Post by WedgeR8 »

My first time writing here so I apologize if something is wrong in the formatting.

I was trying to paint that section of the map with cave wall but when I run this it replaces only one tile (the first) as if the foreach loop stopped on the first element.

Something is wrong and I don't know if it's me that can't really use arrays and foreach or it's the full method that is flawed, so I needed some help...

Edit: Ok, I edited this because I just noticed that seems pointless written like this because i could have just put the locations inside the terrain tag. The Idea was to use this as a macro and use a variable instead of the cave wall terrain, to add a structure without manually repeat the replace terrain for each different type of terrain.

Code: Select all

[store_locations]
    x=0-26
    y=0-13
    variable=map_locations
[/store_locations]
[foreach]
    array=map_locations
    [do]
        [terrain]
            terrain="Xu"
            x=$map_locations.x
            y=$map_locations.y
        [/terrain]
    [/do]
[/foreach]
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Painting sections of the map using arrays

Post by vghetto »

I think foreach uses the variable this_item.
for can use the array variable with an index.
You'll need to look it up on the wiki and check which is which.
WedgeR8
Posts: 7
Joined: December 27th, 2018, 11:09 am

Re: Painting sections of the map using arrays

Post by WedgeR8 »

Ok, thank you man, I managed to get it to work by adding the index and replacing with for, I don't know if it's the optimized way to do it but it works (full code for clarity)

Code: Select all

[store_locations]
    x=0-26
    y=0-13
    variable=map_locations
[/store_locations]
[set_variable]
    name=times
    value=0
[/set_variable]
[for]
    array=map_locations
    reverse=yes
    [do]
        [terrain]
	    terrain="Xu"
	    x=$map_locations[$times].x
	    y=$map_locations[$times].y
        [/terrain]
	[set_variable]
	    name=times
            add=1
	[/set_variable]
    [/do]
[/for]
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Painting sections of the map using arrays

Post by vghetto »

the [for] assigns the variable $i and clears it for you automatically. x=$map_locations[$i].x would have worked.
With the [foreach] you could have done x=$this_item.x

$map_locations.x is the same as $map_locations[0].x that is why foreach in the original post felt like it stopped on the first item. It didn't it applied the terrain on the same location map_locations.length times :)
WedgeR8
Posts: 7
Joined: December 27th, 2018, 11:09 am

Re: Painting sections of the map using arrays

Post by WedgeR8 »

Oh, ok man, thank you
Post Reply