problem with tables

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
Squaar
Prole
Posts: 6
Joined: Sat Oct 13, 2012 1:05 am

problem with tables

Post by Squaar »

Hey so i'm fairly new to love and lua but I have experience with java and C++. I'm trying to make an asteroids type game and i'm having trouble making the gun work. The error i'm getting is:

Code: Select all

Error

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


Traceback

[C]: in function 'getX'
main.lua:43: in function 'player_update'
main.lua:21: in function 'update'
[C]: in function 'xpcall'
my code is:

Code: Select all

function love.load()
love.physics.setMeter(100)
	world = love.physics.newWorld(0,0,true)
	ship = love.graphics.newImage("ship.png")
	bullet = love.graphics.newImage("bullet.png")
	
	player = {}
		player.body = love.physics.newBody(world, love.graphics.getWidth()/2, love.graphics.getHeight()/2, "dynamic")
		player.shape = love.physics.newRectangleShape(34, 44)
		player.fixture = love.physics.newFixture(player.body, player.shape)
		player.accel = 75
		player.turnSpeed = 15
		player.direction = 0.5 * math.pi
	
	bullets = { }
end

function love.update(dt)
	world:update(dt)
	player_update(dt)
	bullets_update(dt)
end

function player_update(dt)
	--turning
	if love.keyboard.isDown("right") then
		player.direction = player.direction + (dt * player.turnSpeed)
	elseif love.keyboard.isDown("left") then
		player.direction = player.direction - (dt * player.turnSpeed)
	end
	
	--forward and back
	if love.keyboard.isDown("up") then
		player.body:applyForce(player.accel * -math.cos(player.direction), player.accel * -math.sin(player.direction))
	elseif love.keyboard.isDown("down") then
		player.body:applyForce(player.accel * math.cos(player.direction), player.accel * math.sin(player.direction))
	end
	
	--shoot bullets
	if love.keyboard.isDown(" ") then 
		table.insert(bullets, {})
			bullets[#bullets].body = love.physics.newBody(world, player.body.getX(), player.body.getY(), "dynamic")
			bullets[#bullets].shape = love.physics.newRectangleShape(5, 20)
			bullets[#bullets].fixture = love.physics.newFixture(bullets[#bullets].body, bullets[#bullets].shape)
			bullets[#bullets].speed = player.body:getLinearVelocity() + 100
			bullets[#bullets].direction = player.direction
	end
	
	--screen looping
	if player.body:getX() <0 then
		player.body:setPosition(love.graphics.getWidth(), player.body:getY())
	elseif player.body:getX() > love.graphics.getWidth() then
		player.body:setPosition(0, player.body:getY())
	end
	if player.body:getY() <0 then
		player.body:setPosition(love.graphics.getHeight(), player.body:getX())
	elseif player.body:getY() > love.graphics.getHeight() then
		player.body:setPosition(0, player.body:getX())
	end
end

--update bullets
function bullets_update(dt)
	for i, o in ipairs(bullets) do
		o.body:applyForce(o.speed + math.cos(o.direction), o.speed + math.sin(o.direction))
		--check for out of bounds
        if (o.x < -10) or (o.x > love.graphics.getWidth() + 10) or (o.y < -10) or (o.y > love.graphics.getHeight() + 10) then
            table.remove(bullets, i)
        end
    end
end

--draw everything on the screen;
function love.draw()
	love.graphics.draw(ship, player.body:getX(), player.body:getY(), player.direction - 0.5 * math.pi, 1, 1, 17, 22)
	for i, o in ipairs(bullets) do
		love.graphics.draw(bullet, o.body.getX(), o.body.getY(), o.direction - 0.5 * math.pi, 1, 1, 3, 3)
	end
end
Any help would definitely be appreciated
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: problem with tables

Post by Helvecta »

Heya! Not a big problem, just change:

Code: Select all

bullets[#bullets].body = love.physics.newBody(world, player.body.getX(), player.body.getY(), "dynamic")
to:

Code: Select all

  bullets[#bullets].body = love.physics.newBody(world, player.body:getX(), player.body:getY(), "dynamic")
Note the colons. This leads into another crash but that's unrelated to this specific problem, and your code baffles me as it is. Someone can surely help you from here, but either way, this is a start! I hope I helped. :3
"Bump." -CMFIend420
Squaar
Prole
Posts: 6
Joined: Sat Oct 13, 2012 1:05 am

Re: problem with tables

Post by Squaar »

Awesome! I got it working. Thanks!

Any tips on making the code less baffling?
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: problem with tables

Post by Helvecta »

Nope! It's not that your code is bad, it's just that I'm not particularly good at programming. :rofl:
"Bump." -CMFIend420
Squaar
Prole
Posts: 6
Joined: Sat Oct 13, 2012 1:05 am

Re: problem with tables

Post by Squaar »

Oh! lol okay
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests