Page 1 of 1

Touch to table

Posted: Tue Mar 28, 2017 9:34 pm
by C_Mihai
Hi! I try to make a code that stores in a table every touch on the screen (after i will make lines between them) to draw.
I used the touch function to draw the touches as in the example from the wiki but it does not change a variable, i want something like void f(&x,&y) from c++ but with touches which will be stored in a table.
Any idea? Feel free to say your opinion.

Re: Touch to table

Posted: Tue Mar 28, 2017 10:01 pm
by Beelz
Lua is all about tables. Put the touch events into a table and tinker from there...

Code: Select all


local touches = {}

function love.touchpressed(id, x, y, dx, dy, pressure)
	local t = {
		id = id,
		x = x,
		y = y,
		dx = dx,
		dy = dy,
		p = pressure
	}
	table.insert(touches, t)
end

local lg = love.graphics

function love.draw()
	for i, touch in ipairs(touches) do
		lg.setColor(255,255,255)
		lg.circle('fill', touch.x, touch.y, 25)
		lg.setColor(0,0,0)
		lg.printf(tostring(touch.id), touch.x-25, touch.y, 50, 'center')
	end
end

Re: Touch to table

Posted: Wed Mar 29, 2017 2:09 am
by airstruck
Keep in mind you can have multiple return values in Lua. The idiomatic thing to do would probably be x, y = f(), where f returns two values. Or you might prefer f(t) instead, where t is a table, and f sets t.x and t.y to some values.

Re: Touch to table

Posted: Thu Mar 30, 2017 11:02 am
by C_Mihai
I tried it with get touches but yes, there many x and y and i could not apply it as a single variable.
Thank you a lot for the help!