Skip to content

e2 issue with VGUI core #35

@Doommarine1991

Description

@Doommarine1991

made this works but there some issues

  1. hitting E on the e2 chip works once then stops (no clue why)
  2. sometimes the weapons list doesnt show all guns added

yet i have no clue why.......

@name Tabbed Entity Spawn Menu with Ammo System - Fixed
@inputs 
@outputs 
@persist Frame:dframe MenuOpen SpawnPos:vector CurrentTab:string
@persist [HealthBtn BatteryBtn CrateBtn]:dbutton
@persist [TabItems TabAmmo TabWeapons BackBtn]:dbutton
@persist [ItemsPanel AmmoPanel WeaponsPanel]:dpanel
@persist SpawnedHealthKits:array SpawnedBatteries:array SpawnedAmmoBoxes:array SpawnedWeapons:array
@persist AmmoPickupCounts:table WeaponPickupCounts:table
@persist WeaponButtonsCreated:number InitStage:number
@persist LastMenuToggle:number LastPickup:number JustClosedMenu:number
@trigger none

if (first()) {
    timer("init", 50)
    InitStage = 0
    WeaponButtonsCreated = 0
    LastMenuToggle = 0
    LastPickup = 0
    JustClosedMenu = 0
}

interval(20)

# -----------------------
# Helper function to get weapon info from button ID
function string getWeaponInfo(ID:number) {
    local Weapons = table()
    Weapons[41, string] = "Crowbar|weapon_crowbar|models/weapons/w_crowbar.mdl"
    Weapons[42, string] = "Stunstick|weapon_stunstick|models/weapons/w_stunbaton.mdl"
    Weapons[43, string] = "Pistol|weapon_pistol|models/weapons/w_pistol.mdl"
    Weapons[44, string] = "357 Magnum|weapon_357|models/weapons/w_357.mdl"
    Weapons[45, string] = "SMG|weapon_smg1|models/weapons/w_smg1.mdl"
    Weapons[46, string] = "AR2|weapon_ar2|models/weapons/w_irifle.mdl"
    Weapons[47, string] = "Shotgun|weapon_shotgun|models/weapons/w_shotgun.mdl"
    Weapons[48, string] = "Crossbow|weapon_crossbow|models/weapons/w_crossbow.mdl"
    Weapons[49, string] = "Grenade|weapon_frag|models/weapons/w_grenade.mdl"
    Weapons[50, string] = "RPG|weapon_rpg|models/weapons/w_rocket_launcher.mdl"
    Weapons[51, string] = "Gravity Gun|weapon_physcannon|models/weapons/w_physics.mdl"
    Weapons[52, string] = "Bugbait|weapon_bugbait|models/weapons/w_bugbait.mdl"
    
    return Weapons[ID, string]
}

# -----------------------
# Helper function to get ammo type info
# -----------------------
function string getAmmoModel(ID:number) {
    local Models = table()
    Models[31, string] = "Pistol|models/items/boxsrounds.mdl"
    Models[32, string] = "SMG1|models/items/boxmrounds.mdl"
    Models[33, string] = "AR2|models/items/combine_rifle_cartridge01.mdl"
    Models[34, string] = "357|models/items/357ammo.mdl"
    Models[35, string] = "Buckshot|models/items/boxbuckshot.mdl"
    Models[36, string] = "AlyxGun|models/items/combine_rifle_cartridge01.mdl"
    
    return Models[ID, string]
}

# -----------------------
# Initialization - Stage 1
# -----------------------
if (clk("init") && InitStage == 0) {
    runOnKeys(owner(), 1)

    MenuOpen = 0
    CurrentTab = "items"
    SpawnedHealthKits = array()
    SpawnedBatteries = array()
    SpawnedAmmoBoxes = array()
    SpawnedWeapons = array()
    AmmoPickupCounts = table()
    WeaponPickupCounts = table()

    # Holo indicator
    holoCreate(1)
    SpawnPos = entity():pos() + entity():up() * 50
    holoPos(1, SpawnPos)
    holoModel(1, "models/holograms/hq_sphere.mdl")
    holoScale(1, vec(0.3, 0.3, 0.3))
    holoColor(1, vec4(0, 255, 0, 150))
    holoParent(1, entity())

    # UI Frame
    Frame = dframe(1)
    Frame:setTitle("Spawn Menu")
    Frame:setSize(350, 550)
    Frame:center()
    Frame:showCloseButton(1)
    Frame:setBackgroundBlur(1)
    Frame:setVisible(0)
    Frame:create()

    # Tab buttons panel
    local TabPanel = dpanel(2, Frame)
    TabPanel:dock(_DOCK_TOP)
    TabPanel:setHeight(50)
    TabPanel:create()

    # Items Tab Button
    TabItems = dbutton(20, TabPanel)
    TabItems:dock(_DOCK_LEFT)
    TabItems:setWidth(100)
    TabItems:dockMargin(5, 5, 5, 5)
    TabItems:setText("Items")
    TabItems:create()

    # Ammo Tab Button
    TabAmmo = dbutton(21, TabPanel)
    TabAmmo:dock(_DOCK_LEFT)
    TabAmmo:setWidth(100)
    TabAmmo:dockMargin(0, 5, 5, 5)
    TabAmmo:setText("Ammo")
    TabAmmo:create()

    # Weapons Tab Button
    TabWeapons = dbutton(22, TabPanel)
    TabWeapons:dock(_DOCK_LEFT)
    TabWeapons:setWidth(100)
    TabWeapons:dockMargin(0, 5, 5, 5)
    TabWeapons:setText("Weapons")
    TabWeapons:create()

    InitStage = 1
    timer("init2", 50)
}

# -----------------------
# Initialization - Stage 2 (Items Panel)
# -----------------------
if (clk("init2") && InitStage == 1) {
    # Items Panel
    ItemsPanel = dpanel(3, Frame)
    ItemsPanel:dock(_DOCK_FILL)
    ItemsPanel:setVisible(1)
    ItemsPanel:create()

    # Health Kit Button
    HealthBtn = dbutton(10, ItemsPanel)
    HealthBtn:dock(_DOCK_TOP)
    HealthBtn:dockMargin(5, 5, 5, 0)
    HealthBtn:setHeight(25)
    HealthBtn:setText("Health Kit")
    HealthBtn:create()

    # Battery Button
    BatteryBtn = dbutton(12, ItemsPanel)
    BatteryBtn:dock(_DOCK_TOP)
    BatteryBtn:dockMargin(5, 5, 5, 0)
    BatteryBtn:setHeight(25)
    BatteryBtn:setText("Battery")
    BatteryBtn:create()

    # Wood Crate Button
    CrateBtn = dbutton(15, ItemsPanel)
    CrateBtn:dock(_DOCK_TOP)
    CrateBtn:dockMargin(5, 5, 5, 0)
    CrateBtn:setHeight(25)
    CrateBtn:setText("Wood Crate")
    CrateBtn:create()

    InitStage = 2
    timer("init3", 50)
}

# -----------------------
# Initialization - Stage 3 (Ammo Panel)
# -----------------------
if (clk("init3") && InitStage == 2) {
    # Ammo Panel
    AmmoPanel = dpanel(4, Frame)
    AmmoPanel:dock(_DOCK_FILL)
    AmmoPanel:setVisible(0)
    AmmoPanel:create()

    # Ammo type buttons (ID 30-39 for ammo)
    local AmmoTypes = table()
    AmmoTypes[1, string] = "Pistol|models/items/boxsrounds.mdl"
    AmmoTypes[2, string] = "SMG1|models/items/boxmrounds.mdl"
    AmmoTypes[3, string] = "AR2|models/items/combine_rifle_cartridge01.mdl"
    AmmoTypes[4, string] = "357|models/items/357ammo.mdl"
    AmmoTypes[5, string] = "Buckshot|models/items/boxbuckshot.mdl"
    AmmoTypes[6, string] = "AlyxGun|models/items/combine_rifle_cartridge01.mdl"

    for (I = 1, 6) {
        local Data = AmmoTypes[I, string]:explode("|")
        local AmmoName = Data[1, string]
        local AmmoModel = Data[2, string]
        
        local Btn = dbutton(30 + I, AmmoPanel)
        Btn:dock(_DOCK_TOP)
        Btn:dockMargin(5, 5, 5, 0)
        Btn:setHeight(25)
        Btn:setText(AmmoName + " Ammo")
        Btn:create()
    }

    InitStage = 3
    timer("init4", 50)
}

# -----------------------
# Initialization - Stage 4 (Weapons Panel)
# -----------------------
if (clk("init4") && InitStage == 3) {
    # Weapons Panel
    WeaponsPanel = dpanel(5, Frame)
    WeaponsPanel:dock(_DOCK_FILL)
    WeaponsPanel:setVisible(0)
    WeaponsPanel:create()

    InitStage = 4
    WeaponButtonsCreated = 0
    timer("createWeapons", 5)
}

# -----------------------
# Create Weapon Buttons Gradually
# -----------------------
if (clk("createWeapons") && InitStage == 4) {
    local WeaponTypes = table()
    WeaponTypes[1, string] = "Crowbar|weapon_crowbar|models/weapons/w_crowbar.mdl"
    WeaponTypes[2, string] = "Stunstick|weapon_stunstick|models/weapons/w_stunbaton.mdl"
    WeaponTypes[3, string] = "Pistol|weapon_pistol|models/weapons/w_pistol.mdl"
    WeaponTypes[4, string] = "357 Magnum|weapon_357|models/weapons/w_357.mdl"
    WeaponTypes[5, string] = "SMG|weapon_smg1|models/weapons/w_smg1.mdl"
    WeaponTypes[6, string] = "AR2|weapon_ar2|models/weapons/w_irifle.mdl"
    WeaponTypes[7, string] = "Shotgun|weapon_shotgun|models/weapons/w_shotgun.mdl"
    WeaponTypes[8, string] = "Crossbow|weapon_crossbow|models/weapons/w_crossbow.mdl"
    WeaponTypes[9, string] = "Grenade|weapon_frag|models/weapons/w_grenade.mdl"
    WeaponTypes[10, string] = "RPG|weapon_rpg|models/weapons/w_rocket_launcher.mdl"
    WeaponTypes[11, string] = "Gravity Gun|weapon_physcannon|models/weapons/w_physics.mdl"
    WeaponTypes[12, string] = "Bugbait|weapon_bugbait|models/weapons/w_bugbait.mdl"

    # Create 3 weapon buttons per tick to avoid hitting creation limits
    local StartIdx = WeaponButtonsCreated + 1
    local EndIdx = min(WeaponButtonsCreated + 3, 12)
    
    for (I = StartIdx, EndIdx) {
        local Data = WeaponTypes[I, string]:explode("|")
        local WeaponName = Data[1, string]
        
        local Btn = dbutton(40 + I, WeaponsPanel)
        Btn:dock(_DOCK_TOP)
        Btn:dockMargin(5, 2, 5, 0)
        Btn:setHeight(30)
        Btn:setText(WeaponName)
        Btn:create()
        
        WeaponButtonsCreated = WeaponButtonsCreated + 1
    }
    
    # If we haven't created all 12 weapons yet, schedule another tick
    if (WeaponButtonsCreated < 12) {
        timer("createWeapons", 5)
    } else {
        # All done, finalize
        WeaponsPanel:modify()
        Frame:modify()
        InitStage = 5
        print("Menu initialization complete!")
    }
}

# -----------------------
# vguiClk - Button Click Handler
# -----------------------
event vguiClk(Player:entity, ID:number, Values:table)
{
    if (Player == owner() && InitStage == 5) {
        # Tab switching
        if (ID == TabItems:id()) {
            ItemsPanel:setVisible(1)
            AmmoPanel:setVisible(0)
            WeaponsPanel:setVisible(0)
            ItemsPanel:modify()
            AmmoPanel:modify()
            WeaponsPanel:modify()
            CurrentTab = "items"
        }
        elseif (ID == TabAmmo:id()) {
            ItemsPanel:setVisible(0)
            AmmoPanel:setVisible(1)
            WeaponsPanel:setVisible(0)
            ItemsPanel:modify()
            AmmoPanel:modify()
            WeaponsPanel:modify()
            CurrentTab = "ammo"
        }
        elseif (ID == TabWeapons:id()) {
            ItemsPanel:setVisible(0)
            AmmoPanel:setVisible(0)
            WeaponsPanel:setVisible(1)
            ItemsPanel:modify()
            AmmoPanel:modify()
            WeaponsPanel:modify()
            CurrentTab = "weapons"
        }
        # Items tab buttons
        elseif (ID == HealthBtn:id()) {
            local Kit = sentSpawn("item_healthkit", SpawnPos, ang(), 0)
            if (Kit:isValid()) { SpawnedHealthKits:pushEntity(Kit) }
            Frame:setVisible(0)
            Frame:modify()
            MenuOpen = 0
            JustClosedMenu = 1
            timer("clearMenuFlag", 500)
        }
        elseif (ID == BatteryBtn:id()) {
            local Batt = sentSpawn("item_battery", SpawnPos, ang(), 0)
            if (Batt:isValid()) { SpawnedBatteries:pushEntity(Batt) }
            Frame:setVisible(0)
            Frame:modify()
            MenuOpen = 0
            JustClosedMenu = 1
            timer("clearMenuFlag", 500)
        }
        elseif (ID == CrateBtn:id()) {
            propSpawnEffect(0)
            local Ent = propSpawn("models/props_junk/wood_crate001a.mdl", SpawnPos, ang(), 0)
            if (Ent:isValid()) { Ent:propFreeze(0) }
            Frame:setVisible(0)
            Frame:modify()
            MenuOpen = 0
            JustClosedMenu = 1
            timer("clearMenuFlag", 500)
        }
        # Ammo spawning (ID 31-36)
        elseif (ID >= 31 && ID <= 36) {
            local AmmoInfo = getAmmoModel(ID):explode("|")
            local AmmoType = AmmoInfo[1, string]
            local AmmoModel = AmmoInfo[2, string]
            
            propSpawnEffect(0)
            local AmmoBox = propSpawn(AmmoModel, SpawnPos, ang(), 0)
            if (AmmoBox:isValid()) {
                AmmoBox:setName(AmmoType)
                SpawnedAmmoBoxes:pushEntity(AmmoBox)
            }
            Frame:setVisible(0)
            Frame:modify()
            MenuOpen = 0
            JustClosedMenu = 1
            timer("clearMenuFlag", 500)
        }
        # Weapon spawning (ID 41-52)
        elseif (ID >= 41 && ID <= 52) {
            local WeaponInfo = getWeaponInfo(ID):explode("|")
            if (WeaponInfo:count() >= 3) {
                local WeaponName = WeaponInfo[1, string]
                local WeaponClass = WeaponInfo[2, string]
                local WeaponModel = WeaponInfo[3, string]
                
                # Check if player already has this weapon
                if (WeaponPickupCounts:exists(WeaponClass) && WeaponPickupCounts[WeaponClass, number] >= 1) {
                    Player:emitSound("@buttons/button10.wav", 80, 100, 1.0)
                    printColor(Player, vec(255,100,100), "[DENIED] You already have this weapon!")
                } else {
                    propSpawnEffect(0)
                    local WeaponProp = propSpawn(WeaponModel, SpawnPos, ang(), 0)
                    if (WeaponProp:isValid()) {
                        WeaponProp:setName(WeaponClass)
                        SpawnedWeapons:pushEntity(WeaponProp)
                    }
                    Frame:setVisible(0)
                    Frame:modify()
                    MenuOpen = 0
                    JustClosedMenu = 1
                    timer("clearMenuFlag", 500)
                }
            }
        }
    }
}

# Clear the menu closed flag
if (clk("clearMenuFlag")) {
    JustClosedMenu = 0
}

# -----------------------
# Menu Toggle with E key when looking at chip
# -----------------------
if (keyClk(owner()) && owner():keyUse() && InitStage == 5) {
    local CurrentTime = curtime()
    local Trace = owner():eyeTrace()
    
    # Check if looking at the chip
    if (Trace:entity() == entity() && Trace:distance() < 150) {
        # Only toggle menu if enough time has passed since last toggle
        if (CurrentTime - LastMenuToggle >= 200) {
            if (MenuOpen) {
                Frame:setVisible(0)
                Frame:modify()
                MenuOpen = 0
                JustClosedMenu = 1
                timer("clearMenuFlag", 500)
            } else {
                Frame:setVisible(1)
                Frame:makePopup()
                Frame:modify()
                MenuOpen = 1
            }
            LastMenuToggle = CurrentTime
        }
    }
}

# -----------------------
# PICKUP SYSTEM (E key) - Only when NOT looking at chip
# -----------------------
if (keyClk(owner()) && owner():keyUse() && InitStage == 5 && !MenuOpen && !JustClosedMenu) {
    local Player = owner()
    local CurrentTime = curtime()
    local Trace = owner():eyeTrace()
    local LookedAtEntity = Trace:entity()
    local Distance = Trace:distance()

    # Don't pickup if looking at the chip or if recently picked something up
    if (LookedAtEntity != entity() && LookedAtEntity:isValid() && Distance < 150 && (CurrentTime - LastPickup >= 200)) {

        # --- HEALTH KIT PICKUP ---
        for (I = SpawnedHealthKits:count(), 1, -1) {
            local Kit = SpawnedHealthKits[I, entity]
            if (Kit:isValid() && LookedAtEntity == Kit) {
                local Denied = 0
                local MaxHealth = 500
                local Cap = 800
                local Add = 100
                local Cur = Player:health()

                if (Cur < MaxHealth) {
                    Player:setHealth(MaxHealth)
                    Player:emitSound("@items/smallmedkit1.wav", 80, 100, 1.0)
                    printColor(Player, vec(100,255,100), "Health restored to " + MaxHealth)
                } elseif (Cur >= Cap) {
                    Denied = 1
                    Player:emitSound("@buttons/button10.wav", 80, 100, 1.0)
                    printColor(Player, vec(255,100,100), "[HEALTH MAXED] You are at maximum health cap (" + Cap + ")!")
                } else {
                    local New = Cur + Add
                    if (New > Cap) {
                        Player:setHealth(Cap)
                        Player:emitSound("@items/smallmedkit1.wav", 80, 100, 1.0)
                        printColor(Player, vec(255,255,100), "Health maxed at cap (" + Cap + ")!")
                    } else {
                        Player:setHealth(New)
                        Player:emitSound("@items/smallmedkit1.wav", 80, 100, 1.0)
                        printColor(Player, vec(100,255,100), "Health: " + New + "/" + Cap)
                    }
                }

                Kit:propDelete()
                SpawnedHealthKits:removeEntity(I)
                LastPickup = CurrentTime
                break
            }
        }

        # --- BATTERY PICKUP ---
        for (I = SpawnedBatteries:count(), 1, -1) {
            local Batt = SpawnedBatteries[I, entity]
            if (Batt:isValid() && LookedAtEntity == Batt) {
                local Denied = 0
                local Max = 500
                local Cap = 800
                local Add = 100
                local Cur = Player:armor()

                if (Cur < Max) {
                    Player:plySetArmor(Max)
                    Player:emitSound("@items/battery_pickup.wav", 80, 100, 1.0)
                    printColor(Player, vec(100,255,100), "Armor restored to " + Max)
                } elseif (Cur >= Cap) {
                    Denied = 1
                    Player:emitSound("@buttons/button10.wav", 80, 100, 1.0)
                    printColor(Player, vec(255,100,100), "[ARMOR MAXED] You are at maximum armor cap (" + Cap + ")!")
                } else {
                    local New = Cur + Add
                    if (New > Cap) {
                        Player:plySetArmor(Cap)
                        Player:emitSound("@items/battery_pickup.wav", 80, 100, 1.0)
                        printColor(Player, vec(255,255,100), "Armor maxed at cap (" + Cap + ")!")
                    } else {
                        Player:plySetArmor(New)
                        Player:emitSound("@items/battery_pickup.wav", 80, 100, 1.0)
                        printColor(Player, vec(100,255,100), "Armor: " + New + "/" + Cap)
                    }
                }

                Batt:propDelete()
                SpawnedBatteries:removeEntity(I)
                LastPickup = CurrentTime
                break
            }
        }

        # --- AMMO BOX PICKUP ---
        for (I = SpawnedAmmoBoxes:count(), 1, -1) {
            local Box = SpawnedAmmoBoxes[I, entity]
            if (Box:isValid() && LookedAtEntity == Box) {
                local AmmoType = Box:getName()
                
                if (!AmmoPickupCounts:exists(AmmoType)) {
                    AmmoPickupCounts[AmmoType, number] = 0
                }
                
                local TotalAmmoGiven = AmmoPickupCounts[AmmoType, number]
                local AmmoCap = 2000
                
                if (TotalAmmoGiven >= AmmoCap) {
                    Player:emitSound("@buttons/button10.wav", 80, 100, 1.0)
                    printColor(Player, vec(255,100,100), "[AMMO MAXED] You are at maximum " + AmmoType + " ammo cap (" + AmmoCap + ")!")
                } else {
                    local AmmoToGive = 500
                    if (TotalAmmoGiven > 0) {
                        AmmoToGive = 200
                    }
                    
                    AmmoToGive = min(AmmoToGive, AmmoCap - TotalAmmoGiven)
                    
                    Player:giveAmmo(AmmoToGive, AmmoType)
                    AmmoPickupCounts[AmmoType, number] = TotalAmmoGiven + AmmoToGive
                    
                    local NewTotal = AmmoPickupCounts[AmmoType, number]
                    Player:emitSound("@items/ammo_pickup.wav", 80, 100, 1.0)
                    
                    if (NewTotal >= AmmoCap) {
                        printColor(Player, vec(255,255,100), "Picked up " + AmmoToGive + " " + AmmoType + " ammo. [MAXED at " + AmmoCap + "]")
                    } else {
                        printColor(Player, vec(100,255,100), "Picked up " + AmmoToGive + " " + AmmoType + " ammo. (Total: " + NewTotal + "/" + AmmoCap + ")")
                    }
                }
                
                Box:propDelete()
                SpawnedAmmoBoxes:removeEntity(I)
                LastPickup = CurrentTime
                break
            }
        }

        # --- WEAPON PICKUP ---
        for (I = SpawnedWeapons:count(), 1, -1) {
            local Weapon = SpawnedWeapons[I, entity]
            if (Weapon:isValid() && LookedAtEntity == Weapon) {
                local WeaponClass = Weapon:getName()
                
                # Check if we've already picked up this weapon type
                if (!WeaponPickupCounts:exists(WeaponClass)) {
                    WeaponPickupCounts[WeaponClass, number] = 0
                }
                
                local WeaponCap = 1  # Can only have 1 of each weapon
                local AlreadyHas = WeaponPickupCounts[WeaponClass, number]
                
                # Always delete the weapon prop first
                Weapon:propDelete()
                SpawnedWeapons:removeEntity(I)
                
                if (AlreadyHas >= WeaponCap) {
                    Player:emitSound("@buttons/button10.wav", 80, 100, 1.0)
                    printColor(Player, vec(255,100,100), "[DENIED] You already have this weapon!")
                } else {
                    Player:giveWeapon(WeaponClass)
                    WeaponPickupCounts[WeaponClass, number] = 1
                    Player:emitSound("@items/ammo_pickup.wav", 80, 100, 1.0)
                    printColor(Player, vec(100,255,100), "Picked up " + WeaponClass + "!")
                }
                
                LastPickup = CurrentTime
                break
            }
        }
    }
}

# -----------------------
# CLEANUP INVALID ENTRIES
# -----------------------
for (A = SpawnedAmmoBoxes:count(), 1, -1) {
    if (!SpawnedAmmoBoxes[A, entity]:isValid()) { SpawnedAmmoBoxes:removeEntity(A) }
}
for (H = SpawnedHealthKits:count(), 1, -1) {
    if (!SpawnedHealthKits[H, entity]:isValid()) { SpawnedHealthKits:removeEntity(H) }
}
for (B = SpawnedBatteries:count(), 1, -1) {
    if (!SpawnedBatteries[B, entity]:isValid()) { SpawnedBatteries:removeEntity(B) }
}
for (W = SpawnedWeapons:count(), 1, -1) {
    if (!SpawnedWeapons[W, entity]:isValid()) { SpawnedWeapons:removeEntity(W) }
}
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions