Page 1 of 1

[0.17.41] Relative paths with require

Posted: Mon May 20, 2019 9:39 pm
by Cooldude2606
I am unsure if this is a bug, it may be how lua works however you also altered the require function which is why I am unsure.

While requiring files using relative paths I came accross a bug in my code however I found the cause to be because of lua's package cache; you may already know but require stores required files so that when the file is required again it returns the same table. The error I found was that with relative paths it treated each one as its own package (seen below using log). If this is just something that lua does then im sorry to bother you with this.

Example:

Code: Select all

File Structure
> Mod Root
-> expcore
->-> gui.lua
->-> gui
->->-> left.lua (this file is for left frames)
->->-> toolbar.lua (this file is for top button flow)

Code: Select all

Requires
#gui.lua
require './gui/toolbar'
require './gui/left'
#left.lua
require './toolbar'
#toolbar.lua
log('added button')
The toolbar.lua file had a log function inside which made me notice how it treated them as two different packages; below is log output:
Image

Again just want to say this might not be a bug and may just be how lua works but due to the altered require function I cant be certain that it is just a lua thing.

Re: [0.17.41] Relative paths with require

Posted: Mon May 20, 2019 11:41 pm
by Rseding91
Duplicate of viewtopic.php?f=48&t=67032

Essentially: don't use relative paths with "require".

If I started over I wouldn't support relative paths and would force every file to be absolute including the mod name: "__mod-name__/.../.../..." because that is the only way you will get fully correct behavior with everything.