[SOLVED] Constructor function malfunction

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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: [SOLVED] Constructor function malfunction

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: [SOLVED] Constructor function malfunction

Post by applebappu »

Hm! It works just fine on my Mint system, that's where I'm doing most of my dev work.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: [SOLVED] Constructor function malfunction

Post 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".
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Constructor function malfunction

Post 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.
applebappu
Prole
Posts: 37
Joined: Thu Jun 24, 2021 5:49 pm

Re: [SOLVED] Constructor function malfunction

Post by applebappu »

Interesting!

(for the record, I took your advice, thank you)
juankax
Prole
Posts: 3
Joined: Mon Jun 20, 2022 3:53 pm

Re: [SOLVED] Constructor function malfunction

Post by juankax »

Hi applebappu,
I have a doubt, answer me when you can to the PM
Thanks
Founder of Eventos BCN and Chofer BCN
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests