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
Drawing rectangle and table problem
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Drawing rectangle and table problem
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.
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
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
Re: Drawing rectangle and table problem
When you do :
The result is :
What you want is :
So just do :
Code: Select all
table.insert(s, newsouris)
Code: Select all
s = {newsouris = { x = x_coord, y = y_coord }}
Code: Select all
s = {x = x_coord, y = y_coord}
Code: Select all
if love.mouse.isDown (1) then
s = { x = love.mouse.getX(), y = love.mouse.getY() }
end
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Drawing rectangle and table problem
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:4vZEROv wrote: ↑Sun Mar 31, 2019 1:47 pm When you do :The result is :Code: Select all
table.insert(s, newsouris)
Code: Select all
s = {newsouris = { x = x_coord, y = y_coord }}
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 True 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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests