Shaderop,
That’s actually a pretty cool idea, I hadn’t thought of that! In my case the use of reflection still looks (I think? read below…) like the best way to achieve the result that’s needed for this project, however your suggestion above regarding the use of an Interface actually answers some lingering questions in my mind. For one, I had seen people use Reflection to create extraordinarily generic enemies and items, which seemed great - however when I learned it’s also not optimal, I kept wondering what could have been done to make those enemies and objects better, as generic enemies / objects do seem like a great way to implement very flexible setups.
Actually in my case, the primary reason I used Reflection, was because I had integrated a main DialogScript object class, which is the master class for all parent Dialogs. So in my hierarchy, it looks a little like this:
OkayDialog
LoginDialog
SettingsDialog
Etc., and each of those is a child of DialogScript. I had integrated singletons for all of those also, and needed a quick way to access each Singleton. Rather than putting a singleton on each Child Class, I had wanted it in the DialogScript class, which resulted in a class definition like the following:
public class DialogScript<T> : MonoBehaviour
At that point, it seemed like using reflection to access the methods was absolutely required, otherwise there was no way to access the objects due to the generic. So as a result, to pass a method into one of the singletons, I had to create a function that would get the singleton, determine its class, get the method, then invoke into it. The process then worked like this:
// This sends the request to call a method on an object
object[] vars = new object[]{InputTagManager.inputTags.None};
invokeDialogMethod(thisPopup, "TransitionDialogIn", vars);
// Then the invokeDialogMethod does the work...
private static void invokeDialogMethod(MainBoomUI.uiDialogs thisDialog, string methodString, object[] inObj)
{
if(thisDialog == MainBoomUI.uiDialogs.None)
return;
try
{
var newDialogObj = new object();
// getDialogScriptForDialog is just a switch/case statement that sends back the proper Singleton for the thisDialog object pushed in
getDialogScriptForDialog<System.Type>(thisDialog, out newDialogObj);
Type type = newDialogObj.GetType();
var method = type.GetMethod(methodString);
method.Invoke(newDialogObj, inObj);
}
catch
{
Debug.LogError("invokeDialogMethod failed when performing the string " + methodString + " on dialog " + thisDialog + " with the variable " + inObj[0] + ". This is completely fatal. Best call the digital coroner, IE, Dave.");
}
}
Again, I add that I’m somewhat new to using Reflection, so I’m open to any suggestions I can get, even if I’m using Reflection entirely wrong. (It’s working well for this project, but I’d love to learn better uses of it for future projects).