multiple classes with similar components?

Hi.Suppose you have some components (like 3 components). You have to use some of them in your classes (maybe you have to use all of them)
See below:

public interface IClass{
   void Func();
}
public class CClass:Monobehaviour,IClass{
   public void Func(){
      ComponentA.Func1();
      ComponentB.Func2();
      ComponentC.Func3();
   }
}

public class CClass1:Monobehaviour,IClass{
   public void Func(){
      ComponentA.Func1(parameters);
      ComponentB.Func2(parameters);
   }
}

public class CClass2:Monobehaviour,IClass{
   public void Func(){
      ComponentA.Func1(parameters);
      ComponentC.Func3(parameters);
   }
}

public class CClass3:Monobehaviour,IClass{
   public void Func(){
      if(Condition){
         ComponentB.Func2(parameters);
      }
      else{
          ComponentC.Func3(parameters);
      }
   }
}

How can we implement fewer classes and more generic? Can we use the command design pattern with an command array? or delegate?

Your code references fields that are undefined.

The context is also way to ambiguous.

What is it you’re attempting to accomplish?

Are you trying to call some ā€˜Func’ on a group of components? What are CClass1/2/3 for? What is your end goal, what are you trying to accomplish?

2 Likes

I have already faced it a lot.
Suppose the classes 1,2,3 are about showing a panel
I need components like effect component,sound component, animation component ,etc
but in different classes (to show panels), I need different components. Sometimes it needs all of them sometimes not,…
Do I need to define a different class for every panel?

Can you show a real world example?

1 Like

Have them all, use what you need, you can even have bools with ifs or an enum with a switch so they can use the components you want them to. Still it sounds like your logic is failing, I understand the question you want answered but it depends on the implementation.

An idea might be to have the ā€œcomponentsā€ inherit or have controler classes that inherit from one ComponentArrayItem with a public method Action(). Then have an array of ComponentArrayItem and call the Action(). Then have clases like ComponentAudio that in Action() they actually use audioSource.Play().

Or something like that, hope i got my point across. If you have any questions i’ll be here.

1 Like

I said above. Sometimes I need to play an animation when a panel was shown or play a sound or effect but in all panels, I don’t want it

I get that.

And your code example is arbitrary, and that description is vague.

I have no idea what sort of design pattern you’re shooting for here. You want to discuss design… but are doing very little to demonstrate design.

…

You know, never mind.

This is what your threads often devolve into.

I don’t know why I even partake in your conversations.

if(iWantToPlayAnim){
    animatotion.Play();
}
if(iWantToPlaySound){
    sound.Play();
}
1 Like

you mean something like below?

public interface ICommand{
   void Run();
}
public class CPlayMusicCommand:Monobehaviour,ICommand{
   [SerializeField]
   AudioSource m_audioSource;
   public void Run(){
      m_audioSource.Play();
   }
}
public class CPlayAnimationCommand:Monobehaviour,ICommand{
   [SerializeField]
   Animator m_animator;
   public void Run(){
      m_animator.Play("anim",0,0);
   }
}
public class CShowPanelController{
   ICommand[] commands;
   void OnEnable(){// show a panel
      for(int i=0;i<commands.Length;i++){
         commands[i].Run();
      }
   }
}

and use a controller that keeps an array of commands and call the commands? Run()
when show a special panel?
but if it was complex?!

1 Like

Exactly that, maybe turn ICommand into a class, since you can’t assign interfaces on the editor.

1 Like

you need to correct yourself at first. I don’t like your conversation
Do I force you to talk with me that you speak like this?

Vague description, indeed.

My first recommendation are events or a listener-interface design, as it looks like a variable number of ā€œcomponentsā€ are supposed to just ā€œwaitā€ for something to happen. That’s a perfect situation for event-based designs.

Perhaps I’m mistaken though.

3 Likes

I’m not the only person who finds you vague, lacking in description of your problems, and often leaving out critical information and bringing it up later like it should have been known the entire time.

I’m just not as sweet as everyone else. And instead insist that if you expect us to offer up our time/resources to assist/discuss with you… that maybe you should offer up a little more effort in describing your problems. I consider it both rude to me, and rude to my fellow forum members.

By allowing shoddy descriptions of problems to persist, it fosters a notion that forum members can just vaguely meander through their problem until some kind soul comes in and sifts through the nonsense and magically pulls an answer out. Both wasting their time, and offering no true learning experience for the OP. Teach a man to fish, not give a man a fish.

As my signature says:

1 Like

I’m really not sure what you’re asking, your posts are unclear and vague and already suggested…

But either way I would refrain from having an Interface of something extremely generic like ICommand with a Run() method in it. Not only would you have to loop through every component every time you want to use it but you have zero guarantee that it’s going to be safe to use. Seems like a bad use of Interfaces to me.

1 Like

Not trying to argue, but i don’t understand where you are coming from. Isn’t the idea to loop through them? and what do you mean about not guaranteed to be safe?

From his examples it looks like he wants to put a Run() method on a bunch of unrelated components and ā€˜fire and forget’ on some object. If he has say, five components on that object that use ICommand then in order to use them all you have to get all of the components and target the ones that use ICommand. Then you are presented with the issue of whether or not you want to actually ā€œRun()ā€ all of those. What is Run() used by? Something so generic is dangerous to fire because it could be used by unrelated systems and have unexpected results.

Whereas if you used something more specific like IUseAnimation { Animate() }, IUseParticles{ PopParticles() } and IHoverHighlight { Highlight() } then you can have your code target more explicitly and specifically what you want to occur, perhaps only firing animations and particles but not highlighting, rather than just hoping that there aren’t components that are going to use the generic Run() method and say, Highlight, when you don’t want them to.

Overall it’s a smelly approach when there are definitely more explicit and clearer approaches.

4 Likes

are you ok? stop spam you are rude WTF
I speak politely. I say again. Speak kindly or dont speak or discuss. I don’t owe you. Understand? never answer to my questions if you think you waste time
I helped to other developers a lot but you need to change yourself
If you see others answered please finish and don’t bother yourself

Just to be clear, we pretty much agree with his points. You’d benefit from trying to understand what he’s saying and use that criticism to create better posts which will in turn yield better responses.

1 Like

he doesn’t need to speak 5 lines and insult me
he can say politely, ā€œCan you say an example and give more infoā€
Even when I said an example clearly and some one answered, he didn’t finish his discussion!

So you prefer to create special classes like a class to show a panel with animation and not effect, other class uses playsound and animation and another one uses effect component etc?
Thanks