Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Dialogue system structure

Discussion in 'Scripting' started by aElSi2, Mar 14, 2021.

  1. aElSi2

    aElSi2

    Joined:
    Feb 20, 2019
    Posts:
    12
    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.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,196
    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.
     
  3. aElSi2

    aElSi2

    Joined:
    Feb 20, 2019
    Posts:
    12
    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?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,196
    Yeah.

    Which sounds clunky, tbh. Rolling your own system for doing the same thing is probably easier.
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    This is going to be controversial, but I use GameObject.Find() for this sort of thing.

    https://docs.unity3d.com/ScriptReference/GameObject.Find.html

    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?:confused:. This solution might "scale poorly," but whether that's important or not all depends on how big your project is.
     
    Last edited: Mar 16, 2021
  6. aElSi2

    aElSi2

    Joined:
    Feb 20, 2019
    Posts:
    12
    Ok guys, thanks for your help. Sorry for the big delays, didn't have time to reply.