• Clip Size?
    8 replies, posted
What's the best way of making the clip size for HUDs? You can put each weapons clip size in a table, but that means everything has to be defined for it to work You can make a variable which changes between each weapon that is pushed up when the current ammo > clip size, but then it fails with weapons that push their ammo up above the clip amount Or am I just completely missing a function? :downs:
If the weapon uses the CS base you can do: Weapon:GetTable().Primary.ClipSize Doesn't work on the HalfLife weapons though as far as I remember. For that I just did as you said and stored the max clip size as a variable. Then I got the active weapon and checked the class name against the pre-stored value. If you want the current number of bullets in the primary clip use: Weapon:Clip1()
[QUOTE=Emz;17393781] [b]Doesn't work on the HalfLife weapons though as far as I remember...[/b][/QUOTE] fuckkkkk oh well I'll try it anyway [b]EDIT[/b]: God damnit, no it doesn't. I think I'm done here.
Just make a table for HL2 weapons. [lua] local hl2_weapons = { [ "weapon_pistol" ] = 18, [ "weapon_357" ] = 6, [ "weapon_physgun" ] = -1, --etc etc } -- Later on in the script for checking the clipsize local clip -- weapon being the entity and GetClass referring to it's classname local class = weapon:GetClass( ) if ( hl2_weapons[ class ] ) then -- if the classname is on the table then the weapon is writen in C++ clip = hl2_weapons[ class ] else -- Otherwise we can get it's clipsize like this clip = weapon:GetTable( ).Primary.Clipsize end -- or shorter: local clip = hl2_weapons[ weapon:GetClass( ) ] || weapon:GetTable( ).Primary.Clipsize -- Do whatever with the clip [/lua]
[QUOTE=Salads;17395792] stuff [/QUOTE] [lua]local hl2wep = {} --HAVE ACTUAL CLIP AMOUNTS hl2wep["weapon_pistol"] = 17 hl2wep["weapon_357"] = 6 hl2wep["weapon_smg1"] = 45 hl2wep["weapon_ar2"] = 30 hl2wep["weapon_shotgun"] = 6 --DO NOT HAVE CLIPS hl2wep["weapon_grenade"] = sRound hl2wep["weapon_rpg"] = sRound hl2wep["weapon_slam"] = sRound hl2wep["weapon_crossbow"] = sRound function AAHUD:primaryClipSize() if !(pl:GetActiveWeapon():GetTable().Primary == nil) then --if it exists return pl:GetActiveWeapon():GetTable().Primary.ClipSize --then use it elseif !(hl2wep[pl:GetActiveWeapon()] == nil or sRound) then --if it has a clip, and is a hl2 weapon return hl2wep[pl:GetActiveWeapon():GetClass()] --then grab it's value elseif !(hl2wep[pl:GetActiveWeapon()] == nil) then --if it doesn't have a clip, and is a hl2 weapon return pl:GetAmmoCount(pl:GetActiveWeapon():GetPrimaryAmmoType()) --then grab how much ammo is there else return "N/A" --ergo what are you doing (physgun) end [/lua] TBH I like making functions more.
[QUOTE=ashashinand;17395908][lua]local hl2wep = {} //HAVE ACTUAL CLIP AMOUNTS hl2wep[weapon_pistol] = 17 hl2wep[weapon_357] = 6 hl2wep[weapon_smg1] = 45 hl2wep[weapon_ar2] = 30 hl2wep[weapon_shotgun] = 6 //DO NOT HAVE CLIPS hl2wep[weapon_grenade] = sRound hl2wep[weapon_rpg] = sRound hl2wep[weapon_slam] = sRound hl2wep[weapon_crossbow] = sRound function AAHUD:primaryClipSize() if !(pl:GetActiveWeapon():GetTable().Primary == nil) then //if it exists return pl:GetActiveWeapon():GetTable().Primary.ClipSize elseif !(hl2wep[pl:GetActiveWeapon()] == nil or sRound) then //if it has a clip return hl2wep[pl:GetActiveWeapon()] elseif !(hl2wep[pl:GetActiveWeapon()] == nil) then // if it doesn't have a clip return pl:GetAmmoCount(pl:GetActiveWeapon():GetPrimaryAmmoType()) else return "N/A" //no ammo at all (physgun) end [/lua] TBH I like making functions more.[/QUOTE] You're doing it wrong.
[QUOTE=Salads;17396003]You're doing it wrong.[/QUOTE] It works though, :v: if you're talking about the weapon -> keys, I'm using hacky shit because I'm lazy; I'll clean it up in a sec if you're talking about my absence of locals, I don't give a damn
The table you've defined there is the equivalent of doing: [lua] local hl2wep = { } hl2wep[ nil ] = 17 hl2wep[ nil ] = 6 hl2wep[ nil ] = 45 hl2wep[ nil ] = 30 hl2wep[ nil ] = 6 -- etc [/lua] Which I'm positive isn't what you want. In addition, you're calling GetActiveWeapon( ) six times every time you call that function, which mean if its being called in a HUDPaint hook, you're calling it roughly 360 times a second. Locally defining it once at the beginning of the function and using that variable instead of calling over and over is what you should be doing. I'm going to assume you have AAHUD defined but pl isn't, unless you've defined it somewhere else as the local player. However, if you do have it defined, you're not going to be able to use your script in autorun because you'll be calling LocalPlayer( ) before there is even worldspawn which will return a null entity. The solution is to define it when the InitPostEntity hook gets called so you know that the world exists and then checking if it's valid each time before use. [lua] local pl hook.Add( "InitPostEntity", "", function( ) pl = LocalPlayer( ) end ) local function Example( ) if ( !pl ) then return end print( pl:GetPos( ) ) end [/lua] Not checking if the player's active weapon is valid and calling an entity meta function on it will give you errors in many cases. Check [lua] if ( weapon_obj && weapon_obj:IsValid( ) ) then [/lua] /critic
[QUOTE=Salads;17396475]The table you've defined there is the equivalent of doing: [lua] local hl2wep = { } hl2wep[ nil ] = 17 hl2wep[ nil ] = 6 hl2wep[ nil ] = 45 hl2wep[ nil ] = 30 hl2wep[ nil ] = 6 -- etc [/lua] [b]See below[/b] In addition, you're calling GetActiveWeapon( ) six times every time you call that function, which mean if its being called in a HUDPaint hook, you're calling it roughly 360 times a second. Locally defining it once at the beginning of the function and using that variable instead of calling over and over is what you should be doing. [b]I should, but I'm more concerned at getting everything working in concept first. Also is 360 basically 66 tick x 6 calls?[/b] [lua] local pl hook.Add( "InitPostEntity", "", function( ) pl = LocalPlayer( ) end ) local function Example( ) if ( !pl ) then return end print( pl:GetPos( ) ) end [/lua] [b]Already did it.[/b] [lua] if ( weapon_obj && weapon_obj:IsValid( ) ) then [/lua] [b]The HUD doesn't render, which doesn't call the function if the player is A: Dead/Invalid, B: Has an invalid weapon (:v:) or C: has the camera weapon out[/b] /critic[/QUOTE] I know you're trying to help, and I appreciate it.
Sorry, you need to Log In to post a reply to this thread.