Help with attachment system?

For the past 2 days I have been working on an attachment system. everything went alright up until the point i started doing “nesting” with attachments. basically a handguard for the gun being attached to the barrel, not the receiver is what i call a nested attachment. Structure:


-- default attachments. features class data. custom attachments change the name (default in this case).

-- this isn't the whole table, just how a "nested" attachment is added
[3] = {

            class = "barrel";
            default = "m4-370mm";      
            source = attachments.barrel; 

            attached = {

                [1] = {

                    sourceName = "handguard";
                    class = "handguard";
                    default = "m4-std-handguard";    

                };

            };

        };
-- and the attachment structure
return {
	weight = 0.1;
	name = "Colt buffer tube";
	class = "buffer";
	maxForwardOffset = -0.6;
	map = {
-- available mounting points
		[1] = {
			sourceName = "stock";
			class = "stock";
		}
	}
}

I also wrote a bunch of code to help it. It’s not particularly useful, so here’s a dump:
https://pastebin.com/BgV3Zn2d

The main issue is that the attachments “mount” properly but often encounter mistakes, “double-nested” attachments don’t mount correctly
image
(There should be a dot sight mounted here)

and if an attachment can have anything nested mounted on it, or has something nested mounted on it, the nested attachment overwrites the spot for the attachment “below” it

Any ideas on a plan i could use instead (i can still rewrite all of this) would be appreciated just as a fix to this horrible mistake. I’m clueless on how everything should work at this point as both of my ideas have failed so far, each one on the thing i did successfully with the other.

2 Likes

You need to renew the pastebin link