I am trying to do arithmetic on table values

184 Views Asked by At

I was trying to do name changing that depends on sex choosing (by default it's male). But I can't do it. I have been trying to do it all day but it still doesn't work correctly. Actually it works, the sex is correctly changing from male to female but when I'm trying to change it back(from female to male), I get an error:

[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

Here's the code:

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
1

There are 1 best solutions below

14
On

Could you please let us know which is the line 1055? Because the only thing I'm able to see is that there are four members of the 'infos' table being edited and then replaced all by the default values of one gender's info.

infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)

infos = defMaleInfos

-------------------------------------------

infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)

infos = defFemaleInfos

Should be:

infos = defMaleInfos

infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)

-------------------------------------------

infos = defFemaleInfos

infos.name = table.Random(listWName)
infos.surname = table.Random(listWSurname)

Edit: There's also something else you should take a look at ( lines 989 - 1022 ), because in those lines you make use of a C way to comment code. That's why you should replace those /* */ by --[[ and --]] respectively:

--[[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
--]]