C# design pattern for abilities that affect an object?

Started by
2 comments, last by CelticSir 4 years, 8 months ago

I asked else where on other websites for this and never got answers, I am struggling to know how to do a design pattern for abilities that you can swap in and out that affect the given object's stats.

 

Lets take a weapon like a gun as an example. Of which can have a number of attachments that affect its aim, fire rate, magazine count etc. You can add/remove the attachments. 

I can setup something like that pretty easily (although its very tedious) by creating each attachment and setting its data to what it affects. But then i ran into problems for putting restrictions in place, because then i need a unique one for each weapon they are restricted to. For example:

Scope A only attaches to gun A , C, D
Magazine A attaches only to gun B , C

When i have these specific restrictions - my tedious approach of hard writing it kinda falls apart. So i need a design pattern where this will work easily.

What is the correct way to design this in C# ? I can't figure out a clean system for this at all. 

Advertisement

What you're asking for is essentially double dispatch, and there isn't a clean way to represent this in C#.

 

Have a read of this

 

 

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
On 8/19/2019 at 4:02 AM, ChaosEngine said:

What you're asking for is essentially double dispatch, and there isn't a clean way to represent this in C#.

 

Have a read of this

 

 

Thanks for the link :) Never heard of double dispatch. Will read up on it !

This topic is closed to new replies.

Advertisement