- Home /
check if part of function is executed in another script
rather new to programming.
I'm making a 2d game where you control a hand and have to defend against insects. I would like the insects to be destroyed when the controls (gameobjectet) are on top of the insects and a given function from the script/class associated with the controls are called.
I need to check if the function in question (Called: Attack) is called from another script that is associated with the insects. I thought to do like demonstrated in this thread: http://answers.unity3d.com/questions/653021/how-to-check-if-a-function-is-called.html
but the function 'Attack' has a few 'if statements' within and I only want the insects to be destroyed if one specific if statement is carried through.
How do I check if part of a function in another script is executed?
Since you are new to program$$anonymous$$g I guess event is too far fetched but that would be the way I would use.
Having an event called at that specific part and the other class listening to it.
$$anonymous$$ore simple way would be to have the Attack method to call a method on the other script which turns out to become spaghetti code as everyone interferes with everyone.
ok.. hmm would this be a good tutorial to follow? https://www.youtube.com/watch?v=N2zdw$$anonymous$$IsXJs
And if yes, how would I use the different parts he mention (listener, maneger etc.) to get the effect I want? biggest question how do i make so the event only happens in a certain part for the attack method?
I added a simplify version of the attack method - i only want an event to happen when "Input SucessFG" happens
float timeframe = 0;
bool chkinput=false;
public void Attack (){
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.F)) {
chkinput = true;
}
if (chkinput) {
timeframe += 1 * Time.deltaTime;
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.G)) {
print ("Input SucessFG");
timeframe = 0;
chkinput = false;
}
if (timeframe > 1) {
print ("Time Out");
timeframe = 0;
chkinput = false;
}
}
}
you create an event and you subscribe a listener. These are the main ideas.
public event Action OnSomethingDone = ()=>{};
public void Attack(){
if(condition){
OnSomethingDone();
}
}
This is for the event. It uses the generic way ins$$anonymous$$d of the old school delegate way shown on the video but the result is the same.
Now for the listener:
void Start(){
OtherType other = GetComponent<OtherType>();
other.OnSomethingDone += Listener$$anonymous$$ethod;
}
void OnDestroy(){
OtherType other = GetComponent<OtherType>();
other.OnSomethingDone -= Listener$$anonymous$$ethod;
}
void Listener$$anonymous$$ethod(){}
Note when there are parenthesis and when there is none. Pretty much it.
honestly I still don't really get it.. I tried and failed.
Answer by fafase · May 14, 2015 at 08:00 AM
public class EventClass:MonoBehaviour{
public event Action OnClick = ()=>{};
public void Attack (){
if (Input.GetKeyDown (KeyCode.F)) {
chkinput = true;
}
if (chkinput) {
timeframe += 1 * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.G)) {
print ("Input SucessFG");
OnClick();
timeframe = 0;
chkinput = false;
}
if (timeframe > 1) {
print ("Time Out");
timeframe = 0;
chkinput = false;
}
}
}
}
public class BugMove:MonoBehaviour{
private EventClass eventClass = null;
void Start(){
eventClass = GetComponent<EventClass>(); // If on another object, use GameObject.Find in front
if(eventClass != null){
eventClass.OnClick = this.OnClickDetected;
}
}
void OnDestroy(){
if(eventClass != null){
eventClass.OnClick -= this.OnClickDetected;
eventClass = null;
}
}
private void OnClickDetected(){ Debug.Log("Gotcha!");}
}
So the first class has the checks and calls OnClick(). At that point, the EventClass does not care if it has any listener, it just calls the method, it could be there is no one listening.
BugMove is the listener, and it is done in the Start. It finds the component and register to the event with :
eventClass.OnClick = this.OnClickDetected;
Now, any time OnClick is called, it will also called OnClickDetected. OnDestroy removes the listener but it can be done anywhere.
Think of it like a following on twitter. EventClass is the famous person and you are the BugMove. When you press Follow, you do what is in the Start. From now on, any time the guy tweets, you get a feed of it.
ok this makes sense to me thanks :)
just two quick questions
can EventClass be placed on an object?
in regard to Action in line 3.. what does it refer to? right now it gives me an error
EventClass inherits $$anonymous$$onoBehaviour and is just a basic script. The name may confuse you to think it is some kind of framework class but it might as well be called AdamSandler, but then it loses the fun.
Action, on the other hand, is a framework keyword. Exactly, it is a generic class representing a delegate that returns void and in this case takes no argument. Plenty about it on the net so look it up.
The event keyword turns our delegate into an event. All in all, it pretty much does the same with some restrictions that can be usefull.
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/events
It is all there.
Your answer
Follow this Question
Related Questions
2D noise function to generate voxel circles 1 Answer
C# how to Access UI Assigned to Function 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
my enemy is broken 1 Answer