Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Your specific error (probably)</h2> <p>You do not test against nil values. Any non-initialized table (= array in this case) members are <code>nil</code> by definition. And comparing <code>nil</code> with a number will generate an error:</p> <pre><code>lua: attempt to compare nil with number </code></pre> <p>However, as you seem unable to provide the <em>actual</em> error message, this is only a guess. Do not take me wrong these are errors in your code, but there could be <em>something else</em> wrong that I overlooked. Anyway here are some comments along with your code to show you what happens</p> <pre><code>if #blocks &lt;= height then -- say #blocks is 3, height is 4 if #blocks[h] &lt;= length then -- E: in iteration 4 blocks[h] is nil if #blocks[h][y] &lt;= width then -- E: in it. 3,4 blocks[h][y] is nil num = blocks[h][y][x] end end end </code></pre> <p>You would have to test against nil on every level first, like</p> <pre><code>if blocks[h] and blocks[h][y] and blocks[h][y][x] and #blocks[h]&lt;=height and #blocks[h][y]&lt;=height and #blocks[h][y][x]&lt;=height num = blocks[h][y][x] end </code></pre> <h2>General programming mistakes</h2> <p><strong><code>blocks</code></strong>, <strong><code>length</code></strong>, <strong><code>width</code></strong> and <strong><code>height</code></strong> seem to be parameters for your function but are not in its header, so I suppose you set them externally before calling the function? This is certainly not good practice.</p> <p><strong><code>temp</code></strong> and <strong><code>num</code></strong> should be declared <code>local</code>.</p> <h2>Alternative</h2> <p>You can make the data-structure more intelligent, for example, if you put your 3D array in a flattened table and add a <code>__call</code> metamethod like this</p> <pre><code>function newMdArray(X, Y, Z) local MT = { __call = function(t, x, y, z, v) if x&gt;X or y&gt;Y or z&gt;Z or x&lt;1 or y&lt;1 or z&lt;1 then return nil end local k = x + X*(y-1) + X*Y*(z-1); if v ~= nil then t[k] = v; end return t[k] end }; return setmetatable({}, MT); end </code></pre> <p>Then this is all you have to do to make a resized copy of it:</p> <pre><code>function resizeMdArray(array, X, Y, Z) local r = newMdArray(X, Y, Z); for x=1, X do for y=1, Y do for z=1, Z do r(x, y, z, array(x, y, z) or 16); end end end return r; end </code></pre> <p>A nice bonus is, since this data structure flattens the 3D array into a 1D array, if you only want to <em>copy</em> the data, you can do so by simply accessing it as a table and copying each element:</p> <pre><code>for i=1, X*Y*Z do new[i] = old[i] end </code></pre> <p>Of course you could do the very same with a "real" (hidden) 3-D array in the background saving you the arithmetic calculations, however then you would have to test for empty values all the time to prevent nil errors.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload