Page 1 of 2

Returning to a previous .lua file

Posted: Sun Aug 07, 2016 1:50 pm
by PiadinaAlexo
Hello there, i'm currently working on a game, and I'm new to Lua. I am working on the main menu, and when i select the "play" button,via the require function, it brings me to the play.lua file. The problem is that i want to return to the previous menu.lua file, but with the require function it doesn't work. Please help!

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 3:13 pm
by MadByte
Pleeease use the search functionality of the forum before asking.
You don't want to switch your lua files when changing the gamestate. It's much simpler then that.

The "messy/global/voldemort" way:

Code: Select all

-- Menu.lua
Menu = {}
function Menu.draw()
  love.graphics.print("Im the MENU STATE")
end


-- Game.lua
Game = {}
function Game.draw()
  love.graphics.print("Im the GAME STATE")
end


-- Main.lua
require("Menu")
require("Game")
currentState = Menu --set current state

function love.draw()
  currentState.draw()
end

function love.keypressed(key)
  if key == "space" then -- switch currentState
    if currentState == Menu then currentState = Game
    else currentState = Menu end
  end
end
There is an endless amount of ways to implement gamestates. This is the simplest I can think of.
If you want a better way with more options try a gamestate library like hump's gamestate.lua.

Consider to learn what "local" in lua does and how you can use it to improve your code.

Here is an example for hump.
Gamestate Example

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 4:24 pm
by PiadinaAlexo
Thank you for your answer, I have edited the file but there's still a problem with :

Code: Select all

function love.draw()
currentState.draw()
end
Love says:
"attempt to index global 'currentState' (a nil value)

Traceback

in function 'draw'
[C]: in function 'xpcall'"

I don't understand why, but it keeps giving me the error at that function.

Code: Select all

require("MainMenu")
require("PlayMenu")
require("Options")
currentState = MainMenu

function love.draw()
	currentState.draw()
end
Can you help me with this? Thanks a lot.

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 4:52 pm
by MadByte
So it says exactly what you did wrong. This line means that a variable you try to use does not exist.
Go ahead and check if you created the tables properly. This is what coding is all about - trial and error.

You can be sure that "currentState" is not the missing variable. Why? Because you declared it with an = in the code snippet you just posted. My next step would be to check if the table "MainMenu" in your MainMenu.lua has been created.

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 5:11 pm
by PiadinaAlexo
I have checked, and i have created all the tables in the other .lua files. I still don't get this. Every table i have created has always been created like this:

Code: Select all

-- in MainMenu.lua
MainMenu = {}
-- in PlayMenu.lua
PlayMenu = {}

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 5:29 pm
by MadByte
Okay, please pack a *.love file and post it here (or just make a zip file).
in case you dont know how:
1. Select all files inside your project directory
2. zip them (i.e with winrar/7-zip etc)
3. Rename the .zip into .love

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 5:47 pm
by PiadinaAlexo
Here it is.

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 5:54 pm
by MadByte
You've declared the tables as locals. this causes them to exist only inside the file (or even in the function) you declared
them in. If you remove the "local" everything would work as expected. Anyway, it is a good idea to use locals, use them
everywhere™ as long as you feel confortable with them. Read more about the scope of local variables here.

Here is a tip on how to get locals to other files without making them global:

Code: Select all

-- MainMenu.lua
local MainMenu = {}

-- do stuff here --

return MainMenu


-- main.lua
local MainMenu = require("MainMenu")
currentState = MainMenu

Make sure to return the table at the end of the file.

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 6:57 pm
by PiadinaAlexo
Oh, I see. Thanks! By the way, I still encounter a problem. I have removed the local to the tables, but when i run the .love project, it graphically shows MainMenu, but the commands that I can use are the ones for PlayMenu. That's kinda odd, since i've set "MainMenu" as the currentState at the start of the code.

Re: Returning to a previous .lua file

Posted: Sun Aug 07, 2016 7:12 pm
by MadByte
You used "love.keypressed" in both files - this cannot work.
You need to implement new functions for each gamestate just the same way you did with "MainMenu.draw()".

Code: Select all

-- MainMenu.lua
local MainMenu = {}

function MainMenu.init()
end

function MainMenu.update(dt)
end

function MainMenu.draw()
end

function MainMenu.keypressed(key)
end

return MainMenu


-- main.lua
local MainMenu = require("MainMenu")
local currentState = MainMenu

function love.load()
  currentState.init()
end

function love.update(dt)
  currentState.update(dt)
end

function love.draw()
  currentState.draw()
end

function love.keypressed(...) -- "..." just means that all arguments get carried over to the recieving function
  currentState.keypressed(...)
end
I recommend to look at the source code of other people, stuff like this can be learned easily that way.