OK, my bad, I've solved this now, so you can ignore it.
I was forgetting that if I created the array within the for clause it would be local to that clause. (At least I think that's what it was, so putting in the following at the start, and removing the terrain[j+2] = {} solved it:
for i = 1, 3 do
terrain[i] = {}
end
JulesBean: No, that's not what your problem was, although your fix clearly worked. You problem was that for every single cell, you were clearing the whole row. terrain[j+2] = {} clears the entire row. And you do that for every cell. So, for example, you would be setting [1][1], then clearing all of [1], then setting [1][2], then clearing all of [1], then finally setting [1][3]. So the only one that was going to remain was [1][3]. The most natural place to initialise each row is probably inside the outer loop (but outside the inner loop). Follow that? Although right at the beginning, as in your current solution, is also quite natural.
TheFalcon: Oh, ok, I understand that now!
Thanks.
I suspect this is probably a simple question if you know the answer, but I expect that goes for most things
The first part of my rivers script goes horribly awry...
Here's the script:
-- main function
function draw_river()
-- start up an array...
local terrain = {}
--make sure the player is in the right place
if (player.wild_mode == FALSE) then
local y = player.wilderness_y
local x = player.wilderness_x
-- is the player on a river feature?
if (wild_map(y,x).feat == 24) or (wild_map(y,x).feat == 27) or (wild_map(y,x).feat == 28) then
-- call the function to get the table...
terrain = get_terrain(y,x)
-- this debug message does *not* work...
msg_print(terrain[1][1])
end
end
end
function get_terrain(y,x)
local j,i
-- set main array:
local terrain = {}
for j = -1, 1 do
for i = -1, 1 do
--set second coord array
terrain[j+2] = {}
-- grab features. they will be put in the array at '1' to '3' in each case.
terrain[j+2][i+2] = wild_map(y+j,x+i).feat
-- debugging message *works* so the table is correctly made
--msg_print(format("j = %s, i = %s, terrain[j+2][i+2] = %s, ", j+2, i+2, terrain[j+2][i+2]))
end
end
-- send the table to the other function
return terrain
end
And here's the problem:
While the --msg_print(format"j =... line in the second function calls correctly, showing that my table is created properly, the msg_print(terrain[1][1]) in the first function throws up an error message, telling me: "argument $1 is '[no object]'".
If I do msg_print(terrain[1]) instead, it tells me that argument $1 is a table... Which it is. :-/
I have tried tostring() on the number, and get the printed value "nil". tonumber() returns the same no object lua error.
So I think I must be transferring it between the functions wrongly somehow, but I don't know how.
TIA,
[EDIT: OK, it's not what I thought. If I put this...
for j = 1, 3 do
for i = 1, 3 do
msg_print(terrain[j][i])
end
end
in the second function (and remove the non-working msg_print in the first) then I get a no object error on this bit instead.
But this should work!!!]
[EDIT: If you skipped to the bottom, this is now solved, so you don't have to read my poorly structured ramblings.
]
ToME Wiki