- Home /
How Do I pass an unknown class to a function?
EDIT: SOLVED: Using delegates is way better than attempting to do what I was doing. See this video: http://www.youtube.com/watch?v=CtIX1ei1I9Y
Howdy!
I'm attempting to create a stopwatch class that will allow me to yield for real world seconds, as opposed to Time.timeScale seconds--this is a problem because I'll yield Waitforseconds in the game for 3 seconds, and then almost immediately for whatever reason the player might want to use a bullet-time function to slow down time. But the thing that was going to happen in three seconds will now no longer actually happen in 3 seconds--it will take hours in real time. So...long story short, I need to be able to pass some information to a yielding function that will then call a function from a string on the calling class. Most of that I've figured out--but there's a slight problem calling the function that I'm not understanding. Here are the two classes I'm attempting to use. The error is: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
public class Howdy : MonoBehaviour {
public SomewhereElse _somewhereElse;
void Start() {
_somewhereElse.CallSayHi<this.GetType()>("SayHi");
}
public void SayHi() {
print("Hi!");
}
}
public class SomewhereElse : MonoBehaviour {
public void CallSayHi<T> (string functionName) {
MethodInfo mInfo = typeof(T).GetMethod(functionName);
mInfo.Invoke(T, null);
}
}
Thanks for your time...
Simon
Seems like it would be a lot easier just to make your own WaitForSeconds coroutine that takes timeScale into account.
Yes, that's the goal, and I had been just multiplying the waitforseconds value by the timescale amount to get the right time. But this only works if the WaitForSeconds is meant to start and stop within the newly adjusted timescale. If I have an always changing timescale, this requires something a bit more precise...non?
Thanks for the quick response, Eric!
Yes, as I said, make your own WaitForSeconds function, and use that ins$$anonymous$$d of WaitForSeconds...make a loop in the function that either takes timeScale into account every iteration, or uses Time.realtimeSinceStartup.
Ok, yup! Cool, that part I have indeed figured out already...that's no problem at all. What I'd like, though, and I think what this question is really asking, is how to cause that custom waitforseconds function to call a function from any calling class based on parameters, when it's finished yielding. Something that works similarly to the "oncomplete" hash table variable in iTween, if you catch my drift?
Thanks again!
Okay, I'll have a look into delegates, good idea. Do you prefer to use delegates over reflection in this situation, though? Victorino seems to be proposing a solution that uses reflection.
Answer by Eric5h5 · Aug 24, 2012 at 04:51 AM
Do you need that? If the goal is just to wait real-world seconds, all you have to do is yield on that custom coroutine the same way you would do WaitForSeconds. If you need to do other things when the coroutine is finished for some other unspecified reasons, then I'd recommend looking into delegates.
Answer by lvictorino · Aug 24, 2012 at 04:50 AM
Generics are made for that. It allows you to use an undetermine type in a methode or a class. This is how GameObject.GetComponent method works actually.
Try this:
void CallSomethingElse <T>(string functionName)
{
// then do whatever you want to do using T instead of callingclass
}
then you can call it this way:
myCallSomethingElseInstance.CallSomethingElse<my_undetermined_class>( my_function_name );
You can read more about generics here : http://msdn.microsoft.com/library/512aeb7t(v=vs.80).aspx
Hope it helps.
Ah! I think this is what I'm interested in. Now I think I just have a syntax problem. I'll add it to the question.
Now that I see what you want to do... I think the solution is what Eric5h5 is proposing. Start a coroutine then raise a deleguate once its finished. I guess @Eric5h5 deserves the "right answer".
Wait, so...it's impossible to send an undeter$$anonymous$$ed class reference to a function? The above code will never work?
Of course it will, but for what you intend to do this is not the best solution imho. I'll post you (later) the solution for the code above if you want. But in the same time I suggest you have a look at delegates :).
Ok, I will have a look at some of these fancy delegate things you keep mentioning. I would be very interested to know how your previous solution might work, though. Cheers!
Your answer
Follow this Question
Related Questions
How to delay a function (how to use WaitForSeconds() and yield) 1 Answer
WaitForSeconds problem with Unity Pro 3 Answers
Wait For Seconds to Load level C# 2 Answers
Yield refuses to work. Js 2 Answers
waitforseconds not working 2 Answers