Placing blocks by ID using more than one array
Hi All
I think I might be trying to do the impossible here, but would be grateful if anyone can an idea if I am barking up the wrong tree please?
I have a building (shown here) with a series of arenas that I want the user to "install" new competition maps onto by running code.
So, I have a series of arrays, one for each column of the arena grid that are defined on start containing block IDs
I have another array that contains the names of the column arrays that is also defined on start.
So this code initiates the arrays and orientated the builder (its "on chat" at the mo, but will eventually be "on start" and will call the second module)
This routine loops through the column arrays in turn to set the variable "col" to each item in the column arrays before passing control to the third module.
The third routine should work its way down the 19 columns in turn placing 30 blocks before moving back to the previous routine to pickup the name of the next column array (col1).
When testing by replacing the "col" variable with the name of a column array it will happily place the column of blocks, but obviously does not progress through the 19 column arrays. ie, it places 19 identical columns of block arrangements. When I replace the direct reference to a column array with the variable "col" that refers to a column array in the place block I get an error which says the string is not assignable to a number. I get that, but the variable "col" picks up its value from the array index, so has to be a number, so should be ok to define the block ID?
I'd be grateful for any thoughts. I could use a different method, but this way would be fairly neat. I will if I have to but before I do, I just want to check that there is not a way of making that "col" variable more palatable for the place block by ID command.
(ignore the "say" blocks. That's just me tracing programme control)
Thanks for your time.
Laurence
-
It would be nice to know more about the column arrays (colarrays) and col, since you initialize from 0-18 and the access from 0-29 and I'm not quite following without seeing all the code.
But if I am understanding you correctly, your line of code which currently has a compatibility error (that triangle) works fine when you substitute the "name of the column array" So the command is just the purple "place" and then "col" - right? and that works?
So I think the problem is that you need a new variable. In run2 you set col to an element of colarrays, and it has that datatype now.
So either you want colarray get value at index in run3, or something else like a new variable. But I need more code (and commented code =] please ) to say more.
Hope it helps.
0 -
Brilliant! Thank you! I have 19 column arrays each with 30 elements, and another array to list the 19 column variables so pretty difficult to take a meaningful screenshot. Completely understand the need to create another variable, and I did try, but just could not shake off the compatibility error, but probably more to do with my inability to manage or control data types than anything else.
But if I am understanding you correctly, your line of code which currently has a compatibility error (that triangle) works fine when you substitute the "name of the column array" So the command is just the purple "place" and then "col" - right? and that works?
Exactly right!
When I run the code the "col" and "index1" variables increment correctly but the "place" command can't accept that "index1" refers to a position in the array "col", - but does if I specifically name the array and don't use the "col" variable which correctly contains name of the appropriate array.
Block by ID here is always text, whether as the col variable or as a direct reference to the array. "Index1" is always an integer incremented by the "for" loop. But it works for the direct reference but not with the variable."Place" is looking for a number and I don't understand why it can't pick up the number in the array "col" at position "index1". I worried that my arrays were not defined properly (as integers), but then the routine would not work when I substitute "col" with a direct reference to the array - at which point I got lost!
I'll happily annotate some screenshots if you have the time to take a look. I do appreciate the support. Thank you.
0 -
So how about adding a line, (prior to the place command,) accessing the value you want using the index1, (saving the amount in a variable) and then using THAT variable in the place command, so you get the movement through the array properly?
Am I tracking along? Worth a shot?
0 -
Laurence Boulter So how is it going? If you are working in MakeCode, you can share the code with the share link also.
0 -
Hi Debbie. Sorry to not have got back to you sooner. I have to fit this in with my other adventures... so need to to take of the ones that pay me first! I spend time n this last Friday, but deviced to start over. I don't think the "place block by ID" block will accept a text based variable to represent the ID of a block. So I restructured and replaced the 19 column arrates for each level with much longer arrays and wrote a routine to work through te three arrays for the three levels of blocks. Works a treat!
I'm spending a day on the project tomorrow, and was going to get back to you then. Don't know if you are on LinkedIn but...
(hopefully that works?)
... but I'll share the code with you.
Thanks for sticking with it!
1
Please sign in to leave a comment.
Comments
5 comments