hello!
i hope someone can help me to figure out how to let three classes to comunicate each other
i have a class A that know about the class B and C, and i have to pass data like bolean or text without B and C know each other… i know that i should use delegates for this kind of things… but i’m wondering in witch way i should use them…anyone can help me please?
There’s a lot of ways… but it hinges more on specifics of what you need.
You don’t give enough information to adequately help your specific situation.
The best we could do with the information given is do a basic intro on OOP and classes. Which honestly, a forum isn’t the best format for that, and you could just go read any intro to OOP and classes book/article/etc to learn about it.
…
So instead, how about you outline what it is exactly you’re trying to do and we may be able to assist in your specific situation.
first of all thank’s you for your help.
i have already read a lot of book and article, but you know… without a practice it’s hard to understand/remember many kind of things…
well, i’ll try to be more clear,as i said, i have the A class that know about B and C but B and C didn’t know about each other.
on the B class a method with await is running a task where a lot of resources and animation is played until a certain condition is matched, in that case the async function in the B class will be break, now, in the C class there is Onclick event (with UI Button) that when will be pressed will start again the Task on the B class…
so a recap will be
A = instantiate B and C
B = start a Task that will be breaked if a certain condition will be tru.
C = if B is in “Waiting” mode and the OnClick event is trigged the Task on B class will start again…
If C only ever effects B, and you want to hard code it as such. You can pass a reference of B to C on instantiate. Like so:
(note in my examples I’m using plane old classes… if you instantiate classes with AddComponent or CreateInstance, use that instead of course)
(it also makes assumptions that references and other stuff like your buttons are setup by you in code/editor not displayed in my examples)
public class ClassA
{
ClassB _b;
ClassC _c;
void Start()
{
_b = new ClassB();
_c = new ClassC();
_c.ClassBReference = _b;
}
}
public class ClassB
{
public bool IsRunning {get; private set;}
public async Task SomeTask()
{
IsRunning = true;
//do stuff
IsRunning = false;
}
}
public class ClassC
{
public UIButton Button;
public ClassB ClassBReference;
void Start()
{
Button.onClick.AddListener(this.OnButtonClicked);
}
private void OnButtonClicked()
{
if(!ClassBReference.IsRunning)
{
ClassBReference.SomeTask();
}
}
}
Where as if C isn’t supposed to know B because it should be usable with any number of targets not just ClassB’s… you may let A wire up the event. Like so:
public class ClassA
{
ClassB _b;
ClassC _c;
void Start()
{
_b = new ClassB();
_c = new ClassC();
_c.Button.onClick.AddListener(() => {
if(!_b.IsRunning)
{
_b.SomeTask();
}
});
}
}
public class ClassB
{
public bool IsRunning {get; private set;}
public async Task SomeTask()
{
IsRunning = true;
//do stuff
IsRunning = false;
}
}
public class ClassC
{
public UIButton Button;
}
There are even other ways to do it. Like a generalized reference to B that is passed to C (like an interface per chance?).
Or creating a factory pattern around this.
Or any other number of ways.
But those are probably the 2 simplest, if not adhoc, ways of doing it.
thank’s you for your help, the second way is what i was looking for in my case… i’ll try to implement it and let you know the results, again thank’s you!
this is working on the way that i was looking for…thanks you!
i’m getting confused if i want to get a value from class c let’s say an int, when and event is over?
for example an event is over and i need a result value from c class…