[Help] Tables/Arrays

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

[Help] Tables/Arrays

Post by theobjpo »

I having troubles with this loadSprite function.
It says "Attempt to index field 'Down'"
Anyone can help?
Attachments
Client.love
(4.42 KiB) Downloaded 92 times
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: [Help] Tables/Arrays

Post 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.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

Re: [Help] Tables/Arrays

Post 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
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: [Help] Tables/Arrays

Post 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.
theobjpo
Prole
Posts: 15
Joined: Sat Sep 08, 2012 4:42 am

Re: [Help] Tables/Arrays

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 8 guests