Hi. My problem is simple. I want to make a SWEP that increases the player's speed when swimming. What do I need? I have the code for the SWEP, minus the swim speed increase:
[code]
//Weapon-Spawning
SWEP.AdminSpawnable = false
SWEP.Spawnable = true
//Model Related
SWEP.ViewModelFOV = 54
SWEP.ViewModel = ""
SWEP.WorldModel = ""
SWEP.HoldType = "normal"
SWEP.Usehands = false
//Game Related
SWEP.Slot = 1
SWEP.SlotPos = 3
SWEP.Weight = 8
SWEP.base = "weapon_base"
//Spawnmenu Related
SWEP.Category = "Other"
SWEP.PrintName = "SwimFins"
SWEP.Author = "QuickNinjaCat"
SWEP.Purpose = "Makes you swim faster. What were you expecting?"
resource.AddFile( "materials/VGUI/entities/weapon_qnc_swimfins.vmt" )
//Misc. Settings
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.FiresUnderwater = false
SWEP.DrawCrosshair = true
SWEP.DrawAmmo = true
SWEP.DrawWeaponInfoBox = false
SWEP.BounceWeaponIcon = false
//Primary Fire Settings
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
//Secondary Fire Settings
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
//Adding Swimming Fin Models to Feet
SWEP.WElements = {
["rightfoot_fin"] = { type = "Model", model = "models/freeman/fin.mdl", bone = "ValveBiped.Bip01_R_Foot", rel = "", pos = Vector(8.75, 2.75, 0), angle = Angle(0, -35, 90), size = Vector(1, 1, 1), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
["leftfoot_fin"] = { type = "Model", model = "models/freeman/fin.mdl", bone = "ValveBiped.Bip01_L_Foot", rel = "", pos = Vector(8.75, 2.75, 0), angle = Angle(0, -35, 90), size = Vector(1, 1, 1), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
}
//Initialize Code
function SWEP:Initialize()
self:SetWeaponHoldType( self.HoldType )
// other initialize code goes here
if CLIENT then
// Create a new table for every weapon instance
self.VElements = table.FullCopy( self.VElements )
self.WElements = table.FullCopy( self.WElements )
self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
self:CreateModels(self.VElements) // create viewmodels
self:CreateModels(self.WElements) // create worldmodels
// init view model bone build function
if IsValid(self.Owner) then
local vm = self.Owner:GetViewModel()
if IsValid(vm) then
self:ResetBonePositions(vm)
// Init viewmodel visibility
if (self.ShowViewModel == nil or self.ShowViewModel) then
vm:SetColor(Color(255,255,255,255))
else
// we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
vm:SetColor(Color(255,255,255,1))
// ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
// however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
vm:SetMaterial("Debug/hsv")
end
end
end
end
end
function SWEP:Holster()
if CLIENT and IsValid(self.Owner) then
local vm = self.Owner:GetViewModel()
if IsValid(vm) then
self:ResetBonePositions(vm)
end
end
return true
end
function SWEP:OnRemove()
self:Holster()
end
if CLIENT then
SWEP.vRenderOrder = nil
function SWEP:ViewModelDrawn()
local vm = self.Owner:GetViewModel()
if !IsValid(vm) then return end
if (!self.VElements) then return end
self:UpdateBonePositions(vm)
if (!self.vRenderOrder) then
// we build a render order because sprites need to be drawn after models
self.vRenderOrder = {}
for k, v in pairs( self.VElements ) do
if (v.type == "Model") then
table.insert(self.vRenderOrder, 1, k)
elseif (v.type == "Sprite" or v.type == "Quad") then
table.insert(self.vRenderOrder, k)
end
end
end
for k, name in ipairs( self.vRenderOrder ) do
local v = self.VElements[name]
if (!v) then self.vRenderOrder = nil break end
if (v.hide) then continue end
local model = v.modelEnt
local sprite = v.spriteMaterial
if (!v.bone) then continue end
local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
if (!pos) then continue end
if (v.type == "Model" and IsValid(model)) then
model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
model:SetAngles(ang)
//model:SetModelScale(v.size)
local matrix = Matrix()
matrix:Scale(v.size)
model:EnableMatrix( "RenderMultiply", matrix )
if (v.material == "") then
model:SetMaterial("")
elseif (model:GetMaterial() != v.material) then
model:SetMaterial( v.material )
end
if (v.skin and v.skin != model:GetSkin()) then
model:SetSkin(v.skin)
end
if (v.bodygroup) then
for k, v in pairs( v.bodygroup ) do
if (model:GetBodygroup(k) != v) then
model:SetBodygroup(k, v)
end
end
end
if (v.surpresslightning) then
render.SuppressEngineLighting(true)
end
render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
render.SetBlend(v.color.a/255)
model:DrawModel()
render.SetBlend(1)
render.SetColorModulation(1, 1, 1)
if (v.surpresslightning) then
render.SuppressEngineLighting(false)
end
elseif (v.type == "Sprite" and sprite) then
local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
render.SetMaterial(sprite)
render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
elseif (v.type == "Quad" and v.draw_func) then
local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
cam.Start3D2D(drawpos, ang, v.size)
v.draw_func( self )
cam.End3D2D()
end
end
end
SWEP.wRenderOrder = nil
function SWEP:DrawWorldModel()
if (self.ShowWorldModel == nil or self.ShowWorldModel) then
self:DrawModel()
end
if (!self.WElements) then return end
if (!self.wRenderOrder) then
self.wRenderOrder = {}
for k, v in pairs( self.WElements ) do
if (v.type == "Model") then
table.insert(self.wRenderOrder, 1, k)
elseif (v.type == "Sprite" or v.type == "Quad") then
table.insert(self.wRenderOrder, k)
end
end
end
if (IsValid(self.Owner)) then
bone_ent = self.Owner
else
// when the weapon is dropped
bone_ent = self
end
for k, name in pairs( self.wRenderOrder ) do
local v = self.WElements[name]
if (!v) then self.wRenderOrder = nil break end
if (v.hide) then continue end
local pos, ang
if (v.bone) then
pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
else
pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
end
if (!pos) then continue end
local model = v.modelEnt
local sprite = v.spriteMaterial
if (v.type == "Model" and IsValid(model)) then
model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
model:SetAngles(ang)
//model:SetModelScale(v.size)
local matrix = Matrix()
matrix:Scale(v.size)
model:EnableMatrix( "RenderMultiply", matrix )
if (v.material == "") then
model:SetMaterial("")
el
Use [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/WaterLevel"]Entity:WaterLevel[/URL]
I'm not 100% sure if it works on players but I see no reason why not. As for player movement speed you'll need something like [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/SetPlayerSpeed]GM:SetPlayerSpeed[/url]
[QUOTE=MattJeanes;50282560]Use [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/WaterLevel"]Entity:WaterLevel[/URL]
I'm not 100% sure if it works on players but I see no reason why not. As for player movement speed you'll need something like [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/SetPlayerSpeed]GM:SetPlayerSpeed[/url][/QUOTE]
Okay. Where would I need to put that in my code? Would it need to be a function or something?
[QUOTE=QuickNinjaCat;50282582]Okay. Where would I need to put that in my code? Would it need to be a function or something?[/QUOTE]
I think Entity:WaterLevel is a function, not completely sure though. You can click on his link to read more about it so you can better understand it. I can however give you the code that will change the player's speed.
[CODE]
self.Owner:SetWalkSpeed(YourSpeed)
self.Owner:SetRunSpeed(RunSpeed)
self.Owner:SetMaxSpeed(MaxSpeed)
return true
[/CODE]
[editline]8th May 2016[/editline]
Yes Entity:WaterLevel is a function
[editline]8th May 2016[/editline]
So you can do something like this:
[CODE]if ( ent:WaterLevel() > 2 ) then
self.Owner:SetWalkSpeed(YourSpeed)
self.Owner:SetRunSpeed(YourRunSpeed)
self.Owner:SetMaxSpeed(YourMaxSpeed)
return true
end
if (ent:WaterLevel() < 2) then
self.Owner:SetWalkSpeed(250)
self.Owner:SetRunSpeed(255)
return true
end[/CODE]
Not sure if this will work but it's worth a shot. :v:
[editline]8th May 2016[/editline]
Return values:
0 - not in the water at all.
1 - slightly submerged in water. (feet in water)
2 - majority of entity in water. (waist in water)
3 - completely underwater. (head in water)
[editline]8th May 2016[/editline]
Let me explain the code.
If completely underwater then set walk speed to x, set run speed to x, and set the max speed to x.
If slightly submerged in water or not at all, set walk speed to normal (which is 250).
[editline]8th May 2016[/editline]
[QUOTE=MattJeanes;50282560]Use [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/WaterLevel"]Entity:WaterLevel[/URL]
I'm not 100% sure if it works on players but I see no reason why not. As for player movement speed you'll need something like [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/SetPlayerSpeed]GM:SetPlayerSpeed[/url][/QUOTE]
"It appears that this function only works properly with players." -Gmod wiki
I'm not sure If you have to state what the entity is though. :what:
Thanks Lord Melvin! Also, I like your profile picture. :v:
[QUOTE=QuickNinjaCat;50283303]Thanks Lord Melvin! Also, I like your profile picture. :v:[/QUOTE]
Wow! It worked? I mean, of course it worked! :saxout:
[QUOTE=Lord_Melvin;50283333]Wow! It worked? I mean, of course it worked! :saxout:[/QUOTE]
Actually, it didn't work. This is the console error I got:
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539: attempt to index global 'ent' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539
[/code]
I put the string of code you gave me in the bottom. So I guess I need to specify the player somehow.
[editline]8th May 2016[/editline]
I know that I have a SWEP installed that increases my player's speed and health. I'll check out that SWEP's code and find what I need to add or copy over. I am such an asshole.
[QUOTE=QuickNinjaCat;50283372]Actually, it didn't work. This is the console error I got:
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539: attempt to index global 'ent' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539
[/code]
I put the string of code you gave me in the bottom. So I guess I need to specify the player somehow.
[editline]8th May 2016[/editline]
I know that I have a SWEP installed that increases my player's speed and health. I'll check out that SWEP's code and find what I need to add or copy over. I am such an asshole.[/QUOTE]
Instead of ent:WaterLevel() try self.Owner:WaterLevel(). Sorry my bad.
:suicide:
[editline]8th May 2016[/editline]
[QUOTE=QuickNinjaCat;50283372]Actually, it didn't work. This is the console error I got:
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539: attempt to index global 'ent' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539
[/code]
I put the string of code you gave me in the bottom. So I guess I need to specify the player somehow.
[editline]8th May 2016[/editline]
I know that I have a SWEP installed that increases my player's speed and health. I'll check out that SWEP's code and find what I need to add or copy over. I am such an asshole.[/QUOTE]
You're not an asshole, you just have an idea you want to try.
[QUOTE=Lord_Melvin;50283412]You're not an asshole, you just have an idea you want to try.[/QUOTE]
Good point. :trumpet:
[editline]8th May 2016[/editline]
[QUOTE=Lord_Melvin;50283412]Instead of ent:WaterLevel() try self.Owner:WaterLevel(). Sorry my bad.
[/QUOTE]
Tried that, got this in the console:
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539: attempt to index global 'self' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539
[/code]
Do I need a specific place for the string of code you gave me? Because I just placed it at the end of the SWEP lua file.
[QUOTE=QuickNinjaCat;50283461]Good point. :trumpet:
[editline]8th May 2016[/editline]
Tried that, got this in the console:
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539: attempt to index global 'self' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:539
[/code]
Do I need a specific place for the string of code you gave me? Because I just placed it at the end of the SWEP lua file.[/QUOTE]
Well I can see some problems putting it at the bottom of the script. Try and put it above you're SWEP:Holster() function.
[QUOTE=Lord_Melvin;50284773]Well I can see some problems putting it at the bottom of the script. Try and put it above you're SWEP:Holster() function.[/QUOTE]
After putting it in the SWEP:Holster() function, I get this console error about trying to index a global "WaterLevel":
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:545: attempt to call method 'WaterLevel' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:545
[/code]
I do recall seeing somewhere that a TTT flaregun's code used the "waterlevel" code to put out a flaming player. I don't play TTT, but I'll see if the flaregun is part of Gmod by default.
I found the code in the file "gamemodes\terrortown\entities\weapons\weapon_ttt_flaregun.lua"
Here is the code:
[code]
local function RunIgniteTimer(ent, timer_name)
if IsValid(ent) and ent:IsOnFire() then
if ent:WaterLevel() > 0 then
ent:Extinguish()
elseif CurTime() > ent.burn_destroy then
ent:SetNotSolid(true)
ent:Remove()
else
-- keep on burning
return
end
end
timer.Destroy(timer_name) -- stop running timer
end
[/code]
Should I create a local function with something like this as the code?
[code]
local function IsSwimming(ent)
if IsValid(ent) then
if ent:WaterLevel() > 2 then
player:SetWalkSpeed(400)
player:SetRunSpeed(405)
elseif ent:WaterLevel() < 2 then
player:SetWalkSpeed(250)
player:SetRunSpeed(255)
end
else
end
end
end
[/code]
The general idea gets across.
[QUOTE=QuickNinjaCat;50297170]After putting it in the SWEP:Holster() function, I get this console error about trying to index a global "WaterLevel":
[code]
[ERROR] addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:545: attempt to call method 'WaterLevel' (a nil value)
1. unknown - addons/swimfins/lua/weapons/weapon_qnc_swimfins.lua:545
[/code]
I do recall seeing somewhere that a TTT flaregun's code used the "waterlevel" code to put out a flaming player. I don't play TTT, but I'll see if the flaregun is part of Gmod by default.
I found the code in the file "gamemodes\terrortown\entities\weapons\weapon_ttt_flaregun.lua"
Here is the code:
[code]
local function RunIgniteTimer(ent, timer_name)
if IsValid(ent) and ent:IsOnFire() then
if ent:WaterLevel() > 0 then
ent:Extinguish()
elseif CurTime() > ent.burn_destroy then
ent:SetNotSolid(true)
ent:Remove()
else
-- keep on burning
return
end
end
timer.Destroy(timer_name) -- stop running timer
end
[/code]
Should I create a local function with something like this as the code?
[code]
local function IsSwimming(ent)
if IsValid(ent) then
if ent:WaterLevel() > 2 then
player:SetWalkSpeed(400)
player:SetRunSpeed(405)
elseif ent:WaterLevel() < 2 then
player:SetWalkSpeed(250)
player:SetRunSpeed(255)
end
else
end
end
end
[/code]
The general idea gets across.[/QUOTE]
I don't see why not .>.
Sorry, you need to Log In to post a reply to this thread.