1

I'm trying to make a grid, using a table, which stores information such as x & y coordinates, texture information and another table. Here's some of the code used to creating the grid:

for i = i, n do
    local random = math.floor(rainbow.random(1, 13))
    self.grid[i] = {
        sprite = Labyrinth:make_cell_texture(spritebatch, position_x, position_y, i, random),
        y = position_y,
        x = position_x,
        nav = Cell:get_nav(random)
    }
    position_x = position_x + 0.09375
end

It's in the

nav = Cell:get_nav(random)

part that I'm creating the new table:

if random == 1 then
    self.nav = {
        north = true,
        east = false,
        south = false,
        west = false,
    }

The problem is accessing said variables, to check if they are true and false. I return self.nav to grid[i].nav, and I am able to print(self.nav.east) and get a true.

But accessing the table later down the line causes it always to return nil, even though I can get it to print that there is a table there.

I've tried with this loop:

for i = 1, 10 do
    if self.grid[i].nav.north and self.grid[i + 10].nav.south then
        print("Two sides are touching!")
    end
end

Also with self.grid[i][4].north, but to no avail.

Is the way I set it up wrong? Also tried renaming nav to [4].

Am I barking up the wrong tree? Weren't tables designed to be this deep?

3
  • 1
    I can't see anything obvious that would cause this to break, but debugging problems like this is generally made much easier by the use of a table pretty-printer; Serpent is a standalone one, and Microlight and Penlight both contain decent pretty-printers and are on LuaRocks. Commented Aug 1, 2012 at 18:03
  • Thank you, will check them out. I have currently just been using Sublime Text, and the compiler telling me off as debugging (which is less then optimal!). Commented Aug 2, 2012 at 11:20
  • I tried them out, and thanks alot. Now I can actually read what my tables contain! Much thanks! Commented Aug 2, 2012 at 12:31

2 Answers 2

2

In your loop where you populate the grid you use this code:

nav = Cell:get_nav(random)

now, from your question it appears it calls a function like this:

function Cell:get_nav(random)
   self.nav = {
    north = true,
    east = false,
    south = false,
    west = false,
}
--more code here
end 

If so, what's happening is that you're passing Cell into the identifier self inside the function, so you're effectively saying

Cell.nav = {
    north = true,
    east = false,
    south = false,
    west = false,
}

This is because Dog:jump(height) is the same as Dog.jump(self,height) where Dog is passed into self.

However I believe what you intend to have is where each index in the grid self.grid[i] contains a table which itself contains a key 'nav' and a table containing the directions. If this is so, change the Cell:get_nav function to return the value like so:

if random == 1 then
return {
    north = true,
    east = false,
    south = false,
    west = false,
}

this will assign the table of directions into self.grid[i].nav, where as before it was assigning a nil value to self.grid[i].nav because:

function edit_my_param(f)
f = 3
end 

nav = edit_my_param(4)
print(nav)

sets the value nav to nil because the function doesn't return a value.

regards, henry

Sign up to request clarification or add additional context in comments.

Comments

0

In some cases you call it Cell[i] and in other cases grid[i] -- are you sure you are referring to the same table?

What does Cell:get_nav return?; the value it returns is put into self.Cell[i].nav so it may be redundant, and is certainly confusing to also set self.nav in Cell:get_nav

1 Comment

Thanks for the reply. I realized I had a typo in there. grid[i] refers to the actual grid cells. While Cell is calling a function, which should create a table at grid[i][4]. I return self.nav back to grid[i], but maybe thats where its overkill?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.