Jump to content




require in dofile()


4 replies to this topic

#1 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 20 May 2018 - 11:37 AM

To make it short; you cant use require in dofile:
dofile_test.lua:
dofile("require_useing_file.lua")
require_useing_file.lua:
require("things_i_want_to_require")

It works in normal lua just fine, but in CC it says require does not exist:
dofile_test.lua:1: attempt to call global 'require' (a nil value)

Is this a bug or why is it different from normal lua?

Edited by Jummit, 20 May 2018 - 12:43 PM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 20 May 2018 - 11:52 AM

Sorry, what was the question? Are you asking for a workaround, or just looking for confirmation of a bug?

#3 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 20 May 2018 - 11:54 AM

This is because require exists in the shell environment, but dofile loads (by default) in the global environment. You should instead pass in the current environment:
dofile("require_using_file.lua", _ENV)


#4 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 20 May 2018 - 11:59 AM

View PostSquidDev, on 20 May 2018 - 11:54 AM, said:

This is because require exists in the shell environment, but dofile loads (by default) in the global environment. You should instead pass in the current environment:
dofile("require_using_file.lua", _ENV)
I looked dofile() up, and I didn't saw you could specify the environment. Or did I look at the wrong lua version?

#5 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 20 May 2018 - 12:03 PM

View PostJummit, on 20 May 2018 - 11:59 AM, said:

I looked dofile() up, and I didn't saw you could specify the environment. Or did I look at the wrong lua version?
Ahhh, it appears it isn't. Sorry, I always forget which functions allow it and which ones don't. You'll have to use loadfile with an environment argument and then invoke the resulting function.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users