- Home /
Finding all public functions on a game object
I'm trying to write a script for dialogue, and I want to it be able to check against public variables/functions, akin to how Unity Animations can find any public functions on an object. I was just curious if there's any known way to achieve this? Or whether it would be shorter to manually code for each possibility.
So that for the sake of checking against various outcomes, such as how many coins a player has, rather than having to code in the script for each and every possible value in each and every possible script I would want to check against. I could simply drag in an object, choose the public function on it I wish to check against, and enter a $$anonymous$$imum requirement based on the returned value type.
Sounds like you are trying to use the object itself as some kind of organizational data structure. You should use constructs that are made for holding/looking up data, like List and Dictionary.
This is exactly what the strategy pattern is all about; choosing the correct behavior (during runtime) for a given situation, then applying that behavior to the situation.
All you need is an interface that you implement in all classes you need the behavior on. Then use this function to get access to it and call your function.
The more I read your above comment, the more it sounds like you want behavior, but you don't want to code it. That's an impossible equation.
I don't believe the system I'm looking for is impossible. Like I said, it's the exact system implemented in a Unity animation, where the object upon which the animation is done, can have all its public functions accessed through the editor, by the animation itself. I just wanted to know if such functionality for an editor could be readily replicated.
Answer by Jamora · Sep 06, 2013 at 11:12 AM
If your system is not terribly complex, maybe a text box with a few different strings shown, it might be best to manually code it all.
If your system is complex: different behaviors for texts, restrictions or other rules that apply. Manually coding that all doesn't sound like a very good design decision. You should take a look into polymorphism and the strategy pattern.
If you still feel a need to access individual functions from classes, you can use reflection to gather all kinds of information about classes, including fields and functions. And even then, you need to somehow (Attributes)apply context information to these fields and functions to allow the computer to choose the correct one. After all, to the computer, it's all 1s and 0s. Reflection is slow and might not work on all platforms.
after looking further into reflection (although not fully understanding it) this does generally appear to achieve what I want it to do. I can access any members or fields of a type purely by the name of that member/field. The only problem being that this does not allow me to change the values of members as far as I can tell, meaning I am unable to access inherited variables.