Page 2 of 2

Re: [SOLVED] Constructor function malfunction

Posted: Wed Jul 14, 2021 10:29 pm
by ReFreezed
(Excuse the double posting.)
I noticed another unrelated problem in the code.
applebappu wrote: Tue Jul 13, 2021 3:37 am

Code: Select all

Mob = require "./Classes/Mob"
The module name format isn't good. This will try to load 'PathToProject///Classes/Mob.lua', which I guess accidentally works on Windows because Windows seems to accept multiple directory separators in a row. It should be like this:

Code: Select all

Mob = require "Classes.Mob" -- Use dot as the directory separator.
I think the Lua manual isn't good at describing this.

Re: [SOLVED] Constructor function malfunction

Posted: Wed Jul 14, 2021 11:13 pm
by applebappu
Hm! It works just fine on my Mint system, that's where I'm doing most of my dev work.

Re: [SOLVED] Constructor function malfunction

Posted: Thu Jul 15, 2021 12:43 am
by ReFreezed
I guess it accidentally works on Mint too. It might work in all places LÖVE supports, but if you put this at the top of the module you should see the weird path that the module was loaded from:

Code: Select all

print(debug.getinfo(1).source) -- Will probably print @//Classes/Mob.lua
(Ignore the @ sign.)

It's more evident if you call require"./././Classes/Mob".

Re: [SOLVED] Constructor function malfunction

Posted: Thu Jul 15, 2021 11:01 am
by pgimeno
Also if you don't use a canonical path, you risk loading the same required file more than once. E.g. if you have this in req.lua:

Code: Select all

print("required")
and you require it differently in more than one place:

Code: Select all

require("req")

Code: Select all

require("./req")
you will get "required" printed twice, meaning the file will have been loaded and executed more than once.

The canonical path uses dots for path separators, is relative to the directory where main.lua is, and uses the same letter case as the filenames.

Re: [SOLVED] Constructor function malfunction

Posted: Thu Jul 15, 2021 5:11 pm
by applebappu
Interesting!

(for the record, I took your advice, thank you)

Re: [SOLVED] Constructor function malfunction

Posted: Mon Jun 20, 2022 4:16 pm
by juankax
Hi applebappu,
I have a doubt, answer me when you can to the PM
Thanks