Returning to a previous .lua file
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 8
- Joined: Sat Aug 06, 2016 2:41 pm
Returning to a previous .lua file
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
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:
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
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
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
-
- Prole
- Posts: 8
- Joined: Sat Aug 06, 2016 2:41 pm
Re: Returning to a previous .lua file
Thank you for your answer, I have edited the file but there's still a problem with :
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.
Can you help me with this? Thanks a lot.
Code: Select all
function love.draw()
currentState.draw()
end
"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
Re: Returning to a previous .lua file
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.
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.
-
- Prole
- Posts: 8
- Joined: Sat Aug 06, 2016 2:41 pm
Re: Returning to a previous .lua file
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
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
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
-
- Prole
- Posts: 8
- Joined: Sat Aug 06, 2016 2:41 pm
Re: Returning to a previous .lua file
Here it is.
- Attachments
-
test.love
- (111.4 KiB) Downloaded 177 times
Re: Returning to a previous .lua file
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:
Make sure to return the table at the end of the file.
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
-
- Prole
- Posts: 8
- Joined: Sat Aug 06, 2016 2:41 pm
Re: Returning to a previous .lua file
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
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()".
I recommend to look at the source code of other people, stuff like this can be learned easily that way.
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
Who is online
Users browsing this forum: Bing [Bot] and 4 guests