Page 1 of 2

Picking a value from a table

Posted: Sun Nov 22, 2015 5:20 pm
by Oblivion_123
Hi, I am currently working on a game and I have come to a dead end in my ability to work, so I need some help from the community.

In my game I have made a table, which then inserts values into it. The table gets created, and with item 1 it had 5 characteristic, same with item 2, 3, 4 and so on.

For example, in the first item, it's called box, it will create a table called 'boxes' and it will insert into the table 'box' which had been given 5 characteristics.

Code: Select all

function newBox(x, y)
	local box = {x = x, y = y, colour = pickAColour(), width = 75, height = 75}
	table.insert(boxes, box)
end
-- MY QUESTION

The issue arrives when further on in the script, when I try to find out the colour of the 6th box. Can anyone help me?
Any help is much appreciated :)

Thanks.

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 5:22 pm
by s-ol
You can access the Color as boxes[6].color.

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 5:31 pm
by Oblivion_123
It only returns nil

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 5:49 pm
by s-ol
oh, you spelled it "colour", my example was with the american spelling ("color")

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 6:14 pm
by Oblivion_123
Yes, I noticed that and I changed i tried it with boxes[6].colour and that's what returned nil. Lol.

Any ideas?

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 6:26 pm
by MadByte
It would be easier if you upload your project. It seems like you don't create the boxes properly or your table keys are strings or something like that. If I had to debug such a problem I would start by print the amount of items in the table on screen or to the console.

Code: Select all

function love.draw()
  love.graphics.print("Boxes: "..#boxes, 10, 10)
end

-- or 

print(#boxes) -- to console
Just saying.

edit
or your pickAColour function returns a nil ...

edit2
ninja'd :(

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 6:28 pm
by pgimeno
pickAColour() may be returning nil?

Does e.g. boxes[1].x work but boxes[1].colour return nil? If so, that's an indication of the above.

Re: Picking a value from a table

Posted: Sun Nov 22, 2015 6:39 pm
by Oblivion_123
Yep, boxes[1].x works and returns -20, 80, and so on whenever I change the 1 to a 2 or to a 3. So... is my pickAColour() function broken, if so, do you know what's wrong with it?

Code: Select all

function pickAColour()
	
	number = love.math.random(1,100)

	reds = { 255, 0, 0 }
	blues = { 0, 0, 255 }
	yellows = { 255, 255, 0 }
	purples = { 168, 0, 255 }
	pinks = { 255, 0, 255 }

	if number <= 50 then
		return blues
	elseif number <= 75 and number >= 51 then
		return purples
	elseif number <= 93 and number >= 76 then
		return pinks
	elseif number <= 99 and number >= 94 then
		return reds
	else
		return yellows
	end

end
Quick note, it only returns nil when I do print( tonumber( boxes[1].colour ) ). Else if I don't do tonumber it returns a string like this.

'0x0099d2b0'

Re: Picking a value from a table

Posted: Mon Nov 23, 2015 1:29 am
by pgimeno
Yeah, it returns a table.

Code: Select all

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> =tonumber({1,2,3})
nil
> ={1,2,3}
table: 0x8873300
> 
That's how Lua works. You access the components like: boxes[1].colour[1] through boxes[n].colour[3]

Re: Picking a value from a table

Posted: Mon Nov 23, 2015 5:47 pm
by Oblivion_123
Ok, but that still doesn't answer my question, is there a way that I can use this info to figure out the colour of box. Do you know any way?