Entity Issue

Mods, guides how to use and install mods go right in here.
Post Reply
User avatar
Turtle95
Posts: 1246
Joined: 21 Mar 2012, 21:48
Contact:

Post » 11 Apr 2012, 00:54

Alright, I've gotten it to work on a box, now I've got to draw the turret (which i did) but two seconds later, its "indexing '?' a nil value' at 'turret.lua: line 243"
Not sure what's wrong. Bolded line is the issue.

Code: Select all

turret = class:new()

function turret:init(x, y)
	self.x = x
	self.y = y
	self.id = id
	self:randomtime()
	self.timer = self.time-0.5
	self.autodelete = true
	self.graphic = Turret
	self.quad = Turretquad
	--PHYSICS STUFF
	self.cox = x
	self.coy = y
	self.x = x-12/16
	self.y = y-14/16
	self.speedy = 0
	self.speedx = 0
	self.width = 12/16
	self.height = 14/16
	self.static = false
	self.active = true
	self.category = 9
	self.parent = nil
	self.portaloverride = true
	self.lasertable = {}
	times = 16 --Turret Death time D:
	self.cox = x
	self.coy = y
	self.dir = dir
	self.r = r
	time2 = 16 -- Test for Laser duration
	
	self.enabled = true
	self.mask = {	true,
					false, false, false, false, false,
					false, true, false, true, true,
					false, false, true, false, false,
					true, true, false, false, true,
					false, true, true, false, false,
					true, false, true, true, true}
					
	self.emancipatecheck = true

	self.userect = adduserect(self.x, self.y, 12/16, 12/16, self)
	
	--IMAGE STUFF
	self.drawable = true
	self.graphic = Turret
	self.quad = Turretquad
	self.offsetX = 10
	self.offsetY = 6
	self.quadcenterX = 6
	self.quadcenterY = 6
	
	
	self.rotation = 0 --for portals
	
	self.falling = false
	self.destroying = false
	self.outtable = {}
	self.portaledframe = false
end

function turret:update(dt)
	self.timer = self.timer + dt
	if self.timer > self.time and self.x > splitxscroll[1] and self.x < splitxscroll[1]+width+2 then
		if self:fire() then
			self.timer = 0
			self:randomtime()
		end
	end
	
	local friction = boxfrictionair
	if self.falling == false then
		friction = boxfriction
	end
	
	if not self.pushed then
		if self.speedx > 0 then
			self.speedx = self.speedx - friction*dt
			if self.speedx < 0 then
				self.speedx = 0
			end
		else
			self.speedx = self.speedx + friction*dt
			if self.speedx > 0 then
				self.speedx = 0
			end
		end
	else
		self.pushed = false
	end
	
	
	if self.parent then
		local oldx = self.x
		local oldy = self.y
		
		self.x = self.parent.x+math.sin(-self.parent.pointingangle)*0.3
		self.y = self.parent.y-math.cos(-self.parent.pointingangle)*0.3
		
		if self.portaledframe == false then
			for h, u in pairs(emancipationgrills) do
				if u.dir == "hor" then
					if inrange(self.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, oldy, self.y, true) then
						self:emancipate(h)
					end
				else
					if inrange(self.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, oldx, self.x, true) then
						self:emancipate(h)
					end
				end
			end
		end
	end
	
	self.userect.x = self.x
	self.userect.y = self.y

	--check if offscreen
	if self.y > 17 then
		self:destroy()
	end
	
	self.portaledframe = false
	
	if self.destroying then
		return true
	else
		return false
	end
	
end

function turret:fire()
	if #objects["bulletbill"] >= maximumbulletbills then
		return false
	end
	
	--get nearest player
	local pl = 1
	for i = 2, players do
		if math.abs(objects["player"][i].x + 14/16 - self.x) < math.abs(objects["player"][pl].x + 14/16 - self.x) then
			pl = i
		end
	end
	
	if objects["player"][pl].x + 14/16 > self.x + bulletbillrange then
		table.insert(objects["bulletbill"], bulletbill:new(self.x, self.y, "right"))
		return true
	elseif self:used() then
		
	end
end

function turret:randomtime()
	local rand = math.random(bulletbilltimemin*10, bulletbilltimemax*10)/10
	self.time = rand
end

function turret:leftcollide(a, b)
	if a == "button" then
		self.y = b.y - self.height
		self.x = b.x + b.width - 0.01
		if self.speedy > 0 then
			self.speedy = 0
		end
		return false
	elseif a == "player" then
		self.pushed = true
		return false
	end
end

function turret:rightcollide(a, b)
	if a == "button" then
		self.y = b.y - self.height
		self.x = b.x - self.width+0.01
		if self.speedy > 0 then
			self.speedy = 0
		end
		return false
	elseif a == "player" then
		self.pushed = true
		return false
	end
end

function turret:floorcollide(a, b)
	if self.falling then
		self.falling = false
	end
	
	if a == "goomba" or a == "bulletbill" then
		b:stomp()
		addpoints(200, self.x, self.y)
		playsound(stompsound)
		self.falling = true
		return false
	end
end

function turret:passivecollide(a, b)
	if a == "player" then
		if self.x+self.width > b.x+b.width then
			self.x = b.x+b.width
		else
			self.x = b.x-self.width
		end
	end
end

function turret:startfall()
	self.falling = true
end

function turret:emancipate()
	if self.parent then
		self.parent:cubeemancipate()
	end
	self:destroy()
end

function turret:destroy()
	self.userect.delete = true
	self.destroying = true
	
	for i = 1, #self.outtable do
		if self.outtable[i].input then
			self.outtable[i]:input("toggle")
		end
	end
end

function turret:addoutput(a)
	table.insert(self.outtable, a)
end

function turret:used(id)
	self.parent = objects["player"][id]
	self.active = false
	objects["player"][id]:pickupturret(self) <--------------------------------------------------
end

function turret:dropped()
	self.parent = nil
	self.active = true
end

function turret:portaled()
	self.portaledframe = true
end

----------------------
bulletbill = class:new()
function bulletbill:init(x, y, dir)
	self.startx = x-14/16
	--PHYSICS STUFF
	self.x = self.startx
	self.y = y-14/16
	self.speedy = 0
	if dir == "left" then
		self.speedx = -bulletbillspeed
		self.customscissor = {self.x-18/16, self.y-2/16, 1, 1}
	else
		self.speedx = bulletbillspeed
		self.customscissor = {self.x+14/16, self.y-2/16, 1, 1}
	end
	self.width = 12/16
	self.height = 12/16
	self.static = false
	self.active = true
	self.gravity = 0
	self.rotation = 0
	self.autodelete = true
	self.timer = 0
	
	--IMAGE STUFF
	self.drawable = true
	self.quad = Laserquad[spriteset]
	self.offsetX = 6
	self.offsetY = 2
	self.quadcenterX = 8
	self.quadcenterY = 8
	self.graphic = laserimg
	
	self.category = 11
	self.animationdirection = dir
	
	self.mask = {	true, 
					true, false, false, false, true,
					true, true, true, true, true,
					true, false, true, true, true,
					true, true, true, true, true,
					true, true, true, true, true,
					true, true, true, true, true}
					
	self.shot = false
	
	playsound(Lasersound)
end

function bulletbill:update(dt)
	if self.x < self.startx - 1 or self.x > self.startx + 1 then
		self.customscissor = nil
	end
	
	if self.shot then
		self.speedy = self.speedy + shotgravity*dt
		
		self.x = self.x+self.speedx*dt
		self.y = self.y+self.speedy*dt
		
		--check if goomba offscreen
		if self.y > 18 then
			return true
		else
			return false
		end
	else
		self.timer = self.timer + dt
		if self.timer >= bulletbilllifetime then
			return true
		end
	
		if self.rotation ~= 0 then
			if math.abs(math.abs(self.rotation)-math.pi/2) < 0.1 then
				self.rotation = -math.pi/2
			else
				self.rotation = 0
			end
		end
		
		if self.speedx < 0 then
			self.animationdirection = "left"
		elseif self.speedx > 0 then
			self.animationdirection = "right"
		elseif self.speedy < 0 then
			self.animationdirection = "right"
		elseif self.speedy > 0 then
			self.animationdirection = "left"
		end
	end
end

function bulletbill:rightcollide(a, b)
	self:globalcollide(a, b)
	return false
end

function bulletbill:leftcollide(a, b)
	self:globalcollide(a, b)
	return false
end

function bulletbill:floorcollide(a, b)
	self:globalcollide(a, b)
	return false
end

function bulletbill:globalcollide(a, b)
	
end

function bulletbill:portaled()
	self.killstuff = true
end

Camewel
Posts: 2996
Joined: 02 Feb 2012, 21:32

Post » 11 Apr 2012, 01:00

This bit of code isn't helpful, if you knew what the error meant, you would know that. The error is caused by 'id' being out of range.

User avatar
Turtle95
Posts: 1246
Joined: 21 Mar 2012, 21:48
Contact:

Post » 11 Apr 2012, 01:01

Camewel wrote:This bit of code isn't helpful, if you knew what the error meant, you would know that. The error is caused by 'id' being out of range.
Exactly how? It's the exact code from the box:used() method, so why does it do this now?

EDIT:

Added this into mario:use, but still nothing

function mario:use()
if self.pickup then
if self.pickup.destroying then
self.pickup = false
else
self:dropbox()
self:dropturret()
return
end
end
local xcenter = self.x + 6/16 - math.sin(self.pointingangle)*userange
local ycenter = self.y + 6/16 - math.cos(self.pointingangle)*userange

local col = userect(xcenter-usesquaresize/2, ycenter-usesquaresize/2, usesquaresize, usesquaresize)
if #col > 0 then
col[1]:used(self.playernumber)
end
end
-----

there is a dropturret method.

Camewel
Posts: 2996
Joined: 02 Feb 2012, 21:32

Post » 11 Apr 2012, 01:28

Did you add the turret to the objects list?

User avatar
Turtle95
Posts: 1246
Joined: 21 Mar 2012, 21:48
Contact:

Post » 11 Apr 2012, 01:31

Camewel wrote:Did you add the turret to the objects list?
Yep, it's all there.

Objects, table in entities.png, table to draw in editor.

Camewel
Posts: 2996
Joined: 02 Feb 2012, 21:32

Post » 11 Apr 2012, 01:35

Wait for Maurice to wake up so can be sarcastic whilst giving you the answer.

User avatar
Turtle95
Posts: 1246
Joined: 21 Mar 2012, 21:48
Contact:

Post » 11 Apr 2012, 01:36

Camewel wrote:Wait for Maurice to wake up so can be sarcastic whilst giving you the answer.
Ha. Ha. Ha. Good one. Seriously, just realised it's not that line. It's on dropped, becoming active again. My bad.

EDIT:

Nvm, fixed it.

User avatar
fakeuser
Posts: 135
Joined: 04 Mar 2012, 01:20

Post » 11 Apr 2012, 02:07

You should reactivate the turret only if it was active before being picked up. I'm writing this because your code shows that you have not considered that fact. Alse my bet would have been you didn't actually add mario:pickupturret() function

Camewel
Posts: 2996
Joined: 02 Feb 2012, 21:32

Post » 11 Apr 2012, 02:12

Also, you should stop making new topics about the same mod, just use the first topic. This is the third topic you've made about your mod.

User avatar
Turtle95
Posts: 1246
Joined: 21 Mar 2012, 21:48
Contact:

Post » 11 Apr 2012, 02:46

Camewel wrote:Also, you should stop making new topics about the same mod, just use the first topic. This is the third topic you've made about your mod.
I haven't made 3. Just two. This one was a help me thing.

EDIT:

@fakeuser: i did
Last edited by Turtle95 on 11 Apr 2012, 02:46, edited 1 time in total.

Camewel
Posts: 2996
Joined: 02 Feb 2012, 21:32

Post » 11 Apr 2012, 02:55


Post Reply