I have a problem with lag

Show off your games, demos and other (playable) creations.
Post Reply
matu
Prole
Posts: 4
Joined: Thu May 28, 2020 3:56 pm

I have a problem with lag

Post 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 :? ?
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: I have a problem with lag

Post 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.
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
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: I have a problem with lag

Post by pgimeno »

And please use the support forum to post questions!
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: I have a problem with lag

Post 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".
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: I have a problem with lag

Post 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. :3
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.
matu
Prole
Posts: 4
Joined: Thu May 28, 2020 3:56 pm

Re: I have a problem with lag

Post 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.
:o: I don't know how i haven't seen this.
Last edited by matu on Fri May 29, 2020 11:33 pm, edited 1 time in total.
matu
Prole
Posts: 4
Joined: Thu May 28, 2020 3:56 pm

Re: I have a problem with lag

Post 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.
matu
Prole
Posts: 4
Joined: Thu May 28, 2020 3:56 pm

Re: I have a problem with lag

Post 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. :3
Thanks for clarifying that it is not necessary to rename the files, you saved me a lot of time.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: I have a problem with lag

Post 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.
:o: 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. :ultrahappy:
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: No registered users and 175 guests