resource consumption issues

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
papainoel14
Prole
Posts: 10
Joined: Fri Mar 13, 2020 3:59 pm

resource consumption issues

Post by papainoel14 »

I was trying to create a game, but over the course of the game, resources will start to rise until the point of the game is consuming an unjustifiable number of resources

I posted the entire code in the mega if you want just take a look
https://mega.nz/#!EP4QEA7K!vY-7isgqT5YW ... clz94AwDGo
Ninguem
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: resource consumption issues

Post by pgimeno »

Hi papainoel14, welcome to the forums.

As a new member I understand that you don't have permission yet for attaching files. But as soon as you do, please attach the project to the thread.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: resource consumption issues

Post by zorg »

I was brave and downloaded the random rar archive! :P

First, since you multiply your time variable with dt, code like this is not guaranteed to run:

Code: Select all

if time == 30 then
    collectgarbage()
  end
I'd suggest testing for math.floor(time) instead.

Second, this should be obvious, but spamming collectgarbage() in your code won't fix the inherent issue :3

sadly i didn''t find anything glaringly responsible for whatever you'd consider unjustifiable memory consumption. :v
i did attach your main.lua for others that don't want to dl stuff from other places though.
main.lua
(12.71 KiB) Downloaded 152 times
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: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: resource consumption issues

Post by pgimeno »

Thanks zorg. I don't have the images so I can't run the project. I do see calls to love.graphics.newFont in the draw function, though. I'd follow the advice in the wiki's big yellow warning sign and see if that helps.
User avatar
papainoel14
Prole
Posts: 10
Joined: Fri Mar 13, 2020 3:59 pm

Re: resource consumption issues

Post by papainoel14 »

I don't know if it makes any difference but I tried to upload it to git, it is more common and easy to view.
I'll take a look at the yellow warning
https://github.com/NinguemLPJ/gameTest
pgimeno wrote: Fri Mar 13, 2020 10:45 pm Hi papainoel14, welcome to the forums.

As a new member I understand that you don't have permission yet for attaching files. But as soon as you do, please attach the project to the thread.

Thank you very much for your courage and help, I will try to make the suggested changes, as soon as I am done I will give feedback
zorg wrote: Sat Mar 14, 2020 7:52 am I was brave and downloaded the random rar archive! :P

First, since you multiply your time variable with dt, code like this is not guaranteed to run:

Code: Select all

if time == 30 then
    collectgarbage()
  end
I'd suggest testing for math.floor(time) instead.

Second, this should be obvious, but spamming collectgarbage() in your code won't fix the inherent issue :3

sadly i didn''t find anything glaringly responsible for whatever you'd consider unjustifiable memory consumption. :v
i did attach your main.lua for others that don't want to dl stuff from other places though.
main.lua
Ninguem
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: resource consumption issues

Post by pgimeno »

Yes that helped, thanks.

I've tried creating the two fonts in advance and the memory no longer kept growing.
User avatar
papainoel14
Prole
Posts: 10
Joined: Fri Mar 13, 2020 3:59 pm

Re: resource consumption issues

Post by papainoel14 »

:awesome:
I can't believe it was just that
ty you so much
pgimeno wrote: Sun Mar 15, 2020 10:50 am Yes that helped, thanks.

I've tried creating the two fonts in advance and the memory no longer kept growing.
Ninguem
User avatar
papainoel14
Prole
Posts: 10
Joined: Fri Mar 13, 2020 3:59 pm

Re: resource consumption issues

Post by papainoel14 »

I decided to redo the project in a more organized way and I came across another problem of unnecessary use of resources.

When you press two movement keys at the same time the resource consumption starts to go up, I could not understand the meaning.
@edit
I made a change, I don't remember what now, but it seems that consumption is ok,
unfortunately i don't have the backup to compare.
Another thing I wanted help with was:
how can i fix my animation.
PS: librarie: anim8
@edit
attached the link and file for anyone who wants to take a look and give me a help
https://github.com/NinguemLPJ/games/tree/master/Jogo_1
Attachments
Jogo_1.rar
Full code
(2.52 MiB) Downloaded 147 times
Ninguem
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: resource consumption issues

Post by pgimeno »

I haven't used anim8, so I don't know how it works, but I think this is the same case as before, but this time with the animations instead of the fonts.

When you go diagonally, the problem is that you're creating many more animations. For example, if you press W and D, first this code executes:

Code: Select all

    if love.keyboard.isDown("w") then
      player.body:setY(player.body:getY() - player.speed * dt)
      if player.andandoW ~= true then -- and not player.andandoA == true and no$
        player.animation = anim8.newAnimation(player.grid('1-9',9), 0.1)
        player.andandoD = false
        player.andandoA = false
        player.andandoS = false
        player.andandoW = true
        player.parado = false
      end
    end
andandoW is initially false, and this code sets it to true. It also sets andandoD to false. Then this code executes:

Code: Select all

    if love.keyboard.isDown("d") then
      player.body:setX(player.body:getX() + player.speed * dt)
      if player.andandoD ~= true then
        player.animation = anim8.newAnimation(player.grid('1-9',12), 0.1)
        player.andandoD = true
        player.andandoA = false
        player.andandoS = false
        player.andandoW = false
        player.parado = false
      end
    end
But now andandoD is false (because you set it to false when pressing W), and the animation is created again, and also andandoW is set to false again. In the next frame, both animations will be created again, and so on. You are creating animations at a very high rate, and the garbage collector can not cope with that speed of object creation.

This is also the reason why your player isn't animated when you go diagonally. The animation frame is reset to zero every screen frame, because you're re-creating the animation every time.

Instead of calling newAnimation every time, prepare each animation in variables in advance, and then assign it to player.animation to change the animation. I don't know if you will need to reset the frame to the first too (using gotoFrame, I suppose), because as I said, I haven't used anim8.

Note that the program, as it appears in GitHub, won't work when you pack it into a .love file, because of a mismatch in upper-case and lower-case filenames. In Libraries/libraries.lua, in the first line, you have:

Code: Select all

anim8 = require('libraries/anim8-master/anim8')
However, the folder is called Libraries, with upper case. Similarly, in main.lua line 10, you have:

Code: Select all

require('Load_Sprites/Load-sprites')
However, the file is called load-sprites, with lower case. These problems are also visible in Linux (which is my system) and MacOS because the filesystems are case-sensitive.

Another note: Instead of using love.keyboard.isDown, use love.keyboard.isScancodeDown. That will allow people with different keyboard layouts to play, For example, in a French keyboard, WASD becomes ZQSD.

Now, you ask how to organize the animations. Here's my advice.

Do not set the animation when pressing keys. Instead, place each key state in a local variable, and use that to change the animation state. That animation state is the only thing that needs to be in the player object. To update the position, store the new position into local variables as well.

Code: Select all

player.animState = ""

local playerGrid = anim8.newGrid(64, 64, player.image:getWidth(), player.image:getHeight()) -- 13 x 21

local playerAnimations = {}
playerAnimations["stopped"] = anim8.newAnimation(playerGrid('1-7',3), 0.1)
playerAnimations["walk left"] = anim8.newAnimation(playerGrid('1-9',10), 0.1)
playerAnimations["walk right"] = anim8.newAnimation(playerGrid('1-9',12), 0.1)
playerAnimations["walk up"] = anim8.newAnimation(playerGrid('1-9',9), 0.1)
playerAnimations["walk down"] = anim8.newAnimation(playerGrid('1-9',11), 0.1)
-- If you don't have animations for the diagonals, you can use this:
playerAnimations["walk up left"] = playerAnimations["walk left"]
playerAnimations["walk up right"] = playerAnimations["walk right"]
playerAnimations["walk down left"] = playerAnimations["walk left"]
playerAnimations["walk down right"] = playerAnimations["walk right"]

player.animation = playerAnimations["stopped"]

function playerUpdate(dt)
  if gameState == 0 then
    return
  end
  local andandoW = love.keyboard.isScancodeDown("w")
  local andandoA = love.keyboard.isScancodeDown("a")
  local andandoS = love.keyboard.isScancodeDown("s")
  local andandoD = love.keyboard.isScancodeDown("d")

  local newX = player.body:getX()
  local newY = player.body:getY()

  local oldAnimState = player.animState

  -- Update vertical movement and animation state
  player.animState = "stopped"  -- we start here, we refine it below
  if andandoW ~= andandoS then -- one is true, the other is false - means we're moving up or down
    if andandoW then -- walking up
      newY = newY - player.speed * dt
      player.animState = "walk up"
    else -- walking down
      newY = newY + player.speed * dt
      player.animState = "walk down"
    end
  end

  if andandoA ~= andandoD then -- one is true, the other is false - means we're moving left or right
    if andandoA then -- walking left
      newX = newX - player.speed * dt
      -- We have already updated animState with the vertical movement. Use it to handle diagonals.
      if player.animState == "walk up" then
        player.animState = "walk up left"
      elseif player.animState == "walk down" then
        player.animState = "walk down left"
      else -- vertically stopped
        player.animState = "walk left"
      end
    else -- walking right
      newX = newX + player.speed * dt
      if player.animState == "walk up" then
        player.animState = "walk up right"
      elseif player.animState == "walk down" then
        player.animState = "walk down right"
      else -- vertically stopped
        player.animState = "walk right"
      end
    end
  end

  if player.animState ~= oldAnimState then
    -- The animation needs to be updated because the animation state has changed.
    player.animation = playerAnimations[player.animState]
    player.animation:gotoFrame(1)  -- maybe?
  end

  -- This is not the best way because it won't handle collisions properly,
  -- but it works the same as in your current code.
  player.body:setX(newX)
  player.body:setY(newY)
end
User avatar
papainoel14
Prole
Posts: 10
Joined: Fri Mar 13, 2020 3:59 pm

Re: resource consumption issues

Post by papainoel14 »

+rep
pgimeno

Ty so much :awesome:
Ninguem
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests