Page 1 of 1

gamestate will eat memory or not?

Posted: Wed Apr 22, 2015 11:00 am
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



Re: gamestate will eat memory or not?

Posted: Wed Apr 22, 2015 12:06 pm
by Sosolol261
It won't :)

Re: gamestate will eat memory or not?

Posted: Wed Apr 22, 2015 1:45 pm
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.

Re: gamestate will eat memory or not?

Posted: Wed Apr 22, 2015 1:59 pm
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

Re: gamestate will eat memory or not?

Posted: Wed Apr 22, 2015 2:39 pm
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




Re: gamestate will eat memory or not?

Posted: Wed Apr 22, 2015 3:47 pm
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?

Re: gamestate will eat memory or not?

Posted: Thu Apr 23, 2015 7:43 am
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!

Re: gamestate will eat memory or not?

Posted: Thu Apr 23, 2015 2:11 pm
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!

Re: gamestate will eat memory or not?

Posted: Fri Apr 24, 2015 10:41 pm
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.

Re: gamestate will eat memory or not?

Posted: Thu Jun 11, 2015 4:27 am
by lex
thank all for the answers..