Page 1 of 1

[Help] Tables/Arrays

Posted: Mon Sep 17, 2012 2:14 am
by theobjpo
I having troubles with this loadSprite function.
It says "Attempt to index field 'Down'"
Anyone can help?

Re: [Help] Tables/Arrays

Posted: Mon Sep 17, 2012 5:34 am
by Lafolie
I'm too tired to figure it all out right now (about to head to bed), but I discerned that line 94 is all wrong. You're treating v2 as if it was a table, when actually it is a quad. This is demonstrated by the print statement you make 2 lines earlier.

Re: [Help] Tables/Arrays

Posted: Tue Sep 18, 2012 1:24 am
by theobjpo
I fixed that.. but now its saying 'Direction' is a nil value?
Here's the updated love:
Game.love
(4.4 KiB) Downloaded 204 times

Re: [Help] Tables/Arrays

Posted: Tue Sep 18, 2012 8:02 am
by Roland_Yonaba
Well, you might want to know that, to Lua, there's a huge difference between these two assignments:

Code: Select all

myVar = Hello
print(myVar) --> nil

myVar = 'Hello'
print(myVar) --> 'Hello'
First time, myVar will be nil, as we assign to myVar another variable named Hello, which vasn't instantiated before, so both Hello and myVar are nil.
Second time we assign to myVar a string 'Hello', and then we print it.

If you already knew that, my apologies, but that's the core of your problem, here.

Line 14:

Code: Select all

 world[char_name] = { Direction = Down, Down = {}, Up = {}, Left = {}, Right = {} }
world[char_name].Direction will be obviously nil. So this line should be replaced by:

Code: Select all

world[char_name] = { Direction = 'Down', Down = {}, Up = {}, Left = {}, Right = {} }
Lines 77, 83, 89, 95:

Code: Select all

...
v.Direction = Up
...
v.Direction = Down
...
v.Direction = Left
...
v.Direction = Right
...
All these variables will be nil, after assignments, as Up, Down, Left Right refers to variables not yet assigned. Should be replaced by:

Code: Select all

...
v.Direction = 'Up'
...
v.Direction = 'Down'
...
v.Direction = 'Left'
...
v.Direction = 'Right'
...

Lines 104-110:

Code: Select all

for k, v in pairs(world) do -- Direction...
  if v.still then
    love.graphics.drawq(v.sprite, v.Direction[1], v.x, v.y)
  else
    local weit = math.floor(wait)
    love.graphics.drawq(v.sprite, v.Direction[weit], v.x, v.y)
end
You're just indexing the passing a string to love.graphics.drawq instead of an expected quad (because v.Direction refers to a string).
What you need here is indexing the field v[v.Direction]. So replace it with:

Code: Select all

for k, v in pairs(world) do -- Direction...
  if v.still then
    love.graphics.drawq(v.sprite, v[v.Direction][1], v.x, v.y) 
  else
    local weit = math.floor(wait)
    love.graphics.drawq(v.sprite, v[v.Direction][weit], v.x, v.y)
end
Hope this helps.

EDIT: Forgot to mention that I don't think you're supposed to call love.keypressed the way you do. That's a callback.

Re: [Help] Tables/Arrays

Posted: Tue Sep 18, 2012 9:06 pm
by theobjpo
Thankyou very much!
I know I'm not supposed to call it like that but I do anyways, it keeps it more organized.