Love2D: A state switcher class (Lua)

Simple state switcher class for Love2D / Lua that uses one file per state. It completely covers Love 2D state management for the game developers. Licensed under CC-BY 4.0 / Creative Commons Attribution International 4.0.

Download: (2,7 Kb; for Love2D / Lua)

Code example:

state.switch("credits") -- switching to credits.lua

Code example with passing a value:

state.switch("credits;2;hello") -- passing 2 as the first value, "hello" as the second value

Code example to retrieve the values passed after switch:

step=passvar[1] -- gets the first value = 2
greeting=passvar[2] -- = "hello"
state.clear() -- clearing the values passed, so no zombie values hang around

Other advantages:

  • edit your code & save to see changes immediately after state switch (without leaving your program)
  • reuse the functions from the previous states (to keep same behaviour) or redeclare the functions (to change behaviour)
  • Enterprise

    Works ok except I can’t seem to find a way for global function to find what state/file is loaded. That makes it kind of hard to be useful…

    I hope I missed it…?

  • dusoft

    Well, you don’t really need to know what state you are in, do you? Since it is file based, everything in that file gets executed. So you just expect cc.lua to be cc state, credits.lua to be credits state etc. So, it is implicitly known beforehand what state you are in based on the called file/state.

    Anyway, use variables (see passing values example) to pass on anything you need. Or feel free to update this state manager library as it has been released under CC license.

  • This is -genius-!
    I made a small change though, because I wanted to be able to send tables between states as well. This is how my version looks:


    function state.switch(_state, …)
    passvar = {…}
    state = _state

    function state.clear()

    return state

  • dusoft

    Hey, Fredrik, I am glad you like it and thanks for the update!

  • DmL

    I’ve been using your switcher successfully for a while to switch between test states, and have come across instances where leaving a state and then returning, I find the state in the same condition I left it…

    How do I do this on purpose? ie, so I can return to mainmenu.lua without level1.lua being reset?

    Also, being able to pass tables would be very useful, is that in the version linked, or would I have to apply the other commenter’s code?

  • dusoft

    Hm, that’s strange. Based on the metatables info, the switcher should completely reload (reinclude) the file. Is it possible that you do not initialize all the variables and some get left behind with the values from the previous states?

    For the tables please use Fredrik’s code as it has been tested.

    BTW, I am glad that people actually use this piece of code as I haven’t really expect it to be useful for anyone else than me.

  • DmL

    So, I just replace your function state.switch() with his, correct?

    So, if I want to resume a previous state, I must pass as arguments everything I want to resume?

  • DmL

    I have definitely been using it! I don’t think learning game programming and lua would have been possible for (at least it would have been very much harder) without it!

    How would you like to be credited? So far I have:
    Daniel Duris, (CC-BY) 2014, dusoft[at] for the Stateswitcher Library.

    I tested with the new code, and everything seems to be working. I do have to explicitly require the stateswitcher in each state where I didn’t before… that might have something to do with the persistence. Am I thinking correctly if I had a global table, it would persist through the states, so long as I didn’t return to the lua which declared the table?

  • dusoft

    That’s correct, just replace the old one with his one.

    And yes, your thinking is correct – if you keep any global variable (including a table), it persists through the states until it is declared “nil” (destroyed) or redeclared by loading a different state.

  • DmL

    State variables don’t seem to work with the commenter’s code, unless I’m misunderstanding some new way of formating the passing variables?

  • dusoft

    I can test it over the weekend, but that’s as much as I can do about it. I have sent you Fredrik’s email address, if you want to contact him about that.