我正在尝试对表值进行算术运算
Posted
技术标签:
【中文标题】我正在尝试对表值进行算术运算【英文标题】:I am trying to do arithmetic on table values 【发布时间】:2018-11-05 12:16:45 【问题描述】:我试图根据性别选择进行名称更改(默认情况下是男性)。但我做不到。我整天都在尝试这样做,但它仍然无法正常工作。 实际上它有效,性别正确地从男性变为女性,但是当我试图将其改回(从女性到男性)时,我收到一个错误:
[ERROR] *path*/cl_new_character.lua:1055: attempt to perform arithmetic on upvalue 'defMaleInfos' (a table value)
1. DoClick - *path*/cl_new_character.lua:1055
2. unknown - lua/vgui/dlabel.lua:232
代码如下:
local defMaleInfos =
model = "models/kerry/player/citizen/male_02.mdl",
name = randommname,
surname = randommsurname,
sex = 1,
playerColor = Vector(1,1,1),
bodygroups =
top = "polo",
pant = "pant",
,
skin = 0,
eyestexture =
basetexture =
["r"] = "eyes/eyes/amber_r",
["l"] = "eyes/eyes/amber_l",
,
,
hasCostume = false,
teetexture =
basetexture = "models/citizen/body/citizen_sheet",
hasCustomThings = false,
,
panttexture =
basetexture = "models/citizen/body/citizen_sheet",
,
local infos = defMaleInfos
local defFemaleInfos =
model = "models/kerry/player/citizen/female_01.mdl",
name = randomwname,
surname = randomwsurname,
sex = 0,
playerColor = Vector(1,1,1),
bodygroups =
top = "polo",
pant = "pant",
,
skin = 0,
eyestexture =
basetexture =
["r"] = "eyes/eyes/amber_r",
["l"] = "eyes/eyes/amber_l",
,
,
hasCostume = false,
teetexture =
basetexture = "models/humans/modern/female/sheet_01",
hasCustomThings = false,
,
panttexture =
basetexture = "models/humans/modern/female/sheet_01",
,
local infosw = defFemaleInfos
*code of panel*
local DButton1 = vgui.Create( "DButton", DPanel2)
DButton1:SetSize( 80,80 )
DButton1:SetPos( w/2-80/2 -100,h/4-80/2+20 )
DButton1:SetText( "" )
DButton1.Paint = function(pnl, w, h )
local m = 1
if infos.sex != 1 then
m = 0
end
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( male )
surface.DrawTexturedRect( 2 + (w-4)/2 - 64/2, 2 + (h-4)/2 - 64/2, 64,64 )
end
DButton1.DoClick = function( pnl )
infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)
infos = defMaleInfos
modelPanel.Actualize()
end
local DButton2 = vgui.Create( "DButton", DPanel2)
DButton2:SetSize( 80,80 )
DButton2:SetPos( w/2-80/2 + 100,h/4-80/2 +20 )
DButton2:SetText( "" )
DButton2.Paint = function(pnl, w, h )
local m = 1
if infos.sex != 0 then
m = 0
end
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( female )
surface.DrawTexturedRect( 2 + (w-4)/2 - 64/2, 2 + (h-4)/2 - 64/2, 64,64 )
end
DButton2.DoClick = function( pnl )
infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)
infos = defFemaleInfos
modelPanel.Actualize()
end
【问题讨论】:
第1055行在哪里,哪里出错了? Pastebin 那是 cl_new_character.lua 吗?然后这是第 1055 行:DButton2:SetPos( w/2-80/2 + 100,h/4-80/2 +20 )
。我没有看到在 defMaleInfos
上执行任何算术运算。我希望看到类似defMaleInfos + 1
。
【参考方案1】:
您能告诉我们1055是哪一行吗?因为我唯一能看到的是“infos”表中有四个成员正在编辑,然后全部替换为一个性别信息的默认值。
infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)
infos = defMaleInfos
-------------------------------------------
infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)
infos = defFemaleInfos
应该是:
infos = defMaleInfos
infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)
-------------------------------------------
infos = defFemaleInfos
infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)
编辑:您还应该查看其他内容(989 - 1022 行),因为在这些行中您会用到注释代码的 C 方式。这就是为什么您应该将那些 /* */ 分别替换为 --[[ 和 --]] :
--[[local TextEntry = vgui.Create( "DTextEntry", DPanel ) -- create the form as a child of frame
TextEntry:SetPos( w/2-100, 30 + 40 )
TextEntry:SetSize( 200, 30 )
TextEntry:SetText( infos.name )
TextEntry.OnTextChanged = function( self )
txt = self:GetValue()
local amt = string.len(txt)
if amt > 15 then
self.OldText = self.OldText or infos.name
self:SetText(self.OldText)
self:SetValue(self.OldText)
else
self.OldText = txt
end
infos.name = TextEntry:GetValue()
end
local TextEntry2 = vgui.Create( "DTextEntry", DPanel ) -- create the form as a child of frame
TextEntry2:SetPos( w/2-100, 30 + 40 * 2 )
TextEntry2:SetSize( 200, 30 )
TextEntry2:SetText( infos.surname )
TextEntry2.OnTextChanged = function( self )
txt = self:GetValue()
local amt = string.len(txt)
if amt > 15 then
self.OldText = self.OldText or infos.surname
self:SetText(self.OldText)
self:SetValue(self.OldText)
else
self.OldText = txt
end
infos.surname = TextEntry2:GetValue()
end
--]]
【讨论】:
您好,谢谢您的帮助,但是如果我使用您提供给我的代码,它根本不起作用。但是使用此代码除了更改名称外,其他所有代码都可以使用,它保持不变。这是代码的链接:Pastebin。你能帮帮我吗? 你能把函数'modelPanel.Actualize()'的代码也贴出来吗?因为这意味着该函数可能无法捕获“infos”变量。 函数 modelPanel.Actualize() 是否与“infos”变量在同一个文件中声明?还有一个问题。函数“DmodelPanel:LayoutEntity”是否应该包含所有代码,直到最后结束? 是的,它在同一个文件中声明。是的,它应该包含所有这些代码 从您传递的代码中,我可以看到有一段 C++ 风格的注释代码可能会在 Lua 脚本上产生问题(第 989 到 1022 行)。在 Lua 中,为了注释多行,您可以使用此方法 '--[[ ]]--'。以上是关于我正在尝试对表值进行算术运算的主要内容,如果未能解决你的问题,请参考以下文章