Middleclass passing objects

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
trumpet7347
Prole
Posts: 2
Joined: Fri Oct 12, 2012 10:40 pm

Middleclass passing objects

Post by trumpet7347 »

Hey guys, Im having some trouble with my code and hoped one of you could help. I am using middleclass and Im trying to pass an object to another function, but for some reason whenever I try to access the object, I get a nil error.

Code: Select all

--Mill.lua--
function Mill:update(dt)
	self:findTrees()
	for i=1, #self.TreesInRange do
		if self.TreesInRange[i].beingGathered == false then
			self.newGatherer(self.TreesInRange[i]) --This calls newGatherer function and passes a "Tree" Object to it
			self.TreesInRange[i].beingGathered = true
		end
	end
end
This is where the function that passes the Object, I have checked to make sure that there is actually an object in the table and its there.

Code: Select all

--Mill.lua
function Mill:newGatherer(tree)
	if tree == nil then
		love.graphics.print('ERROR', 0, 0) --This should not display, but it does :(
	end
	table.insert(Mill.Gatherers, Gatherer:new(325, 550, tree))	
end
and this is where Im having trouble, the print function should not display

I will also upload my .love so you can take a look. I am trying to just make a simple resource gathing system. The file that I am having trouble with is Mill.lua.

Thanks again guys
Attachments
Game.love
(4.94 KiB) Downloaded 94 times
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: Middleclass passing objects

Post by paritybit »

I see that you have a console start with the game; you'd probably be better off printing the error to the console using print instead of love.graphics.print. The way I understand love.graphics.print is that it (like any other code that draws) has to be called form love.draw -- so unless you store it in a variable somewhere and print it repeatedly (each time draw is called) then it won't be printed to the game viewport.

I think your problem is that you're calling self.newGatherer with the dot rather than the colon. It looks like you defined Mill:newGatherer so that it expects self as a first parameter (either implicit with the : operator or explicit). The function is expecting two parameters (self and tree); it uses the first parameter you pass as self, and then tree is nil.

It doesn't look like you use the self, so you could either change the colon to a dot, or change your calling code to use a colon.

I make this mistake about once a night.
trumpet7347
Prole
Posts: 2
Joined: Fri Oct 12, 2012 10:40 pm

Re: Middleclass passing objects

Post by trumpet7347 »

the period was the problem, I knew it was going to be a small mistake like that. Thanks for the help!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 2 guests