In Unity when using coroutines or InvokeRepeating, you have to give a string with the name of the function you want to call. Though this is a pain if you change the name of that function, since you have to remember to change the coroutines that use it. Is there a cleaner way of doing this?
Currently it looks like this:
InvokeRepeating ("SendChangedValues", SEND_RATE, SEND_RATE);
though it would be nice to have something like
InvokeRepeating (SendChangedValues.Name(), SEND_RATE, SEND_RATE); //or
InvokeRepeating (functions.GetName(SendChangedValues), SEND_RATE, SEND_RATE);
Is this possible in c#? Or something else that makes sure I get an error/warning when I change the function's name without changing those strings.
Edit 1: The cleanest thing I could think of is making a const string with the function's name, and putting it just before the function itself. So it's harder to forget to change the string, since it's right there above it, and I also only have to change that one const string to change all the coroutines.
Thanks!
What type is a function name in C? A function name or function designator has a function type. When it is used in an expression, except when it is the operand of sizeof or & operator, it is converted from type "function returning type" to type "pointer to a function returning type".
You cannot get the name of the function in C++, but you can print the pointer and later check the binary (if not stripped) for the function name. The signature can be printed exactly as you are doing, just that the type name is not really 'human readable'.
Every function has a name by which it is known to the rest of the program. The name of a function in C can be anything from a single letter to a long word. The ANSI Standard, however, only guarantees that C will be able to distinguish the first 31 letters of identifiers , or function and variable names.
The identifier __PRETTY_FUNCTION__ holds the name of the function pretty printed in a language specific fashion. These names are always the same in a C function, but in a C++ function they may be different. For example, this program: extern "C" { extern int printf (char *, ...
ahhh.. if it were next the C# version, you could have used the nameof operator.
for now, Does this help your cause?
private static string GetFunctionName(Action method)
{
return method.Method.Name;
}
called using:
string methodName = GetFunctionName(SendChangedValues);
you might want to explore different delegate types.. Action, Func etc.
the only problem with above is that for every method signature, you might need to define the right signature/overloads to get name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With