Trying to use metatables, nothing is drawing

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
andrsrz
Prole
Posts: 1
Joined: Sat Jun 26, 2021 5:34 pm

Trying to use metatables, nothing is drawing

Post by andrsrz »

Hello everyone. So as the title says I'm trying to implement classes using metatables. The problem comes when I'm drawing these objects onto the screen it draws nothing.

These is my player class.

Code: Select all

local world = require('world')
local constant = require('constant')
local input = require('input')
local Laser = require('entities/laser')

local Player = {}

function Player:new(img)
	local player = {}
	player.speed = 600
	player.pos = { x = 0, y = 0 }
	player.dimension = { width = 0, height = 0 }
	-- We need to set these default tables or lua gets picky
	player.lasers = {}

	player.img = img
	-- The values are from the XML file of the spritesheet
	player.quad = love.graphics.newQuad(211, 941, 99, 75, player.img:getDimensions())
	-- These are the initial player position, do NOT use later. Use body positon.
	player.pos.x, player.pos.y, player.dimension.width, player.dimension.height = player.quad:getViewport()
	player.pos.x = love.graphics.getWidth() / constant.HALF
	player.pos.y = love.graphics.getHeight() * constant.PLAYER_POS_Y

	player.body = love.physics.newBody(world, player.pos.x, player.pos.y, 'dynamic')
	player.body:setMass(100)
	player.shape = love.physics.newRectangleShape(player.dimension.width, player.dimension.height)
	player.fixture = love.physics.newFixture(player.body, player.shape)
	player.fixture:setUserData(player)

	setmetatable(player, Player)
end

function Player:shoot(self)
	local laser = Laser:new(self.img, self.body:getX(), self.body:getY() - self.dimension.height)
	table.insert(self.lasers, laser)
end

function Player:draw(self)
	-- Hit box
	love.graphics.polygon('line', self.body:getWorldPoints(self.shape:getPoints()))
	-- Sprite
	love.graphics.draw(
		self.img,
		self.quad,
		self.body:getX(),
		self.body:getY(),
		0,
		1,
		1,
		self.dimension.width / constant.HALF,
		self.dimension.height / constant.HALF
	)

	for i, laser in ipairs(self.lasers) do
		laser:draw()
	end
end

function Player:update(self)
	-- Don't move if both keys are been pressed
	if input.buttonLeft and input.buttonRight then
		return
	end

	-- Move left or right
	if input.buttonLeft then
		self.body:setLinearVelocity(-self.speed, 0)
	elseif input.buttonRight then
		self.body:setLinearVelocity(self.speed, 0)
	else
		self.body:setLinearVelocity(0, 0)
	end

	for i, laser in ipairs(self.lasers) do
		if laser.dead then
			laser.fixture:destroy()
			laser.body:destroy()
			table.remove(self.lasers, i)
		else
			laser:update()
		end
	end

	if input.buttonUp then
		self:shoot()
	end
end

return Player
This is my entities file

Code: Select all

local spritesheet = require('entities/spritesheet')
local BoundaryVertical = require('entities/boundary-vertical')
local BoundaryHorizontal = require('entities/boundary-horizontal')
local Player = require('entities/player')

return function()
	local entities = {
		BoundaryVertical:new(-1),
		BoundaryVertical:new(love.graphics.getWidth() + 1),
		BoundaryHorizontal:new(-150),
		BoundaryHorizontal:new(love.graphics.getHeight() + 1),
		Player:new(spritesheet.img)
	}

	return entities
end
So in the main file I'm drawing these entities, however they're not drawing. When I try to iterate through the entities list it says that is nil. Any idea why? If you need any more information let me know please.
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: Trying to use metatables, nothing is drawing

Post by applebappu »

I just got done wrestling with this myself, actually. (see topic: https://love2d.org/forums/viewtopic.php?f=4&t=91635)

In your case, however, you're not returning anything from your constructor. You might want to try having "return player" after you setmetatable in your "new" function. Otherwise, you're just saying that all these values equal other things, but then not sending the resulting object anywhere.

I would also recommend a bit of a refactor, to get yourself out of the duplication hell you're headed for, haha. You can actually move all those "player.foo" lines inside your initial "player" table, and not have to keep typing "player." every time, if you want. The methods can go in there too, actually. But you do you.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 135 guests