Picking a value from a table

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.
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Picking a value from a table

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Picking a value from a table

Post by s-ol »

You can access the Color as boxes[6].color.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Picking a value from a table

Post by Oblivion_123 »

It only returns nil
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Picking a value from a table

Post by s-ol »

oh, you spelled it "colour", my example was with the american spelling ("color")

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Picking a value from a table

Post 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?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Picking a value from a table

Post 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 :(
Last edited by MadByte on Sun Nov 22, 2015 6:30 pm, edited 2 times in total.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Picking a value from a table

Post 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.
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Picking a value from a table

Post 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'
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Picking a value from a table

Post 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]
Oblivion_123
Prole
Posts: 18
Joined: Thu Oct 01, 2015 5:25 pm

Re: Picking a value from a table

Post 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?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests