Change undetermined properties at runtime
Hello, I'm trying to create a graph system where some nodes can affect the values of objects, or call methods. My problem is is that these values / methods can be (almost) anything and may only be known at runtime. I though of using either reflection or serialized properties, but reflection is usually a bad practice (especially at runtime) (and it's pretty hard to find info about this for unity since almost all results to my searches are related to rendering) and as I understand it serialized properties are mainly meant for editor work, so I'm not sure it could (nor should) be used for runtime stuff.
Any idea ?
Answer by Alanisaac · Jan 06, 2018 at 04:51 PM
I wouldn't say that using reflection is usually a bad practice. Like anything else in the language, it's a certain type of tool that can be used to solve problems -- whether it's good or not has more to do with whether it's the right tool for the given problem.
I don't know the specifics of your use case, but if I were evaluating whether or not to use reflection, here are two things I'd consider (and I'm assuming c# as your language, but the considerations apply regardless):
Performance -- How frequently do you expect the arbitrary calls to happen? Will they happen in response to user action? In a tight loop? If you're executing them very frequently, you may need to find a way to mitigate the fact that reflection tends to be slower than simple property access or method calls. There are libraries out there that can help, which typically cache the reflected member into a delegate. Marc Gravell's FastMember is a good example of one such library that could help. Similarly, take a look at using the dynamic keyword and the DLR instead of statically typed objects. I lump it in with reflection because dynamic
uses reflection under the hood, but does the caching for you, and may be faster.
Maintainability -- Consider what would happen if tomorrow, the name of every method and member you're reflecting over changed slightly. Would these changes cause major disruptions your application, or will you have a way to handle this? Also: would you be reflecting over private/hidden members of a 3rd-party library (like Unity itself) that is out of your control? What happens when that changes?
Finally, are you sure that the actions a node will take are entirely arbitrary? Can you represent the actions themselves with an object model, such as a command pattern? If so, maybe there's a way what you're doing can be statically typed. A code example of what you're trying to do would be helpful if you have one!
I can't post a real code example of what I'm working on since i'm still trying to figure out the best way to do it. But one problem is that i'm trying to do a generic graph system, so I can't predeter$$anonymous$$e whether the "actions" of the graph will occur often or not. And one of my goal is to keep it easy to use, so I find the command pattern pretty bothering if for every action I have to write a new command and split my class into hundreds of commands.
How do you access/reference the Fast$$anonymous$$ember in Unity? I'm using Rider IDE and can't find a way to use it :( I checked the packages.config and it's already referenced there but isn't having any effect. Tried adding a in PackageReferences still nothing and Unity removes it on refresh anyway. TargetFramework = 4.7.1.
Edit: Found the solution: https://docs.microsoft.com/en-us/visualstudio/cross-platform/unity-scripting-upgrade?view=vs-2019#taking-advantage-of-net-compatibility
Your answer
Follow this Question
Related Questions
Delegate.CreateDelegate fail to call correct overloaded function.. 0 Answers
How to reflect raycast rays of objects 2 Answers
Reflection probe bake visual bug 0 Answers
Planar Reflection Probe problem 1 Answer
Reflection NullReferenceException: Object reference not set to an instance of an object 2 Answers