1

Below is the code i am trying to write. I am trying to dynamically assign objects to 2d array in lua. It is returning a error saying trying index a global nil value at line grid[i][j]=diceclass.new( ((i+2)/10),((j+2)/10)) ? Is there a way to fix this or what i am trying to do possible, dynamically assigning a object to each element of the array?

local diceClass = require( "dice" )
grid={}
for i =1,5 do
grid[i]=  {}
    for j =1,5 do

        grid[i][j]=diceclass.new( ((i+2)/10),((j+2)/10))
    end
end




--dice class
local dice = {}
local dice_mt = { __index = dice } 


function dice.new( posx, posy) 
a=math.random(1,6)
local newdice = display.newText(a, display.contentWidth*posx,
                                display.contentHeight*posy, nil, 60)

return setmetatable( newdice, dice_mt )
end



return dice
2
  • What is the problem? What is your question? Commented Jul 18, 2012 at 13:39
  • The problem i want to create a for loop which will create a 2d array of objects. At the line : grid[i][j]=diceclass.new( ((i+2)/10),((j+2)/10) i get a error saying trying to index global < a nil value> which is because i have not declared the objects beforehand. Is there a way to fix this error plus or to dynamically assign objects when creating a 2d array in lua? Commented Jul 18, 2012 at 13:59

1 Answer 1

3

diceClass is not the same variable as diceclass.

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

1 Comment

@David, a typo, not a syntax error. But the error message should have told you that: attempt to index global 'diceclass' (a nil value).

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.