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 ?

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!