Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Current Directory for "require"

Current Directory for "require"

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by MUSHuser   (12 posts)  Bio
Date Thu 06 Jan 2022 09:22 AM (UTC)

Amended on Thu 06 Jan 2022 02:31 PM (UTC) by MUSHuser

Message
I have not played a MUD in over 5 years, so this might be something stupid I am missing.

I have a directory that includes my world file, my external script file, and another Lua file that is being required in the script file. So, to illustrate:
C:\MUSHuser\world.MCL
C:\MUSHuser\scriptfile.lua
C:\MUSHuser\test.lua
scriptfile.lua is just one line of:
require "test"

Upon startup, I get this error:
[string "Script file"]:1: module 'test' not found

My MUSHclient is installed in C:\Program Files (x86)\MUSHclient, which I know is probably frowned upon. However, that was also the case when I was playing 5+ years ago. The directory with my world and scripts was also a copy from before. So, the only thing that should have changed is the version of MUSHclient (and the included Lua).

Why is the require not searching in the same directory of the script file that is calling it? When I edit the script to print(GetInfo(64)), it shows that the current directory is in C:\Program Files (x86)\MUSHclient. This is really strange. Is it possible that previously I had a Global Preference that initialized Lua in a different way?

Thanks in advance for any help that is provided.
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Thu 06 Jan 2022 04:57 PM (UTC)

Amended on Thu 06 Jan 2022 05:08 PM (UTC) by Fiendish

Message
Quote:
I have a directory that includes my world file, my external script file, and another Lua file that is being required in the script file. So, to illustrate:
C:\MUSHuser\world.MCL
C:\MUSHuser\scriptfile.lua
C:\MUSHuser\test.lua
scriptfile.lua is just one line of:
require "test"


You could just not do that. Putting these files outside of the MUSHclient folder standard structure (world file goes in MUSHclient/worlds, lua require files go in MUSHclient/lua, ...) can cause many things to break from violated expectations.

Quote:
scriptfile.lua is just one line of:
require "test"

Upon startup, I get this error:
[string "Script file"]:1: module 'test' not found

When you call require it isn't looking in some random place on your hard drive because Lua's require doesn't work with arbitrary relative paths by default. But you could put test.lua in MUSHclient's lua folder if you want it to be found.

Quote:
My MUSHclient is installed in C:\Program Files (x86)\MUSHclient, which I know is probably frowned upon. However, that was also the case when I was playing 5+ years ago.

I don't care what you were doing 5+ years ago. It's a bad idea to put MUSHclient in UAC protected locations like Program Files or %System Root% because MUSHclient writes files to its own directory and Microsoft doesn't like that.

Quote:
Why is the require not searching in the same directory of the script file that is calling it?

Because MUSHclient doesn't add the script location to the package search path.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #2 on Thu 06 Jan 2022 05:06 PM (UTC)
Message
@Nick, having said the above, it would be possible for MUSHclient to add the script's location to package.path, yes?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by MUSHuser   (12 posts)  Bio
Date Reply #3 on Thu 06 Jan 2022 09:37 PM (UTC)
Message
Thanks for your reply!

So just to confirm, it is likely that between then and now, something changed in Lua so that it no longer looks for required libraries in the same directory as the script file?

I guess I can hardcode a package.path to point to the directory of my Lua files. Thanks again.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #4 on Fri 07 Jan 2022 06:16 AM (UTC)
Message

I would use “dofile” rather than “require” if you want to specify an absolute path (which may or may not be a good idea).

it would be possible for MUSHclient to add the script’s location to package.path

I suppose.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #5 on Fri 07 Jan 2022 09:28 PM (UTC)

Amended on Fri 07 Jan 2022 09:32 PM (UTC) by Fiendish

Message
Quote:
So just to confirm, it is likely that between then and now, something changed in Lua...?

No, that's not what I said.

Quote:
I guess I can hardcode a package.path to point to the directory of my Lua files. Thanks again.

Or you can put your files in the expected locations inside the MUSHclient folder where it will always know to look. Or you can use dofile instead of require as Nick said.

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,356 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.