Page 1 of 1
Incorrect parameter, expected userdata
Posted: Thu Nov 24, 2011 2:30 am
by kurtss
Sorry about another topic again, here's another problem I have.
Here's my love.load function:
Code: Select all
function love.load()
player = {
sprite = love.graphics.newImage("guy.png"),
spriteBatch = love.graphics.newSpriteBatch(sprite, 3),
standing = love.graphics.newQuad(0, 0, 0, 0, 20, 40),
height = 40,
width = 20,
x = gameWidth / 2,
y = gameHeight - 100,
y_velocity = 0,
inAir = false,
}
gravity = 400
jump_height = 300
end
But when I test it, spriteBatch = love.graphics.newSpriteBatch(sprite, 3), returns "incorrect parameter, expected userdata."
What seems to be the problem? I've been trying to figure this out for a while.
Thanks!
Re: Incorrect parameter, expected userdata
Posted: Thu Nov 24, 2011 3:21 am
by Taehl
The problem is, the variable sprite is nil since you never make it equal anything. Look more closely at your code. That line that says "sprite" is referring to player.sprite, since it's in the player table.
Indenting your code for table contents makes this sort of thing much easier to spot:
Code: Select all
function love.load()
player = {
sprite = love.graphics.newImage("guy.png"),
spriteBatch = love.graphics.newSpriteBatch(sprite, 3),
standing = love.graphics.newQuad(0, 0, 0, 0, 20, 40),
height = 40,
width = 20,
x = gameWidth / 2,
y = gameHeight - 100,
y_velocity = 0,
inAir = false,
}
gravity = 400
jump_height = 300
end
Re: Incorrect parameter, expected userdata
Posted: Thu Nov 24, 2011 3:22 am
by Jasoco
Because "sprite" doesn't exist. And "player.sprite" won't exist until the table is closed. You need to create the SpriteBatch after defining the table by referring to "player.sprite" instead of just sprite.
Re: Incorrect parameter, expected userdata
Posted: Thu Nov 24, 2011 3:36 am
by kurtss
Thanks tons! Indenting helped, and now I see where I went wrong.
Re: Incorrect parameter, expected userdata
Posted: Thu Nov 24, 2011 3:50 am
by Jasoco
You know, I made the same mistake when I started too. It took me a bit to figure it out. Just remember to never try and refer to yourself from within yourself. If you need to refer to yourself, do it after you've been completed.
Where yourself is referring to a table.
Incorrect:
Correct:
Code: Select all
table = {
a = 100,
b
}
table.b = table.a + 50
print(table.b)
> 150
You get what I mean. If you try to refer to yourself before you've been closed off with }, you don't actually exist yet.