Something that's confusing me in goature's tutorial

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
geleiademocoto
Prole
Posts: 11
Joined: Sat Apr 26, 2014 11:25 pm

Something that's confusing me in goature's tutorial

Post by geleiademocoto »

Hey all. I was here briefly about a year ago, and now I'm trying to get back into game making. Last time I used josefnpat's tutorial to learn and it was great, but this time as I try to refresh my memory I decided to use a different tutorial so I went with goature. It's great, but there's something I'm confused about. It's probably silly, but I'd rather have everything clear in my head than go on unsure.

Anyway, I just finished part 10 - Hitboxes (https://www.youtube.com/watch?v=POVQBI9 ... B05A624D91) and everything works fine. When I click the zeppelin, the console says "Hit!". The thing is after I finish each part I like to go through the "path" of the code to make sure I understood what happened.

In this video, he created

Code: Select all

 function insideBox()
in main.lua which basically detects collision. It checks if a certain position is inside boundaries. Then, in entities.lua, inside of

Code: Select all

function ents.shoot()
, he calls insideBox(), and it works.

What I don't understand is how I was able to call insideBox() from another file, namely entities.lua. Now, main.lua does contain the line

Code: Select all

require("entities")
inside of love.load(). To my understanding, that means that I could call functions from entities.lua in main.lua. Not the opposite. entities.lua isn't requiring anything. How is it that I'm able to access that function?

I suspect there's something about the lua language that I'm missing here.. Could anyone point me in the right direction? When I search for this, I keep running into questions from people who are getting errors and not being able to call a function from a different file. Not my case.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Something that's confusing me in goature's tutorial

Post by airstruck »

It's a global function. Function and variable declarations are not automatically lexically scoped. If they're not declared with "local" they're global.

Of course that only goes for "stand-alone" functions, it doesn't apply to the syntax sugar for declaring "methods" (described in section 2.5.9 of the manual, third form of function definition syntax sugar). Those functions reside in tables and don't need the "local" keyword to keep them out of the global scope.

Using globals like that is probably not a good practice.
geleiademocoto
Prole
Posts: 11
Joined: Sat Apr 26, 2014 11:25 pm

Re: Something that's confusing me in goature's tutorial

Post by geleiademocoto »

I see! Am I right in assuming then that the reason why we still need to use require("entities") is because the functions in entities.lua are in the shape of

Code: Select all

function ents.Startup()
meaning that they're "methods" inside of the table ents? In which case, the "require" is what allows us to access those non-global functions.

If using globals isn't good practice, a better way would be to put the insideBox() function inside of entities.lua as part of its functions then? Or perhaps making a new file with "helper" functions and having entities.lua require it. I'll take a look at the code for games out there on github to see how people usually deal with functions like these. Thanks for your help!
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Something that's confusing me in goature's tutorial

Post by airstruck »

You're right about the functions in the "ents" table. The problem there is that the "ents" table itself is global (notice it's not declared with the "local" keyword). A more modular design would be to make "ents" local, and have entities.lua return the "ents" table at the end, and then require it like this:

Code: Select all

local ents = require 'entities.lua'
I'm not necessarily saying that using globals is never a good practice, just almost never. I think using globals to address cross-cutting concerns might be a legitimate use. I'm still waiting for feedback on that, though.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests