## Anyone gonna help a newbie? Again... yet Again...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

### Anyone gonna help a newbie? Again... yet Again...

Does anyone see an error in this code? LOVE doesn't say an error to me, but what it does is it doesn't work properly. This is my third topic on coding help what this is supposed to do is build a grid using my images, fitting the size of the window perfectly. Without the color pieces added, it works fine. But, adding the changing one thing in the board table chanegs the entire row! It's laid out like this: A table containing other tables. When you press a button it checks to see if the the grid space is existent and empty. If so, it changes that value. In this code, it constantly draws that table, so if the table is changed, boom, it changes on the screen. I have the table change working, as said above. But, it changes the row, instead of one piece. If there is an error in this, then there's my problem. If not, I know where the problem is.

Code: Select all

function love.draw()
if not selected then
love.graphics.print("PLEASE SELECT A GRID SIZE 4-8", 200, 200)
else
for i=1, 8 do
row=board[i]
for l=1, 8 do
local piece=row[l]
if piece~=0 then
love.graphics.drawq(piece, grid, w*(i-1), he*(l-1), 0, 1, 1, 0, 0)
end
end
end
end
end
"your actions cause me to infer your ego is the size of three houses" -finley
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Anyone gonna help a newbie? Again... yet Again...

You should really, really, really use descriptive topic names.

As for the problem itself, it looks like the offending code is the editing code, which you haven't posted.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

### Re: Anyone gonna help a newbie? Again... yet Again...

Code: Select all

function love.keypressed(key)
if not selected then
for i=4, 8 do
if key==""..i then
selected=i
setGrid(i)
w=w/selected
he=he/selected
grid=love.graphics.newQuad(0, 0, w, he, w, he)
end
end
else
for i=1, 8 do
if ""..i==key then
changeBoard(i, 1)
end
end
end
end

function setGrid(size)
for i=1, size do
local h=board[i]
for i=1, size do
h[i]=empty
end
end
end

function changeBoard(key, row)
if board[row][key]~=0 then
if board[row][key]==empty then
if turn==red then
board[row][key]=red
turn=blue
else
board[row][key]=blue
turn=red
end
end
end
end

"your actions cause me to infer your ego is the size of three houses" -finley
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Anyone gonna help a newbie? Again... yet Again...

LuaWeaver wrote:

Code: Select all

function love.keypressed(key)
for i=1, 8 do
if ""..i==key then
changeBoard(i, 1)
end
end

Doesn't that code set the entire row?
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

### Re: Anyone gonna help a newbie? Again... yet Again...

How so? It goes to board[row][key] and changes it. How does it set the whole row?
"your actions cause me to infer your ego is the size of three houses" -finley
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Anyone gonna help a newbie? Again... yet Again...

LuaWeaver wrote:How so? It goes to board[row][key] and changes it. How does it set the whole row?
Because it is in a for-loop.
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

### Re: Anyone gonna help a newbie? Again... yet Again...

Robin wrote:
LuaWeaver wrote:How so? It goes to board[row][key] and changes it. How does it set the whole row?
Because it is in a for-loop.
But he only sets the element if ""..i==key...

A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string key into a number and add zero to it.

As for the problem you're having, I don't know...
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Anyone gonna help a newbie? Again... yet Again...

leiradel wrote:But he only sets the element if ""..i==key...

A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string key into a number and add zero to it.
I'm wondering about the hacks. What's wrong with tostring() and tonumber()?
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

### Re: Anyone gonna help a newbie? Again... yet Again...

Robin wrote:
leiradel wrote:But he only sets the element if ""..i==key...

A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string key into a number and add zero to it.
I'm wondering about the hacks. What's wrong with tostring() and tonumber()?
Good point, it's just that old habits die hard.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA