stateful.lua

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: stateful.lua

Post by kikito »

vrld wrote:(Good stuff)
That doesn't implement all stateful, but it's more than enough for Game state management, I think. Well done!
Jack5500 wrote:I don't really get how my game structure (update,load,draw) fits into the game.lua. Those are just functions.
Well, the idea is that you initially create a (possibly global) variable called game inside love.load:

Code: Select all

require 'middleclass'
Stateful = require 'Stateful'
require 'game'

function love.load()
  game = Game:new()
  game:gotoState("MainMenu")
end
Then the rest of the usual functions can just "depend" on game - like this:

Code: Select all

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

functino love.update(dt)
  game:update(dt)
end
Then, game "controls" what to do and what to draw. Inside the MainMenu state, there will probably be something like this:

Code: Select all

function MainMenu:startButtonPressed()
  self:gotoState('Play')
end
That line will automatically call Game.states.MainMenu:exitedState() and Game.states.Play:enteredState(), and set the new state to "Play". Then Play can define its own update, & draw etc.

Notice also that the way I pictured here is not the only possible one. You might, for example, need to add more "updates" inside love.update. Or maybe you might not want to use the game inside love.draw. Or maybe you will also want to use it inside love.keypress. It all depends on how you want to structure your game.
Last edited by kikito on Mon Dec 26, 2011 2:17 pm, edited 1 time in total.
When I write def I mean function.
User avatar
Jack5500
Party member
Posts: 148
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: stateful.lua

Post by Jack5500 »

Thanks for the explaination. You, sir, are the god of all explaining. One question is left for me tho: Does the Menu:enteredState() equal the love.load(), just for this state?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: stateful.lua

Post by kikito »

Thanks for the explaination. You, sir, are the god of all explaining.
Thanks :)
One question is left for me tho: Does the Menu:enteredState() equal the love.load(), just for this state?
It's very similar, yes. The difference is that love.load is called only once, when the game starts. <statename>:enteredState() is called automatically every time that state is entered, which can be more than once (every time you displayed the menu, or started a game, for example).
When I write def I mean function.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: stateful.lua

Post by Ellohir »

I see huge amounts of benefits from using middleclass and this library. I also see huge amounts of refactoring in my future xD
User avatar
Jack5500
Party member
Posts: 148
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: stateful.lua

Post by Jack5500 »

I used yesterday completly implementing stateful and it's absolutly awesome. It made my lua coding more clear and structurized. But I have another question:

I'm having a setup for the level AND the player in the same "play" state right now. But I'd like to make the setup for the level external in another state. That would require the play state (player, bg, etc) to run and within this state another state like level01. Is that possible and if it is, how would I do that? :)

Thanks for your amazing work again :)
c00ld00d
Prole
Posts: 5
Joined: Wed Jan 18, 2012 6:56 am

Re: stateful.lua

Post by c00ld00d »

Hello, I'm very new to LOVE2D and I have absolutely no idea how to start in making th main menu. I already made the base of my game, it's a platformer and has 5 levels. I have your middleclass.lua and stateful.lua, and made a game.lua according to a wiki. But I still have no idea how to create a button or change the background in the menu. Any help? I can show you all the code if you can help.

Thanks in advance!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: stateful.lua

Post by kikito »

Hi there!

Middleclass and/or stateful don't really provide a way to create buttons - they are "logical" stuff, not "graphical".

Menus and buttons appear simple to do, but they can be very complex to do if you want all the bells and whistles - for example a fade-in/fade-out color when the mouse hovers in/out the buttons, how the spacing works, etc.

There are some libs out there to simplify this. I'll try to prepare a simple example in some hours.
When I write def I mean function.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: stateful.lua

Post by MarekkPie »

I recommend Gwee. It's simple, comes with a nice tutorial, and has documentation with the download.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: stateful.lua

Post by kikito »

Hi there,

Finally I didn't use any external libs; creating a very basic lib for menus only was easy enough so I created my own.

You may find the .love file attached in the OP.

The file that draws and manages the menus is lib/menu.lua . You can see examples of how to use it in gamestates/main_menu.lua and gamestates/options_menu.lua .

I've also included several states on several files, to illustrate how the states can be organized. Brief explanation:
  • The loading state doesn't do anything! It just shows a countdown, but doesn't load files at all. I just put it there to remind that a loading state is sometimes useful.
  • Notice how each game state implements draw, keypress, mousepress, etc independendly, although there are values by default to each of those methods in game.lua .
  • I believe I didn't need to use game:update() in any of the states, but it's so important that I have left it there in the demo.
  • OptionsMenu is a substate of MainMenu. As a consequence, I can reuse some of the functions of MainMenu without having to write them (for example, the draw() method is inherited).
  • Notice that the NotImplemented state is inserted with pushState instead of with gotoState. This way I can "go back" to the previous state by simply using self:popState(), without having to "remember" the previous state.
Quiz: the demo as a bug on the NotImplemented state. Can anyone guess it?

I noticed a couple of (very minor) bugs on stateful.lua while doing this demo. I fixed them for the demo, but I haven't updated them on the github repo yet. Will do it either today or tomorrow.
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: stateful.lua

Post by TechnoCat »

kikito wrote:I noticed a couple of (very minor) bugs on stateful.lua while doing this demo. I fixed them for the demo, but I haven't updated them on the github repo yet. Will do it either today or tomorrow.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest