【问题标题】:Access or change a local variable, which inside a function, outside of it?访问或更改局部变量,在函数内部,在函数外部?
【发布时间】:2017-03-08 18:21:07
【问题描述】:

我在尝试为特定游戏制作模组时遇到了一点问题。你可能听说过,它叫多人版饥荒。

在游戏数据中,有一个名为tuning.lua 的文件,它处理许多初始变量。它里面只是一个函数,下面是一些局部变量和一个名为TUNING的表/数组/列表,其中包含一堆全局变量。函数下面的局部变量我无法访问,因为它们是本地变量,但我也无法更改任何内容,因为它们是局部变量。

所以,相反,我在一个完全不同的文件中工作,该文件在我的 mod 中,名为 modmain.lua。所以,我的问题是,如何从tuning.lua 脚本中的函数中获取一个局部变量并在modmain.lua 中更改/使用它?这是tuning.lua代码的sn-p:

TUNING = {} -- the table is created


function Tune(overrides)
    if overrides == nil then
        overrides = {}
    end
    --the following are the local variables used in the game
    local seg_time = 30
    local total_day_time = seg_time*16

    local day_segs = 10
    local dusk_segs = 4
    local night_segs = 2

    --default day composition. changes in winter, etc
    local day_time = seg_time * day_segs
    local dusk_time = seg_time * dusk_segs
    local night_time = seg_time * night_segs

    local multiplayer_attack_modifier = 1
    local multiplayer_goldentool_modifier = 1
    local multiplayer_armor_durability_modifier = 0.7
    local multiplayer_armor_absorption_modifier = 1
    local multiplayer_wildlife_respawn_modifier = 1

    local wilson_attack = 34 * multiplayer_attack_modifier
    local wilson_health = 150
    local calories_per_day = 75

    local wilson_attack_period = .1
    -----------------------

    local perish_warp = 1--/200

    TUNING =
    {
    --global variables go here
    }
end

所以,假设我想在 modmain.lua 中将 total_day_time 更改为 24。我需要在那里写什么代码才能做到这一点?或者这是不可能的?基本上,我正在尝试增加游戏中的日段数量(以及延长段时间),这似乎是唯一可以访问它的地方。有一个模组可以增加分段时间,但没有模组可以增加分段数量。 Here's segments that I'm talking about, in case you have no idea.

【问题讨论】:

    标签: lua local


    【解决方案1】:

    您可以使用_G 表变量来存储和检索您真正需要的存储和检索“全局”变量。 或者,更好的是,创建您自己的自定义模块。

    以下代码未经测试:

    tuning.lua

    M.TUNING_VAR = {}
    
    --just an example
    local helloWorld = function()
       print("Hello World!")
    end
    M.template_path = '/BASEMODULE_PATH/file.tmpl'
    function M:TUNING()
      return self.TUNING_VAR
    end
    function M:SET_TURNING(tbl)
     if tbl == nil then
            tbl = {}
     end
     self.TURNING = tbl
    end
    function M:Tune(overrides)
        if overrides == nil then
            overrides = {}
        end
        --the following are the local variables used in the game
        local seg_time = 30
        local total_day_time = seg_time*16
    
        local day_segs = 10
        local dusk_segs = 4
        local night_segs = 2
    
        --default day composition. changes in winter, etc
        local day_time = seg_time * day_segs
        local dusk_time = seg_time * dusk_segs
        local night_time = seg_time * night_segs
    
        local multiplayer_attack_modifier = 1
        local multiplayer_goldentool_modifier = 1
        local multiplayer_armor_durability_modifier = 0.7
        local multiplayer_armor_absorption_modifier = 1
        local multiplayer_wildlife_respawn_modifier = 1
    
        local wilson_attack = 34 * multiplayer_attack_modifier
        local wilson_health = 150
        local calories_per_day = 75
    
        local wilson_attack_period = .1
        -----------------------
    
        local perish_warp = 1--/200
        --your code
    
        self:SET_TURNING(overrides) -- or whatever your want
    end
    
    M.helloWorld = helloWorld
    return M
    

    用法

    the_other_lua_file.lua

    local module = require "turning"
    local module:TURNING()["yourwantedvariable"] = newvalue
    

    【讨论】:

    • 这是,我无法编辑tuning.lua。如,它是代码,它是原始游戏的一部分,我无法将任何内容编辑到或取出。我所能做的就是创建新文件,这些文件要么覆盖它们,要么覆盖它们的特定部分,无论是函数还是变量。如果我在其中编辑任何内容,该模组将不适用于不编辑特定原始游戏文件的任何人,并且当游戏更新时,所有内容都会被覆盖,这将非常不方便。
    猜你喜欢
    • 2011-04-15
    • 1970-01-01
    • 2017-06-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多