Drawing rectangle and table problem

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.
Post Reply
helloyyy
Prole
Posts: 2
Joined: Sat Mar 30, 2019 12:52 pm

Drawing rectangle and table problem

Post 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
helloyyy
Prole
Posts: 2
Joined: Sat Mar 30, 2019 12:52 pm

Re: Drawing rectangle and table problem

Post 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.
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Drawing rectangle and table problem

Post 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.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Drawing rectangle and table problem

Post by 4vZEROv »

When you do :

Code: Select all

table.insert(s, newsouris)
The result is :

Code: Select all

 s = {newsouris = { x = x_coord, y = y_coord }}
What you want is :

Code: Select all

 s = {x = x_coord, y = y_coord}  
So just do :

Code: Select all

 
if love.mouse.isDown (1) then
	s = { x = love.mouse.getX(), y = love.mouse.getY() }
end
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Drawing rectangle and table problem

Post by zorg »

4vZEROv wrote: Sun Mar 31, 2019 1:47 pm When you do :

Code: Select all

table.insert(s, newsouris)
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}}
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Drawing rectangle and table problem

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

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests