【问题标题】:Need Lua Help. attempt to index field 'Config' (a nil value) Gmod13需要 Lua 帮助。尝试索引字段“配置”(零值)Gmod13
【发布时间】:2013-07-01 08:06:53
【问题描述】:

运行游戏模式时出现此错误。

[错误]
gamemodes/rp/gamemode/cl_init.lua:910: 尝试索引字段 'Config'(一个 nil 值)
1. 未知 - gamemodes/rp/gamemode/cl_init.lua:910

config 是一个表,具有诸如config["Run Speed"] 之类的索引,并且整个表在sh_config.lua 文件中全局设置为等于GM.Config。为什么配置没有被注册为一个值?我必须将配置文件包含到 cl_init 文件中吗?如果是这样,如何?使用包含()?

function GM:Think()
if ( self.Config["Local Voice"] ) then             **--Referred line(910)**  
    for k, v in pairs( player.GetAll() ) do
        if ( hook.Call("PlayerCanVoice",GAMEMODE, v) ) then
            if ( v:IsMuted() ) then v:SetMuted(); end
        else
            if ( !v:IsMuted() ) then v:SetMuted(); end
        end
    end
end

-- Call the base class function.
return self.BaseClass:Think();
end

编辑 -- sh_config.lua 中的配置表。

local config = {};

   -- Command 
config["Command Prefix"]            = "/"; -- The prefix that is used for chat commands.
config["Maximum Notes"]             = 2; -- Maximum notes per player 
config["Advert Cost"]               = 60; -- The money that it costs to advertise.

config["Advert Timeout"]            = 150 -- How many seconds between adverts
config["OOC Timeout"]               = 60 -- How many seconds between OOC messages
config["Item Timer"]                = 7 -- How many seconds between item uses
config["Item Timer (S)"]            = 20 -- How many seconds between specific item uses
config["Note Fade Speed"]           = 12 -- How many minutes before nots disappear                                 

-- Voice
config["Local Voice"]               = true; -- Players can only hear a player's voice if they are near them.   This is the index being called which is creating an error.
config["Talk Radius"]               = 256; -- The radius of each player that 
--other players have to be in to hear them talk (units).

-- Player Stuff

 config["Walk Speed"]               = 150; -- The speed that players walk at.
 config["Run Speed"]                    = 275; -- The speed that players run at.


GM.Config = config;

我在 sh_init.lua 中有 includecs("sh_config.lua");include("sh_config.lua")AddCSLuaFile("sh_config.lua") 在 init.lua 中。和 cl_init.lua 中的 include("sh_config.lua");

我仍然收到这个愚蠢的错误。有人可以解释包含和添加文件之间的区别。如何使 sh_config 的变量在其他文件中成为全局变量?或者换句话说,我如何使所需的文件(cl_init)通过 sh_config.lua 中的代码读取,我可以在客户端 init 中使用它的代码?

【问题讨论】:

  • 显示负责设置GM.Config的表的代码以及调用此代码的位置。
  • 添加了代码。在哪里调用它是什么意思?
  • @NickZook 他的意思是你打电话给GM:Think()的sn-p
  • 另一种选择可能是 facepunch.com 的 Lua 脚本部分 - 看到这是一个官方论坛,那里有很多伟大的 GMod Lua 头脑。
  • @NickZook - GM:Think() 由游戏本身调用。它被重复调用 - 与 GM:Tick() 相同 - 通常以 66 或 100 次/秒开始。想想还不止这些。

标签: lua


【解决方案1】:

您需要在 cl_init.lua 的顶部包含 sh_config.lua

include("path/to/file.lua")

请务必在init.lua 文件中也使用AddCSLuaFile("path/to/lua.lua")。您还需要在init.lua 中执行include("path/to/file.lua")。 (不过,这只是共享文件所必需的)

另外我很确定你的配置表的范围将被限制在 sh_config.lua 所以你应该从你的变量声明中删除local

【讨论】:

  • 这可能是一个更好的方法,我实际上忘记了。自从我在 Lua 中为 GMod 开发以来已经有一段时间了。我想你可以这样做 If SERVER then AddCSLuaFile("") end
  • And If CLIENT then include("") end
  • 是的。在许多游戏模式中,“共享”包含在 init.lua 中,并在 cl_init 中包含和 AddCSLuaFile。然后文件被包含在 if CLIENT 块中的 shared.lua 和 AddCSLuaFile() 中。
【解决方案2】:

*sh_config* 是一个共享文件 - 这意味着它必须包含在客户端和服务器端。

In init.lua - 在其他包含的 top 中。把include("sh_config.lua")
同样在 init.lua 中,输入 AddCSLuaFile("sh_config.lua") - 这将确保文件被客户端下载,并在客户端执行。

cl_init.lua - 输入include("sh_config.lua")。还围绕其他包括。这应该可以按预期工作。

看到这是一个配置文件,我认为应该首先包含它,或者几乎首先包含它。它可能包含重要的设置,用于脚本加载的其余部分。

另外 - 通常包含一个 shared.lua ,等于一个 shared_init 文件。这可能是你的情况。如果是,您应该在 shared.lua 中添加一个带有包含的include("sh_config.lua"),并在if CLIENT then-block 中添加AddCSLuaFile("sh_config.lua")

【讨论】:

  • 文件包含在 cl_init.lua 的什么位置?高于或低于 GM:Think() ?
  • 我把它放在最上面。就在 include("sh_init.lua"); 下就像第 8 行一样。
  • 尝试在配置文件顶部添加print("debug")。看看它在加入服务器时是否打印客户端。
猜你喜欢
  • 2011-11-05
  • 1970-01-01
  • 2014-04-04
  • 2021-05-15
  • 2016-09-19
  • 1970-01-01
  • 2012-10-23
  • 2021-05-18
  • 2012-03-12
相关资源
最近更新 更多