HUMP Gamestate delete state

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
Plunky_Bob
Prole
Posts: 1
Joined: Mon Jan 24, 2022 4:21 am

HUMP Gamestate delete state

Post by Plunky_Bob »

Hi everyone,
First time posting here.

I am trying to create a collection of minigames that would ultimately be loaded on a raspberry pi zero 2.
I'm using HUMP gamestate to handle gamestates with roughly 1 gamestate = 1 minigame.

However since I have memory constraints, I'd like to be able to load only one gamestate at any given time.
The problem is, it seems that the HUMP gamestate library keep a reference of the gamestate in memory and that nevers gets garbage collected.

My project is a bit big to post here but here would be an example to reproduce this:

My main.lua:

Code: Select all

gamestate = require "libs.hump.gamestate"
Class = require "libs.hump.class"

function love.load()
	--HOLDS ALL FILE PATHS FOR ALL THE SCENES
	global_register_for_scenes = {"scenes.scene_1.scene",
								  "scenes.scene_2.scene",
								  "scenes.scene_3.scene",
								  "scenes.scene_4.scene",
								  "scenes.scene_5.scene",
								  "scenes.scene_6.scene",
								  "scenes.scene_7.scene",
								  "scenes.scene_8.scene",
								  "scenes.scene_9.scene",}
	--THE INDEX FOR SCENES
	global_scene_index = 1

	-- GAMESTATE INITIALISATION
	-- NEEDED FOR HUMP GAMESTATE TO WORK
	gamestate.registerEvents{'draw', 'update', 'quit'}

	-- CHANGES TO first Scene
	gamestate.switch(require(global_register_for_scenes[global_scene_index]))
end

function love.update()

end

function love.draw()

end

function love.keyreleased(key)
	--QUIT ON ESCAPE 
   if key == "escape" then
      love.event.quit()
   end
   -- PRESS N/right/D TO GET TO NEXT ONE
   if key == "n" or key == "right" or key == "d" then 
	global_scene_index = global_scene_index + 1
	if global_scene_index > #global_register_for_scenes then global_scene_index = 1 end
	gamestate.switch(require(global_register_for_scenes[global_scene_index]))
   end
end
And each scene looks like this:

Code: Select all

local Scenename = {}

function Scenename:init()

	self.direction_choice = {-1, 1} --clockkwise or counterclockwise
	
	self.background_color = {math.random(), math.random(), math.random()}
	
	self.image_holder = {}
	
	for ix =0, love.graphics.getWidth( ), 50 do 
		for iy =0, love.graphics.getHeight( ), 50 do 
			--CREATE OBJECTS TO FILL THE SCREEN WITH RANDOM ROTATION START
			local new_obey = {x = ix,
							  y = iy , 
							  r = math.random(os.time()),
							  w = 100, 
							  h = 100,
							  img = love.graphics.newImage( "img/obey.png"),
							  direction = self.direction_choice [math.random(2)],
							  speed = math.random() * 1.5}
			table.insert(self.image_holder, new_obey)
		end
	end

end

function Scenename:update(dt)

	for _, v in pairs(self.image_holder) do 
		v.r = (v.r + dt * v.speed * v.direction)%(2*math.pi) --rotation 
	end
	
end

function Scenename:draw(dt)
	--DRAW BACKGROUND
	love.graphics.setBackgroundColor(self.background_color)

	--DRAW IMAGE
	for _, v in pairs(self.image_holder) do 
		love.graphics.draw(v.img, v.x, v.y, v.r, 0.5,0.5, 50, 50)
	end
	--FPS COUNTER
	love.graphics.setColor(1,1,1)
	love.graphics.print(love.timer.getFPS( ))
	
end

function Scenename:enter(previous)
	--try deleting previous gamestate
	previous = nil -- Does not work
	-- Try garbagecollect
	collectgarbage("collect")  -- Does not work
end

return Scenename
I don't think I have any global variable lying around in each gamestate.
And I don't even store the gamestates in a variable but load them directly in HUMP gamestate.

So the question is:
- Is there somewhere within the HUMP gamestate library that references / copy each gamestate?
- Is it possible to delete / unreference / destroy a gamestate?
- Where does HUMP stores the loaded gamestates.

Gamestate does create a "stack" and a "initialized_states" table but none seems to reference the loaded gamestates.

The example attached, used around 70 MB of memory at the start and 150 MB when all 9 scenes have been loaded.
(press n/right/d to load next scene)
Attachments
memory_question.love
(388.56 KiB) Downloaded 155 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 20 guests