State System

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.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

State System

Post by TheMeq »

Hi All, How do you guys put together your state system.

The way Love is at the moment, it's as if it expects your entire program in once script. I've tried stateswitcher, which doesn't work the way I expected it to, and I'm now looking at HUMP, which looks extremely complicated to just switch over to another state. Maybe i'm missing something.

Also, what is the point of love.load when all my variables can be loaded outside of love.load as well?
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: State System

Post by TheMeq »

Another thing, why does Love work the way it does? Why put all the update and draw methods in there own functions? Why not just have a loop with both of them in?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: State System

Post by MadByte »

TheMeq wrote: ...and I'm now looking at HUMP, which looks extremely complicated to just switch over to another state. Maybe i'm missing something.
Actually it's pretty simple:

Code: Select all

-- main.lua
Gamestate = require("hump.Gamestate")
Game = require("Game")
function love.load()
  Gamestate.switch(Game)
  Gamestate.registerEvents()
end
Thats all you need to load a state and each callback (update, draw, keypressed, etc) get registered automatically.
The code in your State should look like this:

Code: Select all

local Game = {}

function Game:init() -- gets called the first time you load this state
end

function Game:enter() -- gets called everytime you switch to this state
end

function Game:leave() -- gets called when you switch to another state
end

function Game:update(dt)
end

function Game:draw()
end

return Game
Thats it.
TheMeq wrote: Also, what is the point of love.load when all my variables can be loaded outside of love.load as well?
It's possible to pass command line arguements from outside of the application to love.load. That might be a reason.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: State System

Post by TheMeq »

How should the files be laid out in relation to my main.lua?

Would I create a game.lua and put those function in that file?

Thanks
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: State System

Post by TheMeq »

Ok, I think I got it, but it's throwing up an error.

hump/gamestate.lua:40: attempt to index local 'to' (a boolean value)
Attachments
BitRhythm.zip
(75.77 KiB) Downloaded 150 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: State System

Post by ivan »

TheMeq wrote:Another thing, why does Love work the way it does? Why put all the update and draw methods in there own functions? Why not just have a loop with both of them in?
Because you can't always draw on the screen whenever you want.
Like for example, if vsync is involved.
Also, no reason to draw on the screen more frequently than 1/60 of a second as your eyes won't even notice it.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: State System

Post by TheMeq »

ivan wrote:
TheMeq wrote:Another thing, why does Love work the way it does? Why put all the update and draw methods in there own functions? Why not just have a loop with both of them in?
Because you can't always draw on the screen whenever you want.
Like for example, if vsync is involved.
Also, no reason to draw on the screen more frequently than 1/60 of a second as your eyes won't even notice it.
Thanks for clarifying ivan ^_^
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: State System

Post by TheMeq »

Figured it out, forgot to return at the end. :D
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: State System

Post by bobbyjones »

Man be careful with double posting. The admins are old(jk) and not used to the newer forms of communication where double posting is acceptable. :P
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: State System

Post by TheMeq »

Sorry >_<
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 65 guests