#Game State Machine# help required!!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Master Malakai
Prole
Posts: 3
Joined: Sun Sep 16, 2018 6:24 am

#Game State Machine# help required!!

Post by Master Malakai »

So I pulled some code from a tutorial made by a fellah named Goature.

Here's the link to the video: https://www.youtube.com/watch?v=L_OrFXy ... 1&index=15

Here's my giHub with current state of the "game": https://github.com/MasterMalakai/My-Basic-Game

So far it will load the mainMenu state and from there I can load the game, but I cannot return to the mainMenu state and I can't quite get my head around why. I'm new to all this so still learning but I I have been told that it has something to do with the fact I'm no longer in the 'root' directory.

Instead of the actual answer, maybe an explanation of the concept of what is happening here and the kinds of tools that I can use to work around it. I really would like to figure it out on my own.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: #Game State Machine# help required!!

Post by milon »

Have you worked with multiple .lua files before? I'm thinking that's where the problem is. I haven't learned game states myself (working on it, LOL) but your main.lua file that's in the ZombieGame doesn't require any other files, which means it's not going to link things up properly. At least, when I ran your main.lua, nothing happened. I didn't try any of the other main.lua files. (FYI, I think convention is to only have one main.lua file, and the rest should be named according to what they do.)

Here's a small sample of how main.lua can call another file. It took me a while to really understand this, so take your time with it and ask questions! :)

This example involves 2 files placed in the same folder. The files are called main.lua and otherLuaFile.lua.


main.lua :

Code: Select all

function love.load()
	
	-- include an external file
	-- note that the path is relative to this file
	-- also note that the .lua extension is NOT included
	someModule = require "otherLuaFile"
	
	-- You can't call another file's LOCAL functions
	-- If you uncomment the line below, the program will crash
	--love.graphics.print(someModule.helloWorld(), 100, 200)

end

function love.draw()
	
	-- This will succeed because
	-- someModule holds what was returned from otherLuaFile.lua
	someModule.drawThings()

end

otherLuaFile.lua :

Code: Select all

-- This file gets included with the _require_ command in main.lua

-- WORD WALL EXPLANATION:
-- This took me a long time to figure out myself, so take your time
-- The big picture idea is that we're creating a table (M) and putting
-- everything in M, and lastly we return M back to main.lua
-- Note that main.lua assigns everything in M to someModule
-- We never really use M, except here.  M is purely used for convention.
-- You could call it whatever you like instead of M.
-- someModule.FUNCTION_NAME is how we reference these functions from
-- the main.lua file.


-- Create containing table M (as local!)
local M = {}


-- This is a local function
-- Local functions CANNOT be called from external files
-- They can be called ONLY from other functions within this file
-- This simple function just returns a string of text
local function helloWorld()
	return "Hello world!"
end

-- This is a global function
-- It can be called from external files
-- In this case, it's referenced as someModule.drawThings()
-- Remember, M gets assigned to someModule in main.lua
function M.drawThings()
	
	local str = helloWorld()
	-- Remember that you can't call the local functions from other files
	-- But local functions can always be called from other functions
	-- within the same file
	-- To prove it, let's use that string to do something visible
	love.graphics.print(str, 100, 100)
end

return M
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Master Malakai
Prole
Posts: 3
Joined: Sun Sep 16, 2018 6:24 am

Re: #Game State Machine# help required!!

Post by Master Malakai »

Inside the main main.lua file, that is to say the one that shares folder space with the states and sprites folder, there is a function, loadState, that accepts a 'name' string argument. The function sets out a file path:

function loadState(name)
clearLoveCallbacks()
local path = "states/" .. name <-------- it sets path to "states/" then concatenates the 'name' of the folder inside the states folder
require(path .. "/main") <------- then requires the main.lua file from that folder
load()
end

The main.lua files inside the states folders don't have anything to require because I haven't really built a game, so there isn't anything to require atm. I was just trying to find an efficient state handler to begin with. I have a couple of different state machines I'm working on atm, it's just I got this one from a tutorial... so I don't really understand how it works and so I can't fix the bugs I have encountered with it, like switching back and forth between multiple states.

One of the first things I learnt was to separate my code into multiple files, makes debugging a load easier.

Thanks for taking the time to reply!
User avatar
CogentInvalid
Prole
Posts: 27
Joined: Sat Dec 14, 2013 12:15 am

Re: #Game State Machine# help required!!

Post by CogentInvalid »

require() doesn't do anything when requiring the same file a second time, in order to prevent lua from doing unnecessary work. Your state switching relies on each file redefining love.update, love.draw, etc. when it's required, but that work will only be done the first time you require the file, and you'll be unable to switch back to that state afterwards.
Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests