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?
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?
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.
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?!
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.
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.
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.
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.
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.
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