Forcing attachments on FA:S 2 Alpha SWEPs

Tried
[lua]wep:Attach(1, “compm4”)[/lua]
In my gamemode’s spawn function, copy pasted from an attachment file from FA:S 2 SWEPs, surprisingly wep is not nil, but Attach is. What I wanna do is force player to have a certain attachment at all times if he selects it in a menu I made.

[editline]ffs why do i need to put something here if it just shows edited[/editline]
Found this code in sv_attachments
[lua]local group, att, wep, found, t, t2, CT

local function FAS2_Attach(ply, com, args)
if not ply:Alive() then
return
end

wep = ply:GetActiveWeapon()

if not IsValid(wep) or not wep.IsFAS2Weapon then
	return
end

group, att = tonumber(args[1]), args[2]

if not group or not att or not wep.Attachments or wep.dt.Status != FAS_STAT_CUSTOMIZE or wep.dt.Bipod or wep.NoAttachmentMenu or not table.HasValue(ply.FAS2Attachments, att) then
	return
end

t = wep.Attachments[group]

if t then
	found = false
	
	for k, v in pairs(t.atts) do
		if v == att then
			found = true
		end
	end
	
	if t.lastdeattfunc then
		t.lastdeattfunc(ply, wep)
		t.lastdeattfunc = nil
	end
	
	if found then
		t.last = att
		
		t2 = FAS2_Attachments[att]
		
		if t2.attfunc then
			t2.attfunc(ply, wep)
		end
			
		if t2.deattfunc then
			t.lastdeattfunc = t2.deattfunc
		end
		
		umsg.Start("FAS2_ATTACH", ply)
			umsg.Short(group)
			umsg.String(att)
		umsg.End()
	end
end

end

concommand.Add(“fas2_attach”, FAS2_Attach)[/lua]

Although, I can’t get this to work because I don’t know what exactly group is, nor com. What are the arguments, what should I put for them?

do it yourself for once and use notepad++ to search through the fas addon for fas2_attach occurrences to find the arguments

I tried. It’s not on my level, most things are 1 letter and I just can’t comprehend how/where they are FULLY defined. For example, at the top of the file there is local, which aren’t even defined. To me, it looks like they made ‘t’ appear out of thin air and made it do stuff that it shouldn’t be able to do.

blame spy’s coding, I’ll look at it later

sooo

[editline]a[/editline]
jeezy why

iirc spy/leetnoobs gamemode green shift allowed players to spawn with attachments on their gun already, using a modified customizable weaponry base. since there might be similarities in how the attachment system is done between these two weapon bases (I only say might because I know that fas:2 uses bodygroups instead of external models for attachments, and spy did a hell of a lot of improvements and optimisation with fas:2 as opposed to CW) that would be a good place to start.

if I remember I’ll message spy and ask him about this and see if I can get him to post in this thread. no promises though.

Yes! I figured out how to use the fas_attach command!


fas2_attach 1 compm4

Attaches my compm4.

There’s still a problem though… I’ve tried messing with the code and stuff, but the command won’t work unless I’m in the FA:S 2 C menu ._.

Please?

So you’ll need to tweak your own FA:S base
It’s not easy, you haven’t writed it, you don’t know what spy had in mind…I had that feeling too with pointshop 2, but i could fix some stuff and build my own additions

Just dig, search references, try to traceback everything, discover and learn the base by yourself
If you can’t understand the code, you’re looking it with the wrong pov, because i know that spy it’s a good coder, i bought the swb weps and that shit it’s the best weapon base that i ever tasted

I have worked with the FAS base before. Not to insult spy’s coding abilities but thede the code is actually horrible to work with. Variable names are one to two characters long and the code is more or less completely uncommented.

While it’s definitely great work and a nice base it was clearly never intended to be worked on/with by anyone other than the coder. (This also becomes very obvious once you talk to spy a bit - there are no intentions of adding functions to do this easily or integration features).

For example the concept of locals is completely violated consistently throughout the base, he does C-Style variable declarations with loads of vars declared on one line in file-local or function scope even if they are only used in a much smaller scope.

I have a working example of attaching stuff through lua and will post it later :slight_smile:

BTW: great that you figured out a few things in ps2! I would appreciate any feedback on which parts need work, better documentation or an easier interface.

After adding 6 new items type and 5 passive entities, the MAIN issue that i found and would helped a lot…It’s just an example for an item base :v
Also important hooks for items, like “Think”, but most of my edits were the skin because i didn’t understand why i couldn’t change few elements, like the inventory that turns…Ew ugly? when you have more than 6 slots to equip

I wasted lot of time because the “sh_base_badge” by some reason, loaded before something important and this breaked ps2

So, what could do ps2 documentation better? Just add few links with some examples to download, like TTT page does

Here you can take a look few of my items base, i don’t think that them would require any modification like the hud one or the noise maker needed

I looked through the code various times, I found what state the weapon is in when it’s in customization menu. I successfully removed all lines that check that in anything having to do with attachments, but it’s still not working…

Thanks for the feedback, i’ll add some examples!

[lua]
FAS2 = {}
function FAS2:isAttachmentCompatible( weapon, attachmentId, attachmentId2 )
local attachmentsTable = weapon.Attachments

local found = false
for _, group in pairs( attachmentsTable ) do
	if table.HasValue( group, attachmentId ) then
		found = true
	end
	if table.HasValue( group, attachmentId ) and
	   table.HasValue( group, attachmentId2 ) then
		return false
	end
end
if not found then 
	return false 
end

return true

end

if SERVER then
util.AddNetworkString( “PSL_FAS2_ATTACH” )

--SV function
function FAS2:addAttachmentToWeapon( swep, attachment )
	local attachmentId = attachment.AttachId
	if IsValid( swep ) and swep.IsFAS2Weapon then
		local group
		for _, attachmentGroup in pairs( swep.Attachments ) do
			if table.HasValue( attachmentGroup.atts, attachmentId ) then
				group = attachmentGroup
			end
		end
		if not group then 
			KLogf( 3, "Invalid attachment given to FAS2:addAttachmentToWeapon, weapon was %s, attachment was %s", swep.ClassName, attachmentId )
			return
		end
		
		local oldPlayAnim = FAS2_PlayAnim
		FAS2_PlayAnim = function( ) end
		
		if group.lastdeattfunc then
			group.lastdeattfunc( swep.Owner, wep )
			group.lastdeattfunc = nil
		end
		
		group.last = attachmentId
		
		local fasAttachmentTable = FAS2_Attachments[attachmentId]
		
		if fasAttachmentTable.attfunc then
			fasAttachmentTable.attfunc( self.Owner, swep )
		end
			
		if fasAttachmentTable.deattfunc then
			group.lastdeattfunc = fasAttachmentTable.deattfunc
		end
		
		FAS2_PlayAnim = oldPlayAnim
	
		net.Start( "PSL_FAS2_ATTACH" )
			net.WriteEntity( swep )
			net.WriteString( attachmentId )
		net.Send( swep.Owner )
	else
		KLogf( 3, "Invalid weapon passed to FAS2:addAttachmentToWeapon %s", ( not IsValid( swep ) ) and "(Not Valid)" or "(Not FAS2)" )
		debug.Trace( )
	end
end

end

if CLIENT then
function FAS2:attachmentAddedToWeapon( swep, attachment )
local attachmentId = attachment.AttachId
local group
for _, attachmentGroup in pairs( swep.Attachments ) do
if table.HasValue( attachmentGroup.atts, attachmentId ) then
group = attachmentGroup
end
end
group.active = attachment.Name
group.last = group.last or {}
group.last[attachment.Name] = true

	local fasAttachmentTable = FAS2_Attachments[attachmentId]
	
	if fasAttachmentTable.aimpos then
		swep.AimPos = swep[fasAttachmentTable.aimpos]
		swep.AimAng = swep[fasAttachmentTable.aimang]
		swep.AimPosName = fasAttachmentTable.aimpos
		swep.AimAngName = fasAttachmentTable.aimang
	end
	
	local oldPlayAnim = FAS2_PlayAnim
	FAS2_PlayAnim = function( ) end
	
	if group.lastdeattfunc then
		group.lastdeattfunc(ply, wep)
	end
	
	if fasAttachmentTable.clattfunc then
		if IsValid( wep ) then
			fasAttachmentTable.clattfunc(ply, wep)
		end
	end
	
	FAS2_PlayAnim = oldPlayAnim
	
	group.lastdeattfunc = fasAttachmentTable.cldeattfunc
	
	swep:AttachBodygroup( attachmentId )
end
net.Receive( "PSL_FAS2_ATTACH", function( len )
	local weapon, attachmentId = net.ReadEntity( ), net.ReadString( )
	local attachment = PSLoadout.getAttachment( attachmentId )
	FAS2:attachmentAddedToWeapon( weapon, attachment )
end )

end
[/lua]

You can use FAS2:addAttachmentToWeapon( ply:GetActiveWeapon(), “compm4” ) for example to attach something

That’s the point. The FA:S 2.0 base was never intended to have any modifications done to it or have custom weapons made on it. As such, there are little to no comments and no documentation. I’m working on CW 2.0 though, which is the exact opposite. There are loads of comments, it’s much more modular and it will come with documentation.

Not to mention, if you’re looking for adding your own attachments, the attachment system it comes with is very flexible.

Though it would be solved but… this line doesn’t want to work with me.
[lua]local attachmentId = attachment.AttachId[/lua]
“bad key string to index”
“(number expected, got string)”

Oh i think i forgot a bit of code, this was part of a larger system that also supports CSTM, i’ll have a look and post a solution later.