tables, love.graphics.setColor and math.random problems

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Olee
Prole
Posts: 3
Joined: Mon Jul 27, 2015 12:56 pm

tables, love.graphics.setColor and math.random problems

Post by Olee »

Hi guys, i have a small problem with a game i'm working on. I have created a function for a simple circle to spawn as a random color in a random location with the identity of 'food' which when i run i get this error:

"food.lua:26" bad argument #1 to 'setColor' (number expected, got nil)

Code: Select all

food = {
	foodid = 0,	
	radius = 3,
	segments = 20
}

function food:load()
	fod = {
			x=math.random(1000),
			y=math.random(1000),
			r=math.random(10, 255),
			g=math.random(10, 255),
			b=math.random(10, 255),
		 }
	self.foodid = self.foodid+1
	setmetatable(fod, { __index = food })
	return fod
end

function food:update( dt )
	
end


function food:draw( dt )
	love.graphics.setColor(self.r, self.g, self.b) -- THIS IS LINE 26 WITH THE ERROR
	love.graphics.circle("fill", self.x, self.y, self.radius, self.segments)
end

function food:respawn()
	self.x=math.random(1,1000)
	self.y=math.random(1,1000)
end

function UPDATE_FOOD( dt )
	food.update(dt)
end
I don't see what i am doing wrong. I could create a work around and change:

Code: Select all

 self.r, self.g, self.b 
to

Code: Select all

 fod.r, fod.g, fod.b 
but that's not the point and i don't see why this isn't working and i believe knowing whats going wrong here could benefit myself and the community.

Thanks in advance,
Oli.
User avatar
bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: tables, love.graphics.setColor and math.random problems

Post by bakpakin »

This is a guess, as I don't have your source. Post the full source and it'll be easier to help. :crazy:

Your code is a bit strange and I think your doing some things you didn't mean to do. I'm guessing you're loading the 'food' table wrong because you are unnecessarily adding colon syntax to food:load (food.load)?

The error probably happens because you're passing 'food:draw' the 'food' table (which does not have r, g, b) as 'self', so all arguments evaluate to nil.

Here is food.load without colon syntax:

Code: Select all

function food.load()
   fod = {
         x=math.random(1000),
         y=math.random(1000),
         r=math.random(10, 255),
         g=math.random(10, 255),
         b=math.random(10, 255),
       }
   food.foodid = food.foodid+1
   setmetatable(fod, { __index = food })
   return fod
end
Again, this is a guess.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: tables, love.graphics.setColor and math.random problems

Post by Robin »

I think you're using "food:draw()" instead of "fod:draw()".

Also, "fod" is a global, that might cause issues if you want to make more than one food.
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: tables, love.graphics.setColor and math.random problems

Post by Jasoco »

If you're trying to create a "food object" here's the method I use for creating objects (Only one of many methods)

Code: Select all

sample_object = {}

function sample_object:load(...)
	local _i = {}
	setmetatable(_i, {__index = self})
	_i:setup(...)
	return _i
end
function sample_object:setup(...)
	local arg = ... or {}
	self.kind = "sample_object_kind"
	self.x = arg.x or 0
	self.y = arg.y or 0
	self.anotherArgument = arg.anotherArgument or "default value"
end
You'd create all your game objects using this template (Again, there are other methods. Look up some class modules on the forum if you want. Some of them make it simpler than having to use this code every time. Also, everything in the setup function will vary depending on what the object is.) and update them and draw them all as such:

To create the objects:

Code: Select all

game_object_list = {}
game_object_list["sampleObject1"] = sample_object:create { x = 100, y = 100, etc... }
game_object_list["sampleObject2"] = sample_object:create { x = 300, y = 200, etc... }
...
Inside the game's update function:

Code: Select all

local objects_to_remove = {}
for i, o in pairs(game_object_list) do
	o:update(dt)
	if o.isDead then
		objects_to_remove[#objects_to_remove+1] = i
	end
end
for i = 1, #objects_to_remove do
	game_object_list[objects_to_remove[i]] = nil
end
(I added code for removing "dead" objects using the method I use. Basically in game, when an object is defeated or eaten or otherwise finished being used, you'd set it's isDead flag to true and the game will clean it up.)

Inside the game's draw function:

Code: Select all

for i, o in pairs(game_object_list) do
	o:draw()
end
By using an object model, you can keep all that objects variables inside the object itself and create as many instances of an object type as you need. And all objects, whether they're enemies or players, can be put in the same pool of objects or separate pools if you need to.

By no means is this the only method for doing this. But it's how I do it.


Last bumped by Olee on Wed Nov 11, 2015 7:23 pm.
Post Reply

Who is online

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