luagarrys-mod

How to set a function's argument, as a hook.Add identifier (gmod lua)


this is my code:

function CyberpunkUIShape( leftx, downy, fillcolor, linecolor, wid, hei, bendsize, offset, identifier )
        local trapezoidbg = {
            { x = leftx+bendsize, y = downy-hei }, -- top left
            { x = leftx , y = downy }, -- down left
            { x = leftx+wid, y = downy }, -- down right
            { x = leftx+wid+bendsize, y = downy-hei } -- top right
        }
        -- print("hehe")
        
        local function HUDPaint()

            surface.SetDrawColor(fillcolor)
            surface.DrawPoly( trapezoidbg )
         
            
            surface.SetDrawColor(linecolor)
            surface.DrawLine(leftx+bendsize+offset, downy-hei+offset, leftx+wid+bendsize-offset-2, downy-hei+offset) -- top
            surface.DrawLine(leftx+offset+1, downy-offset-1, leftx+wid-offset, downy-offset-1) -- down
            surface.DrawLine(leftx+bendsize+offset, downy-hei+offset, leftx+offset+1, downy-offset) -- left
            surface.DrawLine(leftx+wid+bendsize-offset-2, downy-hei+offset, leftx+wid-offset-1, downy-offset)
        end
        hook.Add("HUDPaint", "identifier" , HUDPaint)
    end
    CyberpunkUIShape(25, h-75, BGColor, AccentColor, 229, 30, 8, 2, armor) 
    -- CyberpunkUIShape(665, h-75, BGColor, AccentColor, 229, 30, 8, 2, health)

i can't call two "CyberpunkUIShape" Functions at same time, this is current code output: image

but when i uncomment the second function call, this is output: image

there should be 2 shapes at same time, but only second shape works and first one disappears. so, i realized if a hook.Add repeats with same identifier, the last call will work and others will get ignored, so i added an argument to seperate their identifiers. one for health, with "health" identifier, and one for armor, with "armor" identifier.

so, i tried to edit the hook.Add to this: hook.Add("HUDPaint", identifier , HUDPaint)

and these errors:

[cyberpunk_hud] bad argument #2 to 'Add' (string expected, got nil)
1. Add - lua/includes/modules/hook.lua:34
    2. CyberpunkUIShape - addons/cyberpunk_hud/lua/autorun/cyberpunk_hud.lua:502
        3. v - addons/cyberpunk_hud/lua/autorun/cyberpunk_hud.lua:504
            4. unknown - lua/includes/modules/hook.lua:96

[cyberpunk_hud] bad argument #2 to 'Add' (string expected, got nil)
1. Add - lua/includes/modules/hook.lua:34
    2. CyberpunkUIShape - addons/cyberpunk_hud/lua/autorun/cyberpunk_hud.lua:502
        3. v - addons/cyberpunk_hud/lua/autorun/cyberpunk_hud.lua:505
            4. unknown - lua/includes/modules/hook.lua:96

i just want that hook.Add use a function argument. please help. thx


Solution

  • When you write "identifier" it means the string saying the word 'identifier'. When you write identifier it means the variable called 'identifier'.

    I wrote CyberpunkUIShape(25, h-75, BGColor, AccentColor, 229, 30, 8, 2, "armor") and fixed. thx (added " s around the armor)