gamestate will eat memory or not?

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
lex
Prole
Posts: 23
Joined: Fri Nov 28, 2014 4:58 am

gamestate will eat memory or not?

Post by lex »

don't want to eat my memory!
if i use gamesate like this:
will menu still be running behind sceens? and killing my memory!!! or its safe and no killing memory used because of the if statement?? :huh:
:? :? :?

Code: Select all

function love.load()
	scene="game"
	--scene="menu"
end
function love.update(dt)
	if scene=="game" then game.update(dt) end
	if scene=="menu" then menu.update(dt) end
end
function love.draw()
	if scene=="game" then game.draw() end
	if scene=="menu" then menu.draw() end
end


Last edited by lex on Wed Apr 22, 2015 2:40 pm, edited 1 time in total.
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

Re: gamestate will eat memory or not?

Post by Sosolol261 »

It won't :)
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: gamestate will eat memory or not?

Post by s-ol »

Sosolol261 wrote:It won't :)
Wrong, it will "eat memory". The only way to save memory is to have lua not "know about" things, for example by "require"'ing The menu only when needed and clearing the references out of the cached loaded modules, which is a lot of stress for no real reason.

What the code above will do is save processing power, which is probably what you mean anyway because you say "running in the background", which memory can't.

The way you are doing it is perfect, thinking about memory consumption of the menu doesn't make a lot of sense anyway.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
IndieLöver
Prole
Posts: 29
Joined: Mon Jun 10, 2013 8:49 am

Re: gamestate will eat memory or not?

Post by IndieLöver »

Another option.

Code: Select all

function love.load()
   scene=game
   --sceen=menu
end
function love.update(dt)
   scene.update(dt)
end
function love.draw()
   scene.draw()
end
lex
Prole
Posts: 23
Joined: Fri Nov 28, 2014 4:58 am

Re: gamestate will eat memory or not?

Post by lex »

thanks for ALL the reply but
didn't pick this one up what do you mean? here?
can you extend the option so i can understand it??
how will i use the state here?

Code: Select all

    function love.load()
       scene=game
       --scene=menu
    end
    function love.update(dt)
       scene.update(dt)
    end
    function love.draw()
       scene.draw()
    end



User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: gamestate will eat memory or not?

Post by Robin »

I'd like to help you with this, but first I need to know how much you know already, so if you don't mind, I have a few questions for you:

Do you know what, in the context of your game,

Code: Select all

game
means? Can you tell me?

Do you know what, in the context of your game,

Code: Select all

state = game
means? Can you tell me?

Do you know what, in the context of your game,

Code: Select all

game.update(dt)
means? Can you tell me?
Help us help you: attach a .love.
User avatar
IndieLöver
Prole
Posts: 29
Joined: Mon Jun 10, 2013 8:49 am

Re: gamestate will eat memory or not?

Post by IndieLöver »

Oh, sorry, I forgot the important parts!
main.lua

Code: Select all

function love.load()
	require "game"
	require "menu"

	scene=menu
	
	love.graphics.setBackgroundColor(150,50,110)
end

function love.update(dt)
	scene:update(dt)
end

function love.draw()
	scene:draw()
end

function love.mousepressed(x,y,button)
	scene:onClick(x,y,button)
end

function love.keypressed(key)
	scene:keypressed(key)
end
game.lua

Code: Select all

game = {}
	game.lineWidth = 10
	game.font = love.graphics.newFont(16)
	game.planet = {x=love.graphics.getWidth()/2,y=love.graphics.getHeight()/2,r=100,color={190,170,170}}
	game.lines = {count=10,r=50,speed=math.pi,a=0}
	
function game:update(dt)
	--updating line angle
	self.lines.a = self.lines.a + self.lines.speed * dt
	if self.lines.a > math.pi then self.lines.a = self.lines.a - math.pi*2 end
end

function game:draw()
	love.graphics.setLineWidth(self.lineWidth)
	--planet
	love.graphics.setColor(self.planet.color)
	love.graphics.circle("line",self.planet.x,self.planet.y,self.planet.r,self.planet.r*3)
	--lines
	for count = 1,self.lines.count do
		local a = self.lines.a + count / self.lines.count * math.pi*2
		love.graphics.line(self.planet.x+math.cos(a)*self.planet.r,self.planet.y+math.sin(a)*self.planet.r,self.planet.x+math.cos(a)*(self.planet.r+self.lines.r),self.planet.y+math.sin(a)*(self.planet.r+self.lines.r))
	end
	--text
	love.graphics.setColor(180,80,140)
	love.graphics.setFont(self.font)
	love.graphics.print("This isn't really a game, sorry. Press ESCAPE to return to menu.")
end

function game:onClick(x,y,button)

end

function game:keypressed(key)
	scene=menu
end
menu.lua

Code: Select all

menu = {}
	menu.buttons = {
		{text="PLAY",x=love.graphics.getWidth()/2,y=love.graphics.getHeight()*1/4,color={220,190,190},hoverColor={0,0,0},onClick=function() scene=game end},
		{text="EXIT",x=love.graphics.getWidth()/2,y=love.graphics.getHeight()*3/4,color={220,190,190},hoverColor={0,0,0},onClick=function() love.event.push("quit") end},
	}
	menu.font = love.graphics.newFont(24)
	
function menu:update(dt)
	local x,y = love.mouse:getX(),love.mouse:getY()
	self.clickable = nil
	--mouse-button collision
	for i,v in ipairs(self.buttons) do
		if x > v.x - self.font:getWidth(v.text)/2 and
		x < v.x + self.font:getWidth(v.text)/2 and
		y > v.y - self.font:getHeight()/2 and
		y < v.y + self.font:getHeight()/2 then
			self.clickable = v
		end
	end
end

function menu:draw()
	--set font
	love.graphics.setFont(self.font)
	for i,v in ipairs(self.buttons) do
		--set color
		if v == self.clickable then love.graphics.setColor(v.hoverColor)
		else love.graphics.setColor(v.color) end
		--draw button
		love.graphics.print(v.text,v.x,v.y,0,1,1,self.font:getWidth(v.text)/2,self.font:getHeight()/2)
	end
end

function menu:onClick(x,y,button)
	if button == "l" and self.clickable then
		self.clickable.onClick()
	end
end

function menu:keypressed(key)
	if key == "escape" then
		love.event.push("quit")
	end
end
Though I realised, that there are no benefits in this over the usual game state way. I don't know what he **** I was thinking yesterday. Sorry for this inconvinience!
lex
Prole
Posts: 23
Joined: Fri Nov 28, 2014 4:58 am

Re: gamestate will eat memory or not?

Post by lex »

well my goal is not to waster or burn pc memory
as for the question i think gamestate its to display one part of the game and hide the other ones (could be wrong)
state="game"

if state== "game" then
game.load()
end

i am not skillfull enough yet this is so far from what i have learn from seeing others work but
if u can help me understand more I will for ever thank you as for indielover: thank you for your code sure will to study it !!
see my goal is first to learn about how to save memory i don't want to learn other stuff and not knowing i am killing the memory....
i see great games here and i see they dont kill memory i want to learn that, i do believe the if statement its not safe.. and feel
that behind all the game still running and seriously scare the pc will crash...!

Help!
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: gamestate will eat memory or not?

Post by s-ol »

lex wrote:well my goal is not to waster or burn pc memory
as for the question i think gamestate its to display one part of the game and hide the other ones (could be wrong)
state="game"

if state== "game" then
game.load()
end

i am not skillfull enough yet this is so far from what i have learn from seeing others work but
if u can help me understand more I will for ever thank you as for indielover: thank you for your code sure will to study it !!
see my goal is first to learn about how to save memory i don't want to learn other stuff and not knowing i am killing the memory....
i see great games here and i see they dont kill memory i want to learn that, i do believe the if statement its not safe.. and feel
that behind all the game still running and seriously scare the pc will crash...!

Help!
You won't be able to crash your PC by making löve run out of memory. You will also not be able to even get close to reaching the memory limit in regular experimentation, and if you were, an optimization like this wouldn't help you anyway. The if statement is perfectly fine, move on to other things and don't worry about optimization.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
lex
Prole
Posts: 23
Joined: Fri Nov 28, 2014 4:58 am

Re: gamestate will eat memory or not?

Post by lex »

thank all for the answers..
Post Reply

Who is online

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