HUMP: Gamestates. Cleanup question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
fairenough
Prole
Posts: 12
Joined: Fri Aug 11, 2017 4:44 pm
Contact:

HUMP: Gamestates. Cleanup question

Post by fairenough »

Hi all,

I'm trying to wrap my brain around Hump's gamestate functions. I've got them working in that I can switch from my main menu to my "gameLevel1" and back again. The issue is, every time I switch back and forth, it seems like all my enemy and pickup generators are doubled. If I switch back and forth a few times, my screen is basically covered with bad guys!

I'm guessing it's because a few variables I've declared in gameLevel1 are global, and are not getting wiped out when I change gamestates. I'm not sure how to get around this though, while still having access to those variables in more than just the gameLevel1.lua

So for example in the code below, UpdateList is global, but I reference it in Enemies, Particles, and Pickups.
Also, enemyController, particleController, and pickupController are all global, but the same thing: they are referenced in other areas.

Do I have to make everything here local and pass it all into everything that needs it? Or am I barking up the wrong tree entirely?

gameLevel1.lua:

Code: Select all

Gamestate = require 'libs.hump.gamestate'
UpdateList = require 'utils.UpdateList'
Timer = require "libs.hump.timer"

local Backgrounds = require 'controllers.Backgrounds'
local Enemies = require 'controllers.Enemies'
local Particles = require 'controllers.Particles'
local Pickups = require 'controllers.Pickups'

local gameLevel1 = {}

local Player = require 'entities.player'

local src1 = love.audio.newSource('assets/seafloor.mp3')

debugtext = {}

local times = {}



function gameLevel1:enter()
  UpdateList:enter()
  enemyController = Enemies()
  pickupController = Pickups()
  backgroundController = Backgrounds()
  
  local player = Player(100, 50, exp)
  UpdateList:add(player,2)
  
  -- Do particles last so they end up on top of layers visually
  particleController = Particles()
  
  src1:play()
  
end

function gameLevel1:leave()
  UpdateList:RemoveAll()
end

function gameLevel1:update(dt)
  UpdateList:update(dt)
  Timer.update(dt)
    
  -- Debug Info
  while #debugtext > 40 do
    table.remove(debugtext, 1)
  end
--  debugtext[#text+1] = string.format("Enemy Count: %s, %s",#enemyController.yellowPlane.list, #enemyController.redPlane.list )
--  debugtext[#text+1] = string.format("Audio Position: %.2f",src1:tell("samples"))
end

function gameLevel1:draw()
  UpdateList:draw()
  for i = 1,#debugtext do
      love.graphics.setColor(0,0,0, 255 - (i-1) * 6)
      love.graphics.print(debugtext[#debugtext - (i-1)], 10, i * 15)
    end
  love.graphics.setColor(255,255,255)
  
end

function gameLevel1:quit()
  if times[1] then
    local timefile = io.open("output/output.csv","w")
    local st = ""
    for i, t in pairs(times) do
      st = st .. t .. ","
    end
    timefile:write(st)
    timefile:close()
  end
end

function gameLevel1:keypressed(key)
  if key == "space" then
    table.insert(times, src1:tell("samples"))
  end
end

return gameLevel1
EDIT: I just realized that I posted this in General instead of Support, and apparently it won't let me delete and re-post. Sorry everyone! Sorry!
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: HUMP: Gamestates. Cleanup question

Post by Sir_Silver »

Have you put a print command in your gameLevel:leave() function to see if it's actually being called? If things aren't removing, any functions related to removing stuff is where I'd start looking first.

It might be nice to see exactly how UpdateList:Add() and UpdateList:RemoveAll() work.
User avatar
fairenough
Prole
Posts: 12
Joined: Fri Aug 11, 2017 4:44 pm
Contact:

Re: HUMP: Gamestates. Cleanup question

Post by fairenough »

Sorry to not answer your question directly, but it seems adding a Timer.clear() to the leave function did the trick.

But I guess my larger question is, should I be making all of those variables local, and passing them to things, rather than making them global? I guess this is more of a large scale structural question than anything else.
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: HUMP: Gamestates. Cleanup question

Post by erasio »

It is generally advisable to have as few global variables as possible.

That doesn't mean that only few things should be available globally. All of love is behind a single global variable (well... behind "love" :D).

Doing this means you will have less risk of name collisions and accidentally overwriting data or using data you didn't intent to. Hiding all global data behind a few tables definitely makes sense.

Whether you want things available globally or not... well. That's on a case by case basis. Unless you truly need them globally (everywhere) I'd suggest passing them on.

Obviously I'm not sure what you're doing. But a solution when you sometimes need it in a different state would be gamestate.push() and gamestate.pop().

They initiate a new gamestate (call enter and leave) but *only* on the new gamestate. The old gamestate simply doesn't get function calls anymore but is kept alive as is. And as soon as you call pop, it will go back to the suspended one.

This allows you to properly cleanup when leaving the state while keeping all data available in things like shops or so.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests