Page 1 of 1

problem with tables

Posted: Sat Oct 13, 2012 1:14 am
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

Re: problem with tables

Posted: Sat Oct 13, 2012 6:01 am
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

Re: problem with tables

Posted: Sat Oct 13, 2012 5:45 pm
by Squaar
Awesome! I got it working. Thanks!

Any tips on making the code less baffling?

Re: problem with tables

Posted: Sat Oct 13, 2012 11:18 pm
by Helvecta
Nope! It's not that your code is bad, it's just that I'm not particularly good at programming. :rofl:

Re: problem with tables

Posted: Sun Oct 14, 2012 2:54 am
by Squaar
Oh! lol okay