- Home /
Suspension function
So basicaly I was wondering if there's a way to write a function suspending the execution of another funtion, for example
void Foo()
{
// do something
}
Suspend(Foo,5); //Foo() will be executed after 5 seconds
So something like the following...except it doesn't work :)
public class SuspendScript<T> : Monobehaviour
{
public delegate void MyDelegate(T parameter);
public MyDelegate functionToSuspend;
public T parameter; // the parameter of the suspended function
public void Suspend(float time)
{
StartCoroutine(SuspensionCoroutine(time));
}
IEnumerable SuspensionCoroutine(float time)
{
yield return new WaitForSeconds(time);
functionToSuspend(parameter);// execute suspended function after *time* seconds
}
}
and now the calling function
public class SomeClass : MonoBehaviour
{
SuspendScript<string> SS;
void JustForGigs(string s)
{
Debug.Log(s);
}
void Start()
{
SS=new SuspendScript<string>();
SS.functionToSuspend=JustForGigs;
SS.parameter="Haha!";
SS.Suspend(5); //JustForGigs will be executed after 5 seconds
}
Expected outcome: Debug.Log("Haha!")
Actual Outcome: NullReferenceException :(
yeah so my question is if it's even possible to suspend a function like this? And if it is indeed possible then what am I doing wrong? Is there a better way?
One thing I notice is that you are using a $$anonymous$$onoBehaviour, but you can't instantiate $$anonymous$$onoBehaviours with "new", you have to use AddComponent(). It should be throwing you an error for that as well.
Have you tried deriving your SuspendScript class from ScriptableObject ins$$anonymous$$d?
Answer by InsertFunnyUsernameHere · May 15, 2015 at 06:17 PM
Yeah so FortisVentalier gave me the answer I needed and I managed to create a working suspension script, check it out
public class SuspensionScript<T>
{
public delegate void MyDelegate(T value);
public MyDelegate function; //the function to suspend
public T param; // the parameter of the function to suspend
public IEnumerator SuspensionCoroutine(float time)
{
Debug.Log ("MyCoroutine");
yield return new WaitForSeconds (time);
function (param);
yield return null;
}
}
caller function: (you may use any datatype instead of string, just put it in the <> )
public class SomeClass : MonoBehavior
{
SuspensionScript<string> SS;
void JustForGigs(string s)
{
Debug.Log(s);
}
void Start()
{
SS = new SuspensionScript<string>();
SS.function = JustForGigs;
SS.param="Haha!";
StartCoroutine(SS.SuspensionCoroutine(5)); //JustForGigs will be executed after 5 seconds
}
}
Have fun making Time Bombs and stuff
Answer by cjdev · May 15, 2015 at 07:45 PM
Another way of doing it could be using a simple function in the same class, just depends on your needs:
using UnityEngine;
using UnityEditor.VersionControl;
using System.Collections;
using System;
public class Coroutines : MonoBehaviour {
void Start () {
StartCoroutine(Suspend((s) => JustForGigs("Haha!"), 5f));
}
public void JustForGigs(string s)
{
Debug.Log(s);
}
public IEnumerator Suspend(Action<Message> d, float time)
{
yield return new WaitForSeconds(time);
d.Invoke(new Message());
}
}
The Action delegate is in the form of (random parameter variables) => FunctionName(actual parameters).
whoa that's too smart for me. I actualy tried to make it fit into one script but I just couldn't no matter how I tried. Gonnah learn lambda functions now, ty