I decided to start learning LUA on the Throwing Chairs Gun example (from [URL="https://wiki.garrysmod.com/page/Chair_Throwing_Gun"]GMod Wiki[/URL]), so basically I'm going to experiment on it a bit, then edit for my tastes. My goal here is to create something simple, I came up with idea of the weapon which would throw ammo clips on the floor for other players to gain more ammo. Could be useful on starwars rp, I think.
Eventually it should contain:
[U]Design[/U]:
> model of ammo clips in hands
> model of ammo clips for thirdperson view
> sound of ammo gearing up
> preferable custom hands model
[U]Usability[/U]:
> waste ammo (not unlimited ammo packs)
> drop ammo clips (entities, I guess) instead of prop's model
> each drop shall contain not a single, but several (>20) clips per prop
So instead of models that ChairThrowGun spawns, [B]I need to spawn entities - ammo clips [/B]for, say, SMG1, not just models, but workable props.
[B]The first (and main) question[/B]: do I have to write entire function describing what's ammo and how it works or is there existing prop with said settings? How do I load it anyway - like this:
[CODE]function SWEP:ThrowAmmo( model_file )
if ( CLIENT ) then return end
local ent = ents.Create( "prop_physics" )
if ( !IsValid( ent ) ) then return end
ent:SetModel( model_file ) [/CODE]
Here we're setting model for an entity, am I right to assume that I only need to set some sort of function/hook (i.e. ent:SetSomething) already existing for SMG1 ammo clips? So my custom prop with custom model would be viewed by server as SMG1 ammo clips? And used like this? Or shall I use "ent:use" hook? If so, what other hooks for my idea would you recommend?
And if someone will [B]link me to the models, props list [/B](like "models/props/cs_office/Chair_office.mdl" from the tutorial) it would be so great, because I haven't managed to find this 'models' folder in gmod directory, I guess it's packed somewhere. Still it's always better to use something players/server already has instead of adding new files.
The ents.Create function is used to create any entity. If it's an entity already defined you wouldn't have to set the model. The reason we do this for prop_physics is because it's a class for a standard physical prop, not a defined entity. We set the model on that class so it can inherit the appearance and properties.
Things like prop_dynamic would fall in the same, only this class would not have physical properties.
If I wanted to make an ammo box, I would use something like item_ammo_pistol from hl2 ( I don't remember the entity name by heart, sorry )
As for finding models, open up your gmod spawn menu in sandbox and right click any prop. You'll be able to copy the model path and use that
prop_physics is just the class of an entity. your "model throwing gun" is already throwing entities. it's just throwing the prop entity.
another thing, if you don't already know a programming language, it is so much better to learn a programming language or at least the concepts behind one before jumping into glua.
i started learning c++ at sololearn.com ~1 year ago, and that is where I got my basics, but people say c++ isn't really a good place to start at, but I don't have enough experience to vouch for one way or the other.
if you wanna jump straight into lua, go ahead and go onto the lua website and the learn section, they explain stuff, but it's better if you know concepts behind it first, even though lua is a really simple language
again, this is all assuming you don't know any programming already
[QUOTE=solid_jake;50634934]The ents.Create function is used to create any entity. If it's an entity already defined you wouldn't have to set the model. [/quote]
Can I drop, then, entire model stuff and instead do ents.Create( "item_ammo_smg1" ) ?
What's 'if ( !IsValid( ent ) ) then return end' for?
[quote=solid_jake;50634934]The reason we do this for prop_physics is because it's a class for a standard physical prop, not a defined entity. We set the model on that class so it can inherit the appearance and properties.[/quote]
Right, for my purposes it could be dropped too, right?
[quote=solid_jake;50634934]If I wanted to make an ammo box, I would use something like item_ammo_pistol from hl2 [/quote]
Do I need to initialize entities somehow first? Like GM:InitPostEntity() ?
Where do I have to put it (before creating an entity or after) and why, what does it do? My guess is that it loads entities before player connects, right?
[quote=solid_jake;50634934]As for finding models, open up your gmod spawn menu in sandbox and right click any prop. You'll be able to copy the model path and use that[/QUOTE]
Oh, thanks for that, and I also found lots of lists (items, weapons, entities) on the old gmod wiki.
Now that I edited the code it doesn't run again:
[CODE]
--
-- Information
--
SWEP.PrintName = "AmmoSupply"
SWEP.Author = "( Warcode (based on chairthrowing gun example) )"
SWEP.Instructions = "Left mouse to drop an ammo box!"
--
-- Presets
--
SWEP.Category = "Mine"
SWEP.PrintName = "Ammo_Launcher"
SWEP.Spawnable = true
SWEP.AdminOnly = false
SWEP.UseHands = true -- Added hands, doesn't work either with or without them
SWEP.ViewModel = "models/Items/BoxMRounds.mdl" -- goes with hands
--
-- Primary&Secondary options & other stuff
--
SWEP.Primary.ClipSize = 40
SWEP.Primary.DefaultClip = 35
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "SMG1"
SWEP.Secondary.ClipSize = 40
SWEP.Secondary.DefaultClip = 35
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "SMG1"
SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.Slot = 1
SWEP.SlotPos = 2
SWEP.DrawAmmo = true
SWEP.DrawCrosshair = true
--
-- Start a sound
--
local ShootSound = Sound( "Metal.SawbladeStick" )
--
-- Function for Primary Attack
--
function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 )
-- Call 'ThrowAmmo' on self with this model
self:ThrowAmmo( "models/Items/BoxMRounds.mdl" ) -- Do I need this model?
end
--
-- Now for RightClicked Attack
--
function SWEP:SecondaryAttack()
-- Call 'ThrowAmmo' on self with this model
self:ThrowAmmo( "models/Items/BoxBuckshot.mdl" )
end
--
-- Now this is the function we called in Primary
-- & Secondary Attacks
--
function SWEP:ThrowAmmo( entity_file )
self:EmitSound( ShootSound )
if ( CLIENT ) then return end
local ent = ents.Create( "item_ammo_smg1")
if ( !IsValid( ent ) ) then return end
-- Here was 'ent:Create( entity_file )'
-- I guess it wasn't right
-- Now that we're done with initializing entities
-- Let's tell the GMod where it spawns:
ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) )
ent:SetAngles( self.Owner:EyeAngles() )
-- It knows where, now the exact action:
ent:Spawn()
-- Now let's get physics for our entity (right?)
local phys = ent:GetPhysicsObject()
if ( !IsValid( phys ) ) then ent:Remove() return end
-- Spawnable objects will be thrown with such force / velocity
local velocity = self.Owner:GetAimVector()
velocity = velocity * 100
velocity = velocity + ( VectorRand() * 10 ) -- a random element
phys:ApplyForceCenter( velocity )
-- The next part I haven't changed
--
-- Assuming we're playing in Sandbox mode we want to add this
-- entity to the cleanup and undo lists. This is done like so.
--
cleanup.Add( self.Owner, "props", ent )
undo.Create( "Thrown_Ammo" )
undo.AddEntity( ent )
undo.SetPlayer( self.Owner )
undo.Finish()
end
[/CODE]
[QUOTE]i started learning c++ at sololearn.com ~1 year ago[/QUOTE]
I learned C++, WinAPI, C#, Python and Java at university, but never got a chance to really use them iml, mostly created simple window applications for scientific purposes. Now want to contribute to community, especially to swrp, since there are a lot of ideas that could generally improve the gameplay. Not sure if I'd be able to do something major, but I'll try ;) Thanks for tips.
Disregard previous worries, I restarted gmod&server couple of times, didn't change anything in code, it all works: it spawns ammo's on player the only problem is that the player stores it, so I'll probably need to spawn crates with ammo or directly spawn (instead launching it) ammo somewhere nearby.
Once I've done with the code, I'll probably release it with proper credits. Thanks for your help!
Sorry, you need to Log In to post a reply to this thread.