Incorrect parameter, expected userdata

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
kurtss
Prole
Posts: 6
Joined: Wed Nov 23, 2011 4:37 pm

Incorrect parameter, expected userdata

Post 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!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Incorrect parameter, expected userdata

Post 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
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Incorrect parameter, expected userdata

Post 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.
kurtss
Prole
Posts: 6
Joined: Wed Nov 23, 2011 4:37 pm

Re: Incorrect parameter, expected userdata

Post by kurtss »

Thanks tons! Indenting helped, and now I see where I went wrong.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Incorrect parameter, expected userdata

Post 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:

Code: Select all

table = {
  a = 100,
  b = a + 50
}
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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests