Hello, fellow developers. I’m making a dialogue system where after each line something may happen in game. For example, an animation or a sound may play, an event may get triggered, a variable may get set, etc.
I thought it would be a good idea to store the dialogue data in nested scriptableobjects because they are designed to store data and because they support polymorphism.
I implemented this system quite easily but then I came across a problem: there is no way to directly reference a gameobject instance from a scriptableobject to say play an animation. The first solution that came to my mind is to have a list of all gameobjects that need to be manipulated with in the gameobject that reads the dialogue scriptableobject and to have indices or ids of those objects stored in the scriptableobject but it would be a PITA to edit such dialogues.
I could also store the dialogue data in a monobehavior script on a gameobject but it just feels wrong to me because monobehaviors are for behavior and scriptableobjects are for data. Also I would have to struggle without polymorphism.
Do you have any thoughts on how to structure my system? Any help would be appreciated.
The most complex solution is what Unity’s doing with Timeline, with ExposedReference.
Alternatively, you could just store the name of the object, and then at runtime match the named objects with the animators. That’s probably easier and more flexible.
Thanks for the reply. I’ve read about ExposedReferences and as far as i understand, I’m going to need to have a MonoBehaviuor script that implements IExposedPropertyTable and has some kind of a dictionary to store the objects and their ids on the GameObject that serves the dialogue data to other GameObjects. Am I correct?
Yeah.
Which sounds clunky, tbh. Rolling your own system for doing the same thing is probably easier.
This is going to be controversial, but I use GameObject.Find() for this sort of thing.
People will tell you it’s slow, but I’ve never had a noticeable problem. It’s not like you’re going to be using it 1000 times per frame. Also, in Dialog, performance is not super critical.
THe only other problem is that string matching is not enforced by the compiler, but that’s not unmanageable.
Edit: I guess you could make this more type safe, by replacing the name strings with components, then you could use FindObjectOfType(). Then I guess you’d have to make hundreds of uniquely-named components that don’t do actually do anything?
. This solution might “scale poorly,” but whether that’s important or not all depends on how big your project is.
Ok guys, thanks for your help. Sorry for the big delays, didn’t have time to reply.