SWEP:Reload singleplayer equivalent

I have code under the SWEP:Reload() hook that gets called only in multiplayer, and I want it to be called in singleplayer too. Is there a workaround for getting SWEP:Reload() code to get called in singleplayer?

It gets called serverside only in single-player for prediction reasons. You shouldn’t rely for it to be called clientside, even in multi-player – you should be networking where applicable.

Care to elaborate? I am not very experienced with Lua.

In a shared weapon file:


function SWEP:Reload()
   print("Foo")
end

Will only print serverside in single-player. In multi-player, it will print once serverside and one+ times clientside (for prediction). You should not rely on the method to be called clientside at all, and instead use manual networking for anything clientside-specific you need. Of course, posting your own code would help.

Basically I have a machinegun that ejects a bullet casing and a link (the thing that holds the rounds together in the belt) when fired. However, when the gun is first fired after either being reloaded or deployed, a starting link is ejected instead of the normal link. I know it sounds overkill, but I’m going for attention to detail on this one.



SWEP.Shell = "7.62x51"
SWEP.ShellLink = "vollmer_link"
SWEP.ShellLinkStart = "vollmer_link_start"

SWEP.HasNotFired = true

function SWEP:CreateShell(sh, link, stlink) --this code is called when the gun is fired
	if self.Owner:ShouldDrawLocalPlayer() then
		return
	end
	
	sh = self.Shell or sh --regular bullet casing
	link = self.ShellLink or link --link
	stlink = self.ShellLinkStart or stlink --starting link
	att = self.Wep:GetAttachment(self.Wep:LookupAttachment("ejector")) --where the casings are ejected from

	if att then
		att.Pos.z = att.Pos.z - 2
		dir = att.Ang:Forward()
	
		if self.HasNotFired then
			self.HasNotFired = false
			FAS2_MakeFakeShell(stlink, att.Pos + dir * 3, EyeAngles(), dir * 100, 0.6, 5)
		else
			FAS2_MakeFakeShell(link, att.Pos + dir * 3, EyeAngles(), dir * 100, 0.6, 5)
		end
		FAS2_MakeFakeShell(sh, att.Pos, EyeAngles(), dir * 200, 0.6, 5)
	end
end

function SWEP:Reload()

	self.HasNotFired = true --this is the part that doesn't work in singleplayer, it does not change which results in the gun ejecting the normal link instead of the starting link like it should after being reloaded
        
        --more reload stuff


I can post pictures if further elaboration is needed.

Firstly, you should initialise HasNotFired inside of your Initialize method. Otherwise, all instances of your weapon’s “HasNotFired” will change when one does. You should use a NetworkVar to manage it.

How would I tell said NetorkVar to change when the gun is reloaded in singleplayer?

  1. http://wiki.garrysmod.com/page/WEAPON/SetupDataTables
    this should be used in predicted hooks.

  2. i guess you could use this as well http://wiki.garrysmod.com/page/Entity/SetNWBool but the datatable is probably better

  3. https://wiki.garrysmod.com/page/game/SinglePlayer

  4. What that guy said