Error With Object Oriented Things

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.
User avatar
xlr8877
Prole
Posts: 11
Joined: Fri Mar 23, 2018 2:45 pm

Error With Object Oriented Things

Post by xlr8877 »

I'm fairly new to LUA and LOVE2D both.
I've been getting this for a while and need to finish it to continue.
The code is bellow.

Image

main.lua:

Code: Select all

camera = {}
camera.x = 0
camera.y = 0
camera.scaleX = 1
camera.scaleY = 1
camera.rotation = 0

function camera:set()
  love.graphics.push()
  love.graphics.rotate(-self.rotation)
  love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
  love.graphics.translate(-self.x, -self.y)
end

function camera:unset()
  love.graphics.pop()
end

function camera:move(dx, dy)
  self.x = self.x + (dx or 0)
  self.y = self.y + (dy or 0)
end

function camera:rotate(dr)
  self.rotation = self.rotation + dr
end

function camera:scale(sx, sy)
  sx = sx or 1
  self.scaleX = self.scaleX * sx
  self.scaleY = self.scaleY * (sy or sx)
end

function camera:setPosition(x, y)
  self.x = x or self.x
  self.y = y or self.y
end

function camera:setScale(sx, sy)
  self.scaleX = sx or self.scaleX
  self.scaleY = sy or self.scaleY
end

function love.load()

	setup = {
		placeholder = 0
	}
	player = {
		grid_x = 672,
		grid_y = 160,
		act_x = 200,
		act_y = 200,
		speed = 10,
		canmove = false,
		movetimer = 0,
		health = 640,
		maxhealth = 640,
		blink = 300,
		mana = 640,
		maxmana = 640,
		spell = 0,
		sptrturns = 0
	}
	map = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 0, 0, 5, 5, 5, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 1, 5, 5, 5, 5, 1, 5, 5, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
	}

    Object = require "classic"

    require "monster"

    

	goblin = Monster(544, 192, 50, 160)
    --troll = Monster(608, 384, 300, 250)
    
	love.window.setMode(640, 640)
end
 
function love.update(dt)
	if player.blink > 0 then
		player.blink = player.blink - 1
	end
	if player.blink < 1 then
		player.blink = 300
	end
	if player.health < 0 then
		player.health = 0
	end
	camera.x = (player.act_x + 16) - 320
	camera.y = (player.act_y + 16) - 320
	if player.movetimer < 60 then
		player.movetimer = player.movetimer + 1
	end
	if player.movetimer == 60 then
		player.canmove = true
		player.movetimer = 0
	end
	if player.movetimer == 40 then
		player.canmove = false
	end

	player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
	player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)

	goblin:update(dt)
	--troll:update(dt)
end
 
function love.draw()
	camera:set()
	for y=1, #map do
		for x=1, #map[y] do
			if map[y][x] == 0 then
				love.graphics.setColor(150, 150, 150)
				love.graphics.line(x * 32, y * 32 + 8, x * 32 + 32, y * 32 + 8)
				love.graphics.line(x * 32, y * 32 + 16, x * 32 + 32, y * 32 + 16)
				love.graphics.line(x * 32, y * 32 + 24, x * 32 + 32, y * 32 + 24)
				love.graphics.line(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32)
				love.graphics.line(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 8)
				love.graphics.line(x * 32 + 16, y * 32 + 16, x * 32 + 16, y * 32 + 24)
				love.graphics.line(x * 32, y * 32 + 8, x * 32, y * 32 + 16)
				love.graphics.line(x * 32 + 32, y * 32 + 8, x * 32 + 32, y * 32 + 16)
				love.graphics.line(x * 32, y * 32 + 24, x * 32, y * 32 + 32)
				love.graphics.line(x * 32 + 32, y * 32 + 24, x * 32 + 32, y * 32 + 32)
			end
			if map[y][x] == 1 then
				love.graphics.setColor(255, 255, 255)
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
				love.graphics.rectangle("line", x * 32 + 8, y * 32 + 8, 16, 16)
			end
			if map[y][x] == 4 then
				love.graphics.setColor(255, 255, 255)
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
				love.graphics.line(x * 32, y * 32, x * 32 + 32 , y * 32 + 32)
				love.graphics.line(x * 32 + 32, y * 32, x * 32, y * 32 + 32)
			end
			if map[y][x] == 5 then
				love.graphics.setColor(255, 255, 255)
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
				love.graphics.line(x * 32, y * 32 + 16, x * 32 + 32 , y * 32 + 16)
			end
			if map[y][x] == 6 then
				love.graphics.setColor(255, 255, 255)
				love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
				love.graphics.line(x * 32, y * 32 + 16, x * 32 + 16 , y * 32 + 32, x * 32 + 32, y * 32 + 16, x * 32 + 16, y * 32, x * 32, y * 32 + 16)
			end
		end
	end
	if player.movetimer > 25 then
		love.graphics.setColor(0, 255, 255)
	end
	if player.movetimer < 26 then
		love.graphics.setColor(255, 0, 0)
	end
	if player.health < 1 then
		love.graphics.setColor(0, 255, 255)
	end
	love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
	if player.blink > 199 and player.blink < 221 and player.health > 0 then
		-- Dummy code!
	else 
		if player.health > 0 then
			love.graphics.setColor(255, 255, 255)
		end
		if player.health < 1 then
			love.graphics.setColor(0, 0, 0)
		end
		love.graphics.rectangle("fill", player.act_x + 4, player.act_y + 8, 8, 4)
		love.graphics.rectangle("fill", player.act_x + 20, player.act_y + 8, 8, 4)
		love.graphics.setColor(0, 0, 0)
		love.graphics.rectangle("fill", player.act_x + 4, player.act_y + 8, 2, 2)
		love.graphics.rectangle("fill", player.act_x + 20, player.act_y + 8, 2, 2)
	end

	goblin:draw()

	camera:unset()
	love.graphics.setColor(75, 75, 75)
	love.graphics.rectangle("fill", 0, 0, player.maxhealth, 32)
	love.graphics.setColor(200, 0, 0)
	love.graphics.rectangle("fill", 0, 0, player.health, 32)
	love.graphics.setColor(75, 75, 75)
	love.graphics.rectangle("fill", 0, 32, player.maxmana, 16)
	love.graphics.setColor(0, 0, 200)
	love.graphics.rectangle("fill", 0, 32, player.mana, 16)
end
 
function love.keypressed(key)
	if key == "0" then
		player.spell = 0
	end
	if key == "1" then
		player.spell = 1
	end
	if key == "2" then
		player.spell = 2
	end
	if key == "3" then
		player.spell = 3
	end
	if key == "4" then
		player.spell = 4
	end
	if player.health > 0 then
		if key == "w" and player.canmove == true then
			if player.spell == 1 and testMap(0, -1) and player.mana > 39 then
				map[(player.grid_y / 32 - 1)][(player.grid_x / 32)] = 1
				player.mana = player.mana - 40
				player.canmove = false
			end
			if player.spell == 2 and testMap(0, -1) and player.mana > 79 then
				map[(player.grid_y / 32 - 1)][(player.grid_x / 32)] = 5
				player.mana = player.mana - 80
				player.canmove = false
			end
			if player.spell == 3 and testBuilt(0, -1) and player.mana > 4 then
				map[(player.grid_y / 32 - 1)][(player.grid_x / 32)] = 0
				player.mana = player.mana - 5
				player.canmove = false
			end
			if player.spell == 4 and player.mana > 24 then
				player.sptrturns = 10
				player.mana = player.mana - 125
				player.canmove = false
			end
		elseif key == "s" and player.canmove == true then
			if player.spell == 1 and testMap(0, 1) and player.mana > 39 then
				map[(player.grid_y / 32 + 1)][(player.grid_x / 32)] = 1
				player.mana = player.mana - 40
				player.canmove = false
			end
			if player.spell == 2 and testMap(0, 1) and player.mana > 79 then
				map[(player.grid_y / 32 + 1)][(player.grid_x / 32)] = 5
				player.mana = player.mana - 80
				player.canmove = false
			end
			if player.spell == 3 and testBuilt(0, 1) and player.mana > 4 then
				map[(player.grid_y / 32 + 1)][(player.grid_x / 32)] = 0
				player.mana = player.mana - 5
				player.canmove = false
			end
			if player.spell == 4 and player.mana > 24 then
				player.sptrturns = 10
				player.mana = player.mana - 125
				player.canmove = false
			end
		elseif key == "a" and player.canmove == true then
			if player.spell == 1 and testMap(-1, 0) and player.mana > 39 then
				map[(player.grid_y / 32)][(player.grid_x / 32 - 1)] = 1
				player.mana = player.mana - 40
				player.canmove = false
			end
			if player.spell == 2 and testMap(-1, 0) and player.mana > 79 then
				map[(player.grid_y / 32)][(player.grid_x / 32 - 1)] = 5
				player.mana = player.mana - 80
				player.canmove = false
			end
			if player.spell == 3 and testBuilt(-1, 0) and player.mana > 4 then
				map[(player.grid_y / 32)][(player.grid_x / 32 - 1)] = 0
				player.mana = player.mana - 5
				player.canmove = false
			end
			if player.spell == 4 and player.mana > 24 then
				player.sptrturns = 10
				player.mana = player.mana - 125
				player.canmove = false
			end
		elseif key == "d" and player.canmove == true then
			if player.spell == 1 and testMap(1, 0) and player.mana > 39 then
				map[(player.grid_y / 32)][(player.grid_x / 32 + 1)] = 1
				player.mana = player.mana - 40
				player.canmove = false
			end
			if player.spell == 2 and testMap(1, 0) and player.mana > 79 then
				map[(player.grid_y / 32)][(player.grid_x / 32 + 1)] = 5
				player.mana = player.mana - 80
				player.canmove = false
			end
			if player.spell == 3 and testBuilt(1, 0) and player.mana > 4 then
				map[(player.grid_y / 32)][(player.grid_x / 32 + 1)] = 0
				player.mana = player.mana - 5
				player.canmove = false
			end
			if player.spell == 4 and player.mana > 24 then
				player.sptrturns = 10
				player.mana = player.mana - 125
				player.canmove = false
			end
		elseif key == "up" and testMap(0, -1) and player.canmove == true then
			if testTrap(0, -1) then
				player.health = player.health - 10
				map[(player.grid_y / 32 - 1)][(player.grid_x / 32)] = 0
			end
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 0
			player.grid_y = player.grid_y - 32
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 3
			if player.sptrturns > 0 then
				map[(player.grid_y / 32 + 1)][(player.grid_x / 32)] = 4
				player.sptrturns = player.sptrturns - 1
			end
			player.canmove = false
		elseif key == "down" and testMap(0, 1) and player.canmove == true then
			if testTrap(0, 1) then
				player.health = player.health - 10
				map[(player.grid_y / 32) + 1][(player.grid_x / 32)] = 0
			end
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 0
			player.grid_y = player.grid_y + 32
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 3
			if player.sptrturns > 0 then
				map[(player.grid_y / 32 - 1)][(player.grid_x / 32)] = 4
				player.sptrturns = player.sptrturns - 1
			end
			player.canmove = false
		elseif key == "left" and testMap(-1, 0) and player.canmove == true then
			if testTrap(-1, 0) then
				player.health = player.health - 10
				map[(player.grid_y / 32)][(player.grid_x / 32 - 1)] = 0
			end
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 0
			player.grid_x = player.grid_x - 32
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 3
			if player.sptrturns > 0 then
				map[(player.grid_y / 32)][(player.grid_x / 32 + 1)] = 4
				player.sptrturns = player.sptrturns - 1
			end
			player.canmove = false
		elseif key == "right" and testMap(1, 0) and player.canmove == true then
			if testTrap(1, 0) then
				player.health = player.health - 10
				map[(player.grid_y / 32)][(player.grid_x / 32 + 1)] = 0
			end
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 0
			player.grid_x = player.grid_x + 32
			emap[(player.grid_y / 32)][(player.grid_x / 32)] = 3
			if player.sptrturns > 0 then
				map[(player.grid_y / 32)][(player.grid_x / 32 - 1)] = 4
				player.sptrturns = player.sptrturns - 1
			end
			player.canmove = false
		elseif key == "up" and player.canmove == false then
			player.health = player.health - 40
		elseif key == "down" and player.canmove == false then
			player.health = player.health - 40
		elseif key == "left" and player.canmove == false then
			player.health = player.health - 40
		elseif key == "right" and player.canmove == false then
			player.health = player.health - 40
		elseif key == "space" and player.canmove == true then
			if testEnemy(1, 0) then
				if goblin:TestPlayer(-1, 0) then
					goblin.health = goblin.health - 20
					if goblin:TestMap(1, 0) then
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 0
						goblin.grid_x = goblin.grid_x + 32
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 2
					end
				end
			end
			if testEnemy(-1, 0) then
				if goblin:TestPlayer(1, 0) then
					goblin.health = goblin.health - 20
					if goblin:TestMap(-1, 0) then
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 0
						goblin.grid_x = goblin.grid_x - 32
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 2
					end
				end
			end
			if testEnemy(0, 1) then
				if goblin:TestPlayer(0, -1) then
					goblin.health = goblin.health - 20
					if goblin:TestMap(0, 1) then
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 0
						goblin.grid_y = goblin.grid_y + 32
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 2
					end
				end
			end
			if testEnemy(0,-1) then
				if goblin:TestPlayer(0, 1) then
					goblin.health = goblin.health - 20
					if goblin:TestMap(0, -1) then
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 0
						goblin.grid_y = goblin.grid_y - 32
						emap[(goblin.grid_y / 32)][(goblin.grid_x / 32)] = 2
					end
				end
			end
			player.canmove = false
		elseif key == "space" and player.canmove == false then
			player.health = player.health - 40
		end
	end
end
 
function testMap(x, y)
	if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
		return false
	end
	if emap[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 2 then
		return false
	end
	if emap[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 3 then
		return false
	end
	if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 5 then
		return false
	end
	return true
end
function testEnemy(x, y)
	if emap[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 2 then
		return true
	end
	return false
end
function testTrap(x, y)
	if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 4 then
		return true
	end
	return false
end
function testBuilt(x, y)
	if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 5 then
		return true
	end
	return false
end
monster.lua:

Code: Select all

Monster = Object:extend()

function Monster:new(grid_x, grid_y, health, damage)
    self.grid_x = grid_x
    self.grid_y = grid_y
    self.act_x = grid_x
    self.act_y = grid_y
    self.speed = 10
	self.maxhealth = health
	self.health = health
	self.damage = damage
	self.trapdamage = 30
end

function Monster:update(dt)
    if self.health < 0 then
		self.health = 0
	end
	if self.health > self.maxhealth then
		self.health = self.maxhealth
	end
	if player.movetimer == 10 and self.health > 0 then
		if player.grid_x > self.grid_x and self.TestMap(1, 0) then
			if self.TestTrap(1, 0) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32 + 1)][(self.grid_x / 32)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_x = self.grid_x + 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		elseif player.grid_x < self.grid_x and self.TestMap(-1, 0) then
			if self.TestTrap(-1, 0) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32 - 1)][(self.grid_x / 32)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_x = self.grid_x - 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		elseif player.grid_y > self.grid_y and self.TestMap(0, 1) then
			if self.TestTrap(0, 1) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32)][(self.grid_x / 32 + 1)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_y = self.grid_y + 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		elseif player.grid_y < self.grid_y and self.TestMap(0, -1) then
			if self.TestTrap(0, -1) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32)][(self.grid_x / 32 - 1)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_y = self.grid_y - 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		else
			if self.TestPlayer(1, 0) then
				player.health = player.health - self.damage
			end
			if self.TestPlayer(-1, 0) then
				player.health = player.health - self.damage
			end
			if self.TestPlayer(0, 1) then
				player.health = player.health - self.damage
			end
			if self.TestPlayer(0, -1) then
				player.health = player.health - self.damage
			end
		end
	end
	self.act_y = self.act_y - ((self.act_y - self.grid_y) * self.speed * dt)
	self.act_x = self.act_x - ((self.act_x - self.grid_x) * self.speed * dt)
end

function Monster:draw()
    love.graphics.setColor(255, 0, 0)
	love.graphics.rectangle("fill", self.act_x, self.act_y, 32, 32)
	if self.health > 0 then
		love.graphics.setColor(255, 200, 0)
	end
	if self.health < 1 then
		love.graphics.setColor(0, 0, 0)
	end
	love.graphics.rectangle("fill", self.act_x + 4, self.act_y + 8, 8, 4)
	love.graphics.rectangle("fill", self.act_x + 20, self.act_y + 8, 8, 4)
end

function Monster:TestMap(x, y)
	if clone.map[self:grid_x / 32 + x][self:grid_y / 32 + y] == 1 then
		return false
	end
	if clone.emap[self:grid_x / 32 + x][self:grid_y / 32 + y] == 2 then
		return false
	end
	if clone.emap[self:grid_x / 32 + x][self:grid_y / 32 + y] == 3 then
		return false
	end
	if clone.map[self:grid_x / 32 + x][self:grid_y / 32 + y] == 5 then
		return false
	end
	return true
end
function Monster:TestPlayer(x, y)
	if emap[self.grid_x / 32 + x][self.grid_y / 32 + y] == 3 then
		return true
	end
	return false
end
function Monster:TestTrap(x, y)
	if emap[self.grid_x / 32 + x][self.grid_y / 32 + y] == 4 then
		return true
	end
	return false
end
Obviously the classic.lua is in the .love as well.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Error With Object Oriented Things

Post by pedrosgali »

I think it's because you use self:grid_x instead of self.grid_x. The colon syntax is used for function calls so by putting the colon you are stating that you are calling a function and you'd like 'self' to be passed as the first argument. This is why it expecting a function call.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Error With Object Oriented Things

Post by Sir_Silver »

What pedrosgali says is true. As a side note, it would be much easier to troubleshoot if you upload a .love file, because trying to figure out where line 88 is is difficult.
User avatar
xlr8877
Prole
Posts: 11
Joined: Fri Mar 23, 2018 2:45 pm

Re: Error With Object Oriented Things

Post by xlr8877 »

I think the .love is in this reply, hope it helps.
z.love
(3.36 KiB) Downloaded 355 times
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Error With Object Oriented Things

Post by Sir_Silver »

Thank you, that made it much easier to troubleshoot: As pedrosgali said, the problem was with the use of ":" where a "." was needed. So you need to change your Monster.TestMap function to this:

Code: Select all

function Monster:TestMap(x, y)
	if clone.map[self.grid_x / 32 + x][self.grid_y / 32 + y] == 1 then
		return false
	end
	if clone.emap[self.grid_x / 32 + x][self.grid_y / 32 + y] == 2 then
		return false
	end
	if clone.emap[self.grid_x / 32 + x][self.grid_y / 32 + y] == 3 then
		return false
	end
	if clone.map[self.grid_x / 32 + x][self.grid_y / 32 + y] == 5 then
		return false
	end
	return true
end
This will solve the issue you had, but you have another problem. You never actually created a table called clone anywhere, so this program does eventually crash, but that's a whole different problem you'll need to sort out yourself.
User avatar
xlr8877
Prole
Posts: 11
Joined: Fri Mar 23, 2018 2:45 pm

Re: Error With Object Oriented Things

Post by xlr8877 »

I did that and now I'm getting this error instead: (I took of the "clone." btw because that was an accident from a long time ago that I forgot to remove along with the rest of it's code.)
Image
z.love
(3.34 KiB) Downloaded 353 times
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: Error With Object Oriented Things

Post by NotARaptor »

That's because you're calling self.TestMap - this doesn't pass the object reference as the first parameter, so inside the method it takes the first parameter to TestMap and assigns that to self.

Change all the calls to self:TestMap(...) and it will work fine.
User avatar
xlr8877
Prole
Posts: 11
Joined: Fri Mar 23, 2018 2:45 pm

Re: Error With Object Oriented Things

Post by xlr8877 »

I did that and I'm getting this error:
Image
z.love
(3.34 KiB) Downloaded 375 times
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Error With Object Oriented Things

Post by pgimeno »

When it says you're attempting to index a nil value, it means that you are trying to use a table that is not defined.

I haven't even looked at your code, as you should learn to debug your own program. But if you used something like this:

Code: Select all

variable[index]
then look into how and where you defined "variable", and even whether it's misspelled. If you're getting the error message, it's because "variable" is nil, and you can't index a nil element. (Replace "variable" and "index" with whatever the names are in your program)

The error messages are sometimes concise, but they provide the information as to what exactly went wrong, which is the first step in debugging your program. Why it went wrong, is for you to find out.
User avatar
xlr8877
Prole
Posts: 11
Joined: Fri Mar 23, 2018 2:45 pm

Re: Error With Object Oriented Things

Post by xlr8877 »

This is where the error is supposed to be, this is in a file called "monster.lua":

Code: Select all

function Monster:TestMap(x, y)
	if map[self.grid_x / 32 + x][self.grid_y / 32 + y] == 1 then
		return false
	end
	if emap[self.grid_x / 32 + x][self.grid_y / 32 + y] == 2 then
		return false
	end
	if emap[self.grid_x / 32 + x][self.grid_y / 32 + y] == 3 then
		return false
	end
	if map[self.grid_x / 32 + x][self.grid_y / 32 + y] == 5 then
		return false
	end
	return true
end
This is where those are defined in the main.lua

Code: Select all

function love.load()

	setup = {
		placeholder = 0
	}
	player = {
		grid_x = 672,
		grid_y = 160,
		act_x = 200,
		act_y = 200,
		speed = 10,
		canmove = false,
		movetimer = 0,
		health = 640,
		maxhealth = 640,
		blink = 300,
		mana = 640,
		maxmana = 640,
		spell = 0,
		sptrturns = 0
	}
	map = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 0, 0, 5, 5, 5, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 1, 5, 5, 5, 5, 1, 5, 5, 1 },
		{ 1, 0, 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 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
	}

    Object = require "classic"

    require "monster"

    

	goblin = Monster(544, 192, 50, 160)
    
	love.window.setMode(640, 640)
end
These are the create update and draw functions in "monster.lua" file:

Code: Select all

Monster = Object:extend()

function Monster:new(grid_x, grid_y, health, damage)
    self.grid_x = grid_x
    self.grid_y = grid_y
    self.act_x = grid_x
    self.act_y = grid_y
    self.speed = 10
	self.maxhealth = health
	self.health = health
	self.damage = damage
	self.trapdamage = 30
end

function Monster:update(dt)
    if self.health < 0 then
		self.health = 0
	end
	if self.health > self.maxhealth then
		self.health = self.maxhealth
	end
	if player.movetimer == 10 and self.health > 0 then
		if player.grid_x > self.grid_x and self.TestMap(1, 0) then
			if self.TestTrap(1, 0) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32 + 1)][(self.grid_x / 32)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_x = self.grid_x + 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		elseif player.grid_x < self.grid_x and self.TestMap(-1, 0) then
			if self.TestTrap(-1, 0) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32 - 1)][(self.grid_x / 32)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_x = self.grid_x - 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		elseif player.grid_y > self.grid_y and self.TestMap(0, 1) then
			if self.TestTrap(0, 1) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32)][(self.grid_x / 32 + 1)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_y = self.grid_y + 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		elseif player.grid_y < self.grid_y and self.TestMap(0, -1) then
			if self.TestTrap(0, -1) then
				self.health = self.health - self.trapdamage
				map[(self.grid_y / 32)][(self.grid_x / 32 - 1)] = 0
			end
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 0
			self.grid_y = self.grid_y - 32
			emap[(self.grid_y / 32)][(self.grid_x / 32)] = 2
		else
			if self.TestPlayer(1, 0) then
				player.health = player.health - self.damage
			end
			if self.TestPlayer(-1, 0) then
				player.health = player.health - self.damage
			end
			if self.TestPlayer(0, 1) then
				player.health = player.health - self.damage
			end
			if self.TestPlayer(0, -1) then
				player.health = player.health - self.damage
			end
		end
	end
	self.act_y = self.act_y - ((self.act_y - self.grid_y) * self.speed * dt)
	self.act_x = self.act_x - ((self.act_x - self.grid_x) * self.speed * dt)
end

function Monster:draw()
    love.graphics.setColor(255, 0, 0)
	love.graphics.rectangle("fill", self.act_x, self.act_y, 32, 32)
	if self.health > 0 then
		love.graphics.setColor(255, 200, 0)
	end
	if self.health < 1 then
		love.graphics.setColor(0, 0, 0)
	end
	love.graphics.rectangle("fill", self.act_x + 4, self.act_y + 8, 8, 4)
	love.graphics.rectangle("fill", self.act_x + 20, self.act_y + 8, 8, 4)
end
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 11 guests