Multidimensional Arrays

TheLastScotsman

  • Posts: 87
I am currently trying to create a grid that my character can move on using multidimensional arrays in the drag and drop mode. I have a basic knowledge of how multidimensional arrays work but I am not sure how to go about making the grid using the drag and drop mode. My problem is I dont know how to increment the x and y based on their positions in the arrays. This is what I have so far, instead of incrementing them I just have the 1's and 0's in their array spaced out and the arrays themselves are spaced to make sure it is at least is differentiating them. Also the example in the looping areas section of the help center does not work. Any sugestions or any multidimensional array tips in general would be much appreciated, thanks in advance.

« Last Edit: July 09, 2011, 10:41:30 pm by TheLastScotsman »
I want to be the very best that no one ever was. To create them is my true test, to train them is my cause. I will travel across the web searching far and wide, each forum post to understand the power thats inside!

Epic428

  • Posts: 1118
Well one problem in your logic is the fact that you are setting My Map equal to 5 different lists. This means that it will only equal the last list. I recommend adding each "level" to to the list My Map.

Also, because we don't have a get item from list as list block, or an as list block itself, you will need to use a code block to access any list inside another.

I had created a very simple behavior that creates a 2D List and then prints the values in the list. This should at least give you a basic idea of how they are done and how you can access them. I have attached the Screenshot.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

TheLastScotsman

  • Posts: 87
Hey any chance you could explain some of that to me cause I am not completly getting it?
I want to be the very best that no one ever was. To create them is my true test, to train them is my cause. I will travel across the web searching far and wide, each forum post to understand the power thats inside!

Alexin

  • *
  • Posts: 3127
The first "repeat" makes an "horizontal" list.
If Grid Width is 4:

0 1 2 3

Then, it puts that list into the "vertical" one Grid Height times.
You end up with something like this, considering Grid Height is 4:

0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3

There's a problem, though, if you use that code as it is. Each row (horizontal list) corresponds to the same list, so if you change one, all the others change too.


Knowing that you want lists inside another list, it helps to draw the lists and use arrows to connect them, in a sheet of paper.
"Find the fun"
alexin@stencyl.com

TheLastScotsman

  • Posts: 87
I get that, I guess whats confusing me is the print list part. Also I tried creating a behavior like that so I could mess around with it and see if I could figure it out and it did't work for some reason.
I want to be the very best that no one ever was. To create them is my true test, to train them is my cause. I will travel across the web searching far and wide, each forum post to understand the power thats inside!

Alexin

  • *
  • Posts: 3127
The printList part simply iterates over the vertical list. Since each element of the vertical list is a list itself, then it also iterates over it.
"Find the fun"
alexin@stencyl.com

Epic428

  • Posts: 1118
Yeah, this was just a basic example of adding a list inside another list. and then how to retrieve the data from each list inside the main list. How you choose to add and access the lists is up to you, as it depends on the situation.

It would be nice if there were blocks, for making and accessing multidimensional arrays, built right into the engine. For now, some of that code can be condensed a bit and placed into a Global Custom block. If anything, at least an  "[] as List" block would be nice.

Perhaps, if I get some time this week, I'll sit down and write a behavior for handling multidimensional lists, to reduce the need for code blocks, but I think there would still be an issue with it as I have had problems with custom blocks returning Lists.

It appears that when you set the return type to list, it doesn't actually return a list for some reason. It instead returns an object. I realized this because if you make a custom block, set the return type to List, then place a set [list attribute] to block and put the custom block inside it, the compiler yells about Implicit Coercion of Object to unrelated type Array.[/list]
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

coleislazy

  • *
  • Posts: 2607
There are some tricks you can use to work with multidimensional arrays in SW. Check out my screenshot and note the use of variables, and that the X's in the IDE don't always translate to errors in code. In the "for each" blocks, I used the variable name "item" to refer to the original "for each" block's current item. And in the last block I left off the opening and closing bracket because SW automatically puts them around that field. Its hacky, but I think its useful.

EDIT: Forgot to mention that I used the variable "index0" to refer to the current loop count for the first "repeat __ times" box. The second loop uses "index1", and so on.

« Last Edit: July 11, 2011, 04:52:59 pm by coleislazy »

Epic428

  • Posts: 1118
It is interesting that you were able to add index0 where a number would belong, as well as adding the brackets to the get item block to retrieve a particular value.

Have you actually tested that this works?
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

coleislazy

  • *
  • Posts: 2607
I have. I've also used it to generate a random rogue-like dungeon where Map[12][14] refers to the grid location and the array stored at that location (so its actually a 3 dimensional array) contains the information about that tile.

If you flip it over to the code preview, you can see that the sytax is correct. Not all fields will accept variables like that, however. Some turn the fields into strings.

Epic428

  • Posts: 1118
Well that is pretty sweet then. I didn't realize that it was possible to do that. I know text blocks can be left empty and will still compile regardless of the red X, but I hadn't tested other field types.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs