Page 1 of 1

Concating strings and integers

Posted: Mon Oct 19, 2015 4:00 pm
by MichaelShort
So we're trying to concat log and i. When that happens we will be able to identify the log by its log number. ex log256 and log320
log..i doesn't concat it so any help would be appreciated. We're new to love2d.

Code: Select all

for i=64, 1856, 192 do
	love.graphics:reset()
    	logs = { log..i = love.graphics.draw(logImg, i, 896) }
end

Re: Concating strings and integers

Posted: Mon Oct 19, 2015 4:15 pm
by micha
Concatenating string does not work implicitely in table definitions. Try this instead:

Code: Select all

logs = {}
for i=64, 1856, 192 do
  local key = log..i
  logs[key] = whatever
end
But I am not sure what you try to achieve by assigning the return value of love.graphics.draw to a variable. love.graphics.draw does not return anything.

Furthermore, it is probably much better to use the number i as key directly instead of turning it into a string:

Code: Select all

logs = {}
for i=64, 1856, 192 do
  logs[i] = whatever
end

Re: Concating strings and integers

Posted: Mon Oct 19, 2015 4:46 pm
by bartbes
The correct syntax here is

Code: Select all

for i=64, 1856, 192 do
    love.graphics:reset()
    logs = {["log" .. i] = love.graphics.draw(logImg, i, 896)}
end
Sadly, semantically this isn't what you want at all. I doubt you want to reset the graphics every iteration (and it's love.graphics.reset, not love.graphics:reset), nor would you want to recreate the 'logs' table every iteration. And, of course, as micha said, love.graphics.draw doesn't return anything, and you'd probably just want the keys to be numbers anyway.

Re: Concating strings and integers

Posted: Tue Oct 20, 2015 3:06 pm
by MichaelShort
Alright thank you guys.