Page 1 of 1
I have a problem with lag
Posted: Thu May 28, 2020 6:19 pm
by matu
First of all, i have not a perfect english, so sorry for my posible mistakes.
Hello, i'm new at Lua and LOVE2d and i'm starting with a practice (here's how i learn
) and when i was ending i programmed an animated background and when i started the game it was very lag(more than the last way i started the game). I used this codes for the animation:
Code: Select all
function love.load()
stageTime = 0
stageState = "0"
stage = none
end
function love.update(dt)
--Animation's time--
stageTime = stageTime + 1
if stageState == 30 then
stageState = 0 --This is cause i only made 30 pictures for the animation--
end
--Animation's conf--
if stageTime == 30 then
stageState = stageState + 1
stageTime = 0
end
stage = love.graphics.newImage("graphics/stage/stage"..tostring(stageState)..".png") --This line is that lags--
end
function love.draw()
--Animation drawing--
love.graphics.draw(stage,0,0,0,0)
end
Can anyone tell me how to remove the lag please
?
Re: I have a problem with lag
Posted: Fri May 29, 2020 4:08 am
by zorg
Hi and welcome to the forums!
The gigantic yellow warning box on the page for
love.graphics.newImage should tell you your mistake.
Also, do use the dt variable so that your thing runs consistently with regards to time.
Re: I have a problem with lag
Posted: Fri May 29, 2020 10:12 am
by pgimeno
And please use the support forum to post questions!
Re: I have a problem with lag
Posted: Fri May 29, 2020 11:40 am
by sphyrth
Woohoo! Now time for me to provide a sample solution of what to do instead:
Code: Select all
function love.load()
stageTime = 0
stageState = "1"
-- I'm replacing your "stage = none" with this:
-- Step 1: (A table of stages)
stages = {}
-- Step 2: Insert all your 30 images in that table.
for i = 1, 30 do
table.insert(stages, love.graphics.newImage("graphics/stage/stage" .. tostring(i) .. ".png")
end
end
function love.update(dt)
--Animation's time--
stageTime = stageTime + 1
if stageState == 30 then
stageState = 1 --This is cause i only made 30 pictures for the animation--
end
--Animation's conf--
if stageTime == 30 then
stageState = stageState + 1
stageTime = 0
end
-- Now you no longer have to use this laggy thing:
-- stage = love.graphics.newImage("graphics/stage/stage"..tostring(stageState)..".png")
-- Instead, look at love.draw()
end
function love.draw()
--Animation drawing--
love.graphics.draw(stages[stageState],0,0,0,0)
end
NOTE: I started with 1 instead of 0 because tables don't reference 0 values. So I guess you have to rename the images starting from "1" instead of "0".
Re: I have a problem with lag
Posted: Fri May 29, 2020 1:49 pm
by zorg
And now that that happened, i can fix the above code so it:
- uses dt like it should
- uses locals like it should
- uses 0-based indexing so we can microoptimize a branch out (and you don't need to rename anything)
Code: Select all
local frameDelay, frameTime, currentFrame
function love.load()
frameDelay = 1/30 -- Animate with 30 frames per second
frameTime = 0.0
currentFrame = 0 -- Start on the first frame
-- A table of frames
frame = {}
-- Insert all 30 images into that table
for i = 0, 29 do
frame[i] = love.graphics.newImage("graphics/stage/stage" .. tostring(i) .. ".png")
end
end
function love.update(dt)
-- Time the animation
frameTime = frameTime + dt
if frameTime >= frameDelay then
-- Wrap around the end
currentFrame = (currentFrame + 1) % 30 -- number of frames
-- Decrease time by the frame time
frameTime = frameTime - frameDelay
end
end
function love.draw()
love.graphics.draw(frame[currentFrame],0,0,0,0)
end
sphyrth wrote: ↑Fri May 29, 2020 11:40 am
NOTE: I started with 1 instead of 0 because tables don't reference 0 values. So I guess you have to rename the images starting from "1" instead of "0".
table methods indeed usually ignore what's at the 0th key (#, ipairs, etc. won't give "correct" results), but it's still fine to use it, both in terms of speed (thanks to luaJIT handling that) and the fact that there are ways to code around such limitations.
Re: I have a problem with lag
Posted: Fri May 29, 2020 10:27 pm
by matu
zorg wrote: ↑Fri May 29, 2020 4:08 am
Hi and welcome to the forums!
The gigantic yellow warning box on the page for
love.graphics.newImage should tell you your mistake.
Also, do use the dt variable so that your thing runs consistently with regards to time.
I don't know how i haven't seen this.
Re: I have a problem with lag
Posted: Fri May 29, 2020 11:32 pm
by matu
sphyrth wrote: ↑Fri May 29, 2020 11:40 am
Woohoo! Now time for me to provide a sample solution of what to do instead:
Code: Select all
function love.load()
stageTime = 0
stageState = "1"
-- I'm replacing your "stage = none" with this:
-- Step 1: (A table of stages)
stages = {}
-- Step 2: Insert all your 30 images in that table.
for i = 1, 30 do
table.insert(stages, love.graphics.newImage("graphics/stage/stage" .. tostring(i) .. ".png")
end
end
function love.update(dt)
--Animation's time--
stageTime = stageTime + 1
if stageState == 30 then
stageState = 1 --This is cause i only made 30 pictures for the animation--
end
--Animation's conf--
if stageTime == 30 then
stageState = stageState + 1
stageTime = 0
end
-- Now you no longer have to use this laggy thing:
-- stage = love.graphics.newImage("graphics/stage/stage"..tostring(stageState)..".png")
-- Instead, look at love.draw()
end
function love.draw()
--Animation drawing--
love.graphics.draw(stages[stageState],0,0,0,0)
end
NOTE: I started with 1 instead of 0 because tables don't reference 0 values. So I guess you have to rename the images starting from "1" instead of "0".
Thanks, I know I'm supposed to know certain things about Lua (which I don't quite know) to start with love2d so thanks for the explicit information.
Re: I have a problem with lag
Posted: Fri May 29, 2020 11:35 pm
by matu
zorg wrote: ↑Fri May 29, 2020 1:49 pm
sphyrth wrote: ↑Fri May 29, 2020 11:40 am
NOTE: I started with 1 instead of 0 because tables don't reference 0 values. So I guess you have to rename the images starting from "1" instead of "0".
table methods indeed usually ignore what's at the 0th key (#, ipairs, etc. won't give "correct" results), but it's still fine to use it, both in terms of speed (thanks to luaJIT handling that) and the fact that there are ways to code around such limitations.
Thanks for clarifying that it is not necessary to rename the files, you saved me a lot of time.
Re: I have a problem with lag
Posted: Thu Jun 11, 2020 6:19 pm
by milon
matu wrote: ↑Fri May 29, 2020 10:27 pm
zorg wrote: ↑Fri May 29, 2020 4:08 am
Hi and welcome to the forums!
The gigantic yellow warning box on the page for
love.graphics.newImage should tell you your mistake.
Also, do use the dt variable so that your thing runs consistently with regards to time.
I don't know how i haven't seen this.
Are you referring to the dt variable, or
love.graphics.newImage? Do you still have questions about it?
matu wrote: ↑Fri May 29, 2020 11:32 pm
Thanks, I know I'm supposed to know certain things about Lua (which I don't quite know) to start with love2d so thanks for the explicit information.
I started that way too - jumping into Love without knowing Lua. It's okay - the people on this forum are awesome at helping out! Keep asking questions! You'll learn lots.