Page 1 of 1
Drawing rectangle and table problem
Posted: Sat Mar 30, 2019 1:07 pm
by helloyyy
Hi, I'm new and really enjoy trying making game with love but I have some problem :
I have a s table wich contains table which x,y coordinate for drawing rectangle when I click mousebutton. I can draw them all with ipairs loop (and it's working nice) but I want draw only the first one, how can I make it (i explored all the ways and it doesn't work), how can I access to x and y only on the first object ?
here is code :
s={}
function love.load()
end
function love.update(dt)
if love.mouse.isDown (1) then
newcoord= {x=love.mouse.getX(),y=love.mouse.getY()}
table.insert(s,newcoord)
end
end
function love.draw(dt)
--for i, v in ipairs(s) do
--love.graphics.rectangle( "line",v.x,v.y,1,1)
--end
end
Re: Drawing rectangle and table problem
Posted: Sat Mar 30, 2019 6:43 pm
by helloyyy
Well, I found a way, deleting every other values in table :
for i, v in ipairs(s) do
table.remove(s,i+1)
love.graphics.rectangle( "line",v.x,v.y,love.mouse.getX()-v.x,love.mouse.getY()-v.y)
The goal was to make basic mouse rectangle selection, full code : (its working nice)
s={}
function love.load()
end
function love.update(dt)
if love.mouse.isDown (1) then
newsouris= {x=love.mouse.getX(),y=love.mouse.getY()}
table.insert(s,newsouris)
else
for i in ipairs (s) do
table.remove(s,i)
end
end
end
function love.draw(dt)
for i, v in ipairs(s) do
table.remove(s,i+1)
love.graphics.rectangle( "line",v.x,v.y,love.mouse.getX()-v.x,love.mouse.getY()-v.y)
end
end
But I would be grateful if someone can tell me how to access value in a table stored in other table directly without an ipairs loop.
Here for example I would like to print or use newsouris.x stored in table s{}, but I don't know the syntax.
Re: Drawing rectangle and table problem
Posted: Sat Mar 30, 2019 7:49 pm
by keharriso
After table.insert(s, newsouris), newsouris is now the last element in s. You can access the last element in s with: s[#s], so newsouris.x would be: s[#s].x. You either want that or the first one at s[1].x. Does that help? I might have misunderstood your question.
Re: Drawing rectangle and table problem
Posted: Sun Mar 31, 2019 1:47 pm
by 4vZEROv
When you do :
The result is :
Code: Select all
s = {newsouris = { x = x_coord, y = y_coord }}
What you want is :
So just do :
Code: Select all
if love.mouse.isDown (1) then
s = { x = love.mouse.getX(), y = love.mouse.getY() }
end
Re: Drawing rectangle and table problem
Posted: Sun Mar 31, 2019 7:35 pm
by zorg
4vZEROv wrote: ↑Sun Mar 31, 2019 1:47 pm
When you do :
The result is :
Code: Select all
s = {newsouris = { x = x_coord, y = y_coord }}
This is wrong though, table.insert will insert whatever newsouris is as the "#s+1"-th element into s, so something like this is what would happen:
Code: Select all
local t = {'a', 'b'}
table.insert(t, { x = x_coord, y = y_coord })
-- this results in t being {[1] = 'a', [2] = 'b', [3] = {['x'] = x_coord, ['y'] = y_coord}}
Re: Drawing rectangle and table problem
Posted: Mon Apr 01, 2019 11:20 pm
by 4vZEROv
zorg wrote: ↑Sun Mar 31, 2019 7:35 pm
True, my bad, the error is similar tho. You have a table inside another table and you don't want that