Page 1 of 1

help with "expected userdata"

Posted: Sun Apr 15, 2012 3:24 pm
by BEANSFTW
Hi. I'm new to programing, love2d and lua. I have a problem with:
------------------------------------------------------------------------------
Error

main.lua:89: Incorrect parameter type: expected userdata.

Traceback

[C]: in function 'draw'
main.lua:89: in function 'draw'
[C]: in function 'xpcall'
-----------------------------------------------------------------------------------
Whats userdata? Help would be great :crazy:

here's my code:

main.lua:

Code: Select all

require "player"

function love.load()

	
  
	player = love.graphics.newImage("assets/player.png")
 
	playerx = 270
  
	playery = 420
  
  
  
	-- music
	music = love.audio.newSource( "assets/music.ogg" , "steam")
	music:setLooping(true)
	love.audio.play(music)
  
	Cloudx = 0
  
    yCloud = 128
  
	cloudimage = ("assets/cloud.gif")
	bullet = ("assets/bullet.gif")
  
	
end


function love.update(dt)
 


	Cloudx =  Cloudx + 32*dt 
	if Cloudx >= (900 + 278) then
		Cloudx = 0
	end

	
	
	if love.keyboard.isDown("d") then
		playerx = playerx + 275*dt
		player = love.graphics.newImage("assets/player.png")
	end	

	if love.keyboard.isDown("s") then
		playery = playery + 675*dt
	end
	
	
	
	
	if love.keyboard.isDown("a") then
		player = love.graphics.newImage("assets/playerback.png")
		playerx = playerx - 275*dt
	end	
	
	
	if playery > 420 then
		playery = 420
	end	
	
	
	
	
end

function love.draw()

	
	
	
  love.graphics.setColor(40,255,40)
  love.graphics.rectangle("fill",
    0,0,love.graphics.getWidth(),love.graphics.getHeight())
  love.graphics.setColor(255,255,255)
  
  
  
	love.graphics.setColor(111,183,255)
	love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),450)
	love.graphics.setColor(255,255,255)
	
	
	
	love.graphics.draw(cloudimage, Cloudx - 278, 128,278)
	
	
	love.graphics.draw(player, playerx, playery)
end
player.lua:

Code: Select all

playerxy = {}
    x = 0
	y = 0

Re: help with "expected userdata"

Posted: Sun Apr 15, 2012 4:51 pm
by Giggzaw
Hi!

I only started yesterday with lua and love so i could be wrong.
I think the mistake is that you gave it a string instead of an image at line 89 (when you try drawing the cloud).

Probably you should change the cloudimage variable to:

Code: Select all

cloudimage = love.graphics.newImage("assets/cloud.gif")
As I said anyway, I could be wrong :D

Re: help with "expected userdata"

Posted: Sun Apr 15, 2012 5:12 pm
by Nixola
Giggzaw, you're right. Beans, you should do the same with 'bullet'

Re: help with "expected userdata"

Posted: Sun Apr 15, 2012 7:58 pm
by tentus
By the way, if you ever notice love.graphics.newImage inside of love.update, you've made a mistake (a very bad one). If I hold down D in your game, my computer has to open a file, read it, process it, and assign it to a variable, EVERY. SINGLE. FRAME.

The proper way to do it is to pre-load all your images in love.load, and then point the drawing variable at the proloaded images. A short example:

Code: Select all

function love.load()
   left = love.graphics.newImage("assets/left.png")
   right = love.graphics.newImage("assets/right.png")
   player = left
end
function love.update(dt)
   if love.keyboard.isDown("a") then
      player = left
   end
   if love.keyboard.isDown("d") then
      player = right
   end
end
function love.draw()
   love.graphics.draw(player, 5, 5)
end

Re: help with "expected userdata"

Posted: Sun Apr 15, 2012 8:48 pm
by BEANSFTW
THANKS!