1

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
Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42
hazed
  • 61
  • 5
  • Where is line 1055, where the error is occurring? – cyclaminist Nov 05 '18 at 16:21
  • [Pastebin](https://pastebin.com/FhwRYXR4) – hazed Nov 05 '18 at 18:12
  • So that is cl_new_character.lua? Then this is line 1055: `DButton2:SetPos( w/2-80/2 + 100,h/4-80/2 +20 )`. I don't see any arithmetic being performed on `defMaleInfos`. I would expect to see something like `defMaleInfos + 1`. – cyclaminist Nov 05 '18 at 18:44

1 Answers1

0

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
--]]
  • Hello, thank you for your help, but if I use the code that you have provided me, it doesn't work at all. But with this code all works except name changing, it remains the same. Here's the link to the code: [Pastebin](https://pastebin.com/wFD86K2d). Can you help me, please? – hazed Nov 05 '18 at 15:55
  • Could you post also the code of the function 'modelPanel.Actualize()'? Because it means the function probably doesn't catch the 'infos' variable. – G.Amihalachioaie Nov 05 '18 at 17:06
  • Is the function modelPanel.Actualize() declared in the same file as the 'infos' variable? One more question. Should the function 'DmodelPanel:LayoutEntity' contain all the code until the last end? – G.Amihalachioaie Nov 05 '18 at 17:22
  • Yes, it's declared in the same file. Yes, it should contain all this code – hazed Nov 05 '18 at 17:27
  • Okay, does the last end of the last code you've passed close the container of the function 'DmodelPanel:LayoutEntity'? Because at the very start when the definition of the function is done, a 'return end' shows up converting that function into something useless. – G.Amihalachioaie Nov 05 '18 at 17:29
  • Oh, I'm sorry. I've provided you not the full code of the function. [Pastebin](https://pastebin.com/34xb0X50) – hazed Nov 05 '18 at 17:39
  • One more thing, could you specify which line is the *1055* one? – G.Amihalachioaie Nov 05 '18 at 17:44
  • Which one of the 22 is exactly? – G.Amihalachioaie Nov 05 '18 at 18:03
  • What do you mean? – hazed Nov 05 '18 at 18:05
  • In the pastebin link you've sent there are 22 lines of code, and we need to know which one is exactly the '*path*/cl_new_character.lua:1055' line. Could you please indicate it or post it? – G.Amihalachioaie Nov 05 '18 at 18:07
  • 1
    From the code you've passed I can see there's a commented section of code in C++ style thing that could generate problems on a Lua script ( Lines 989 to 1022 ). In Lua in order to comment multiple lines you've can use this method '--[[ ]]--'. – G.Amihalachioaie Nov 05 '18 at 18:30
  • Have you tried to remove the /* and */ from the lines 989 and 1022? – G.Amihalachioaie Nov 05 '18 at 19:35
  • Yes, I have tried but it still doesn't work. The name changes only after clothes changing. – hazed Nov 05 '18 at 20:44
  • Where does exactly the clothes change happen in the code? ( _specify a line please_ ) – G.Amihalachioaie Nov 05 '18 at 21:03