Touch to table

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
C_Mihai
Prole
Posts: 4
Joined: Mon Feb 13, 2017 8:34 pm

Touch to table

Post 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.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Touch to table

Post 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

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Touch to table

Post 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.
C_Mihai
Prole
Posts: 4
Joined: Mon Feb 13, 2017 8:34 pm

Re: Touch to table

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

Who is online

Users browsing this forum: No registered users and 7 guests