English 中文(简体)
why do i get "attempt to call global require (a nil value)"?
原标题:
  • 时间:2009-11-12 22:58:08
  •  标签:
  • lua

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:

..ContentModulesCoreCameraFreeCamera.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.





相关问题
Wrapping a Lua object for use in C++ with SWIG

Currently I know how to have C++ objects instantiated and passed around in Lua using SWIG bindings, what I need is the reverse. I am using Lua & C++ & SWIG. I have interfaces in C++ and ...

Can t lua_resume after async_wait?

I have some lua script that have some long running task like getting a web page so I make it yield then the C code handle get page job async, so the thread free to do other job and after a specify ...

How to remove a lua table entry by its key?

I have a lua table that I use as a hashmap, ie with string keys : local map = { foo = 1, bar = 2 } I would like to "pop" an element of this table identified by its key. There is a table.remove() ...

Lua plain string.gsub

I ve hit s small block with string parsing. I have a string like: footage/down/temp/cars_[100]_upper/cars_[100]_upper.exr and I m having difficulty using gsub to delete a portion of the string. ...

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

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 ...

热门标签