I having troubles with this loadSprite function.
It says "Attempt to index field 'Down'"
Anyone can help?
[Help] Tables/Arrays
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Help] Tables/Arrays
- Attachments
-
- Client.love
- (4.42 KiB) Downloaded 91 times
Re: [Help] Tables/Arrays
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.
Re: [Help] Tables/Arrays
I fixed that.. but now its saying 'Direction' is a nil value?
Here's the updated love:
Here's the updated love:
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: [Help] Tables/Arrays
Well, you might want to know that, to Lua, there's a huge difference between these two assignments:
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:
world[char_name].Direction will be obviously nil. So this line should be replaced by:
Lines 77, 83, 89, 95:
All these variables will be nil, after assignments, as Up, Down, Left Right refers to variables not yet assigned. Should be replaced by:
Lines 104-110:
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:
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.
Code: Select all
myVar = Hello
print(myVar) --> nil
myVar = 'Hello'
print(myVar) --> 'Hello'
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 = {} }
Code: Select all
world[char_name] = { Direction = 'Down', Down = {}, Up = {}, Left = {}, Right = {} }
Code: Select all
...
v.Direction = Up
...
v.Direction = Down
...
v.Direction = Left
...
v.Direction = Right
...
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
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
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
Thankyou very much!
I know I'm not supposed to call it like that but I do anyways, it keeps it more organized.
I know I'm not supposed to call it like that but I do anyways, it keeps it more organized.
Who is online
Users browsing this forum: No registered users and 4 guests