Page 2 of 2

Displaying sub-table values of moving object

Posted: Sun May 31, 2020 7:19 am
by CanYouReply
I am checking if I can display table values of an moving object and which I coded as below :

Code: Select all

temptable = {}
temptable.x = 710
temptable.y = 20
temptable.width = 40
temptable.height = 30


function love.draw()
	local coXX = 100
	for i,v in pairs(temptable) do -- if instead we do ipairs then the values of index & key is not showing
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.print("value of i : ", 20, 220)
		love.graphics.print("value of v : ", 20, 240)
		love.graphics.print(i, coXX, 220)
		love.graphics.print(v, coXX, 240)
		coXX = coXX + 150
	end

	love.graphics.setColor(255, 100, 50, 255)
	love.graphics.rectangle("fill", temptable.x, temptable.y, temptable.width, temptable.height)
end

function love.update(dt)
	temptable.x = temptable.x
	temptable.y = temptable.y + 10 * dt
end
The above codes are running perfectly and screenshot of the screen is attached:

Image

Now I want to display sub-table values for the following changes in my code :

Code: Select all

temptable = {}
	for i=0, 8 do
		local box = {}
		box.x = 710
		box.y = 20
		box.width = 40
		box.height = 30
		table.insert(temptable, box)
	end 
	

function love.draw()
	local coXX = 100
	for i,v in pairs(temptable) do -- if instead we do ipairs then the values of index & key is not showing 
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.print("value of i : ", 20, 220)		
		love.graphics.print("value of v : ", 20, 240)
		love.graphics.print(i, coXX, 220)		
		love.graphics.print(v, coXX, 240)
		coXX = coXX + 150
	end

	love.graphics.setColor(255, 100, 50, 255)
	for i, v in pairs(temptable) do 
		love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
	end
end 

function love.update(dt)
	for i, v in pairs(temptable) do
		v.x = v.x 
		v.y = v.y + 10 * dt 
	end 
	
end



But the display dont show any values against the keys of the sub-table. given below is the screenshot. Where is the mistake I have done ? Thanks in advance.

Image

Re: Object orientation & code complexity [Multiplayer 4X]

Posted: Sun May 31, 2020 8:29 am
by hoistbypetard
(Aside: you probably want a new topic for this instead of attaching it to a very old thread...)

I don't exactly understand your goal, but I see one problem:
In you second loop, each v is a table, not a field. If you want to display field values, you need something like

Code: Select all

for ii, vv in pairs(v) do
-- print ii and vv however it makes sense for you
end
inside your loop over the table of tables.

Re: Object orientation & code complexity [Multiplayer 4X]

Posted: Mon Jun 01, 2020 4:05 pm
by CanYouReply
Hi, Sorry its my mistake to understand how to start a new thread....
But I can't understand your solution...all I wanted is to see the sub-table 'field' values on my screen while it moves, i.e. 'field' values of object 'Box'.
As suggested by you I have made the following changes as below :

Code: Select all

temptable = {}
	for i=0, 8 do
		local box = {}
		box.x = 710
		box.y = 20
		box.width = 40
		box.height = 30
		table.insert(temptable, box)
	end 
	

function love.draw()
	local coXX = 100
	for i,v in pairs(temptable) do 
		for ii, vv in pairs(v) do
			love.graphics.setColor(255, 255, 255, 255)
			love.graphics.print("value of i : ", 20, 220)		
			love.graphics.print("value of v : ", 20, 240)
			love.graphics.print(ii, coXX, 220)		
			love.graphics.print(vv, coXX, 240)
			coXX = coXX + 150
		end 
	end

	love.graphics.setColor(255, 100, 50, 255)
	for i, v in pairs(temptable) do 
		love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
	end
end 

function love.update(dt)
	for i, v in pairs(temptable) do
		v.x = v.x 
		v.y = v.y + 10 * dt 
	end 
	
end
Now it seems to be showing on screen ... but is it the 'field' values of sub-table 'Box' in my code ? Have I done it correctly ? Thanks in advance.

Re: Object orientation & code complexity [Multiplayer 4X]

Posted: Mon Jun 01, 2020 10:57 pm
by hoistbypetard
You were only showing the first sub-table. I modified your program so that the x positions of the boxes are each different, it prints the values for each box, and so that the box colors are different. (I stored the colors as a sub-table of each box.)

That should make it easier to get a mental picture of what's going on.

Code: Select all

function love.load()
	local majorVersion = love.getVersion()
	local colorScale = 1
	if majorVersion == 11 then colorScale = 255 end
	math.randomseed(os.time())
	temptable = {}
	for i=0, 8 do
		local box = {}
		box.y = 20
		box.width = 40
		box.height = 20
		box.x = 2 + 2 * i * box.width
		box.color = {
			math.random(1, 255)/colorScale,
			math.random(1,255)/colorScale,
			math.random(1,255)/colorScale,
			255/colorScale
		}
		print('generated box at : ' .. tostring(box.x) .. ' with color:', unpack(box.color))
		table.insert(temptable, box)
	end 
end

function love.draw()
	local coYY = 160
	for i,v in pairs(temptable) do 
		local coXX = 100
		love.graphics.setColor(unpack(v.color))
		for ii, vv in pairs(v) do
			if ii ~= 'color' then
				love.graphics.print(tostring(i) .. ": i : ", 20, coYY)
				love.graphics.print(tostring(i) .. ": v : ", 20, coYY+20)
				love.graphics.print(ii, coXX, coYY)		
				love.graphics.print(vv, coXX, coYY+20)
				coXX = coXX + 150
			end
		end
		coYY = coYY + 40
	end

	for i, v in pairs(temptable) do 
		love.graphics.setColor(unpack(v.color))
		love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
	end
end 

function love.update(dt)
	for i, v in pairs(temptable) do
		v.x = v.x 
		v.y = v.y + 10 * dt 
	end 
end