Simple Can Shooter

Hey guys. It’s me, WTF_Nuke and I just started LUA. Well not really started, but began doing stuff. So recently I saw a _Killburn’s latest invention, which was a Can Shooter. It looked awesome and I was thinking that making something along those lines would be a great start. So I did. It’s really simple right now, and can’t do much but flail cans at high velocity. So without further ado, here is a vid:

http://www.garrysmod.org/img/?t=dll&id=89909
Disclaimer _Killburn isn’t involved in this in any way, I just liked his idea and created my own.
Credits: Me, _KIllburn for the idea, MegaJohnny for help, Grea$eMonkey for help, and NullPoint for help. You guys are awesome.

Cool, some tips, do an SetOwner(self.Owner) somewhere so you actually get the kill! Next thing: Different can types? sticky cans the explode and all that.

cool, that’s a good way start learning lua.

Oh that’s how you do it, thanks. Yeah, I’m going to add a combustion can soon, I was just being lazy earlier.
EDIT: So I was thinking of adding maximum ammo capacity. How would I do that? I was thnking of checking the Ammo1 and ammo2 and if they were above the max than it would take away ammo until it is at the max via loop. Is there a simpler way to do this?

can you du that you can pick cans as an ammo? it will be realy cool

Yeah I’m working on it right now. I just need some help with my Entities.

There is a simpler way to do it than with a loop. There’s a player.SetAmmo function that lets you set the ammo of a particular type, rather than adding or taking away ammo.

One way is
[lua]
if self.Owner:GetAmmoCount(“ammotype”) > maxammo then
self.Owner:SetAmmo(maxammo, “ammotype”)
end
[/lua]
There’s a way that only needs one line of code. Math.min takes 2 numbers and returns whichever’s smaller.

[lua]
self.Owner:SetAmmo(math.min(self.Owner:GetAmmoCount(“ammotype”), maxammo), “ammotype”)
[/lua]

Replacing the placeholders with whatever’s relevant.

I’m thinking this code would go in SWEP:Initialize on the server.

Thanks for the help man. Really appreciate it.

Could you get a link up please? I would very much like to try this :slight_smile:

A link will be up shortly. I am adding some features with flammable cans and other doo-dads like max ammo.

You should add time bomb cans, you shoot them, they beep a few times, and explode with the Half-Life 1 sound effect. That’d be :awesome:

maybe make it so you spectate the can until it hits something?
that would be interesting

[editline]04:07PM[/editline]

maybe some post proccessing

I might implement a bomb can in the future (can-ade launcher or something witty) but I don’t know how to make it spectate the can since I am quite new to LUA.

Why is GetAmmoCount a nil value?

For spectating look at this:

http://www.facepunch.com/showthread.php?t=885113

It’s complaining that GetAmmoCount doesn’t exist? That seems very strange. I’m sure it exists, it has a Wiki article. Can we see the code generating the nil value error?

Weapon:GetAmmoCount use’s a number value (enum) not a string.

These are returned by the function: Weapon:GetPrimaryAmmoType()

Even if he was using weapon.GetAmmoCount and giving it a bad argument, it shouldn’t be giving him a nil value error for that reason.

If he’s using my code without alterations then he’ll be using player.GetAmmoCount rather than the weapon one. And the player one does take a string according to the Wiki.

I think I see the problem. Since I put it in initialize, the thing is only called when the weapon is spawned. If I spawn the weapon on the floor or has no owner. Maybe put it in think?

Of course, I didn’t think about that. My mistake.

I found something that might work, though. SWEP:Equip is called when the weapon gets picked up, and SWEP:EquipAmmo is called when you pick up another of the weapons, but since you already have one it just gives you ammo. You could put your max ammo code in SWEP:EquipAmmo.

But instead of having self.Owner in the code it would be better to put NewOwner as per the argument to SWEP:EquipAmmo.

Oh ok thanks. Wait so self.NewOwner?