【问题标题】:Storing a single function in a module在模块中存储单个函数
【发布时间】:2015-04-07 14:00:24
【问题描述】:

我正在为一个对象创建一个原型,并且我希望构造函数采用一个参数,该参数允许该对象从外部模块获取函数。这是我的代码。我将在下面进一步解释我的问题。

对象原型:

local obj = {}
local obj_mt = { __index = obj }

function obj.new( moduleString )
    local newObj = {}
    newObj:f = require( moduleString )

    return setmetatable( newObj, obj_mt )
end

return obj

功能模块:

local function foo()
    -- code
end

return foo

当我运行它时,我收到一个错误,告诉我在 newObj:function = 之后应该有函数参数。我不是通过 require( moduleString ) 返回一个函数吗?我需要提到的另一件事是,如果我使用:

newObj.f = require( moduleString )

代替:

newObj:f = require( moduleString )

将函数存储在表newObj中没有问题,但是当我运行它时,该函数不能使用self参数来引用newObj(或构造原型时使用的任何变量名)。所以基本上我需要的是一个存储在外部模块中的函数,它可以访问它在使用 self 关键字加载时放置的父表。

编辑:这是代表 foo() 的实际函数:

local function AIfollow( path, L, R, T, B, X, Y )
    local L = L
    local R = R
    local T = T
    local B = B
    local x = self.img.x   <---- Here is the error
    local y = self.img.y
    local xLoc = ( X - X%tileSize )/tileSize
    local yLoc = ( Y - Y%tileSize )/tileSize

    if( xLoc < R and xLoc > L and yLoc < T and yLoc > B ) then
        local vx = self.img.x - x
        local vy = self.img.y - y
        local d = math.sqrt( vx*vx + vy*vy )
        self.img:setLinearVelocity( vx/d*self.speed, vy/d*self.speed )
    else
        self.img:setLinearVelocity( path[x][y].vX*self.speed, path[x][y].vY*self.speed )
    end
end

这个东西的细节并不重要;我想指出的是,在标记的行我收到一个错误,告诉我它正在尝试索引一个不存在的全局变量 self。我不知道该怎么做是让函数 AIfollow 中的 self 引用它分配给的表。

【问题讨论】:

  • 一方面,你知道'function'是Lua中的保留关键字,不是吗?
  • 哈哈,是的。我想这是一个愚蠢的变量名,不是吗?

标签: module lua require self


【解决方案1】:

我认为这就是你想要做的:

main.lua

m = require 'module'

m:new('foo')
m:f()

foo.lua

local function foo(self)
  print('Inside foo')
end

return foo

module.lua

local m = {}
local m_mt = { __index = m }

function m:new( moduleString )
    self.newObj = {}
    self.f = require( moduleString )

    return setmetatable( self.newObj, m_mt )
end

return m

【讨论】:

  • 这几乎是我已经尝试过的。它的问题是我使用的实际函数 foo 包含对 self 的引用,我想引用它分配给的父表。当我在 Corona 模拟器中运行它并从 main.lua 调用 m.f 时,我收到一条错误消息,提示 self 为 nil。为了解决这个问题,我必须使用符号“m:f”而不是“mf”,这给了我一个我在最初描述的错误邮政。有什么建议吗?
  • 查看我的新答案。我认为这一次它更接近你想要的。
【解决方案2】:

这是一种不同的方法,可能更接近您想要的。

main.lua

m = require 'module'

a = m:new('foo','A',{'a','b','c'})
a:f()

b = m:new('foo','B',{'aa','bb','cc'})
b:f()

a:f()     --once again call A to verify two different objects exist

foo.lua

local function foo(self)
  print('Inside foo '..self.title)
  for k,v in pairs(self.dat) do
    print(k,v)
  end
end

return foo

module.lua

local m = {}

function m:new(moduleString, title, table)
    self = {}
    self.title = title or ''            --none by default
    self.dat = table or {}              --optional table data
    self.f = require( moduleString )
    return self                         --return object reference
end

return m

【讨论】:

  • 非常感谢,这正是我所需要的!
猜你喜欢
  • 2017-03-06
  • 2023-01-28
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多