开发者

why do i get "attempt to call global 'require' (a nil value)"?

开发者 https://www.devze.com 2022-12-11 12:23 出处:网络
I have 3 lua files, Init.lua, FreeCamera.lua and Camera.lua , init.lua calls require \"Core.Camera.FreeCamera\"

I have 3 lua files, Init.lua, FreeCamera.lua and Camera.lua ,

init.lua calls require "Core.Camera.FreeCamera"

Free Camera:

module(...)
require "Core.Camera.Camera"

local M = {}
FreeCamera = M

M = Class( Camera )
function M:__constructor(x,y,z)
  self.Active = false
  self.x = x
  self.y = y
  self.z = z
end

and

module(...)

local M = {}
Camera = M

M = Class()

function M:__constructor(x,y,z)
  self.Active = false
  self.x = x
  self.y = y
  self.z = z
end

FreeCamera "开发者_开发技巧inherits" Camera kind of. I am trying to require FreeCamera in my init file and I am getting this:

..\Content\Modules\Core\Camera\FreeCamera.lua:12: attempt to call global 'require' (a nil value). Any idea Why? Am I using require the right way? Clearly it is getting into FreeCamera.lua, which is great, but it gets stuck on the next require.


To 'import' identifiers into a module, you might write code something like the following:

local require = require
local Class = Class
local io, table, string
    = io, table, string
local type, pairs, ipairs, assert
    = type, pairs, ipairs, assert

module(...)

local Camera = require 'Core.Camera.Camera'

and so on.

The module(...) command removes (almost) everything from your global-variable namespace—it creates a new global namespace that belongs to the new module. Aside from a couple of special identifiers like _M and _G, the new namespace is empty. But local identifiers stay visible.

The local declarations make the dependencies of your module clear, and they can also communicate your intent. As a bonus, they make access to the named identifiers more efficient. So this is very good Lua style.


The module function creates a new empty environment and so require is not found when called. Try calling require before calling module.


You can:

lua module(..., package.seeall)

To import the global environment into your module. This is (presumably) the easiest solution, but may not be to your taste.

0

精彩评论

暂无评论...
验证码 换一张
取 消