Event System + Scriptable Objects?

Hello,

I’m trying to make a system for dialogue choices in my game. I would like for a player to be able to select a dialogue choice, and to potentially activate a method based on that choice (i.e. talking to a shop keep and one choice would let you ask for a quest, while the other takes you to the shop).

Here is a really basic event system holder I’m using:

using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class MethodAfterChoice : MonoBehaviour
{
    //drag the scripts in here in inspector
    public UnityEvent choiceEffectEvent;
}

So If I’m attaching this script to the NPC character I can drag in the objects that I want just fine and it works if there’s only one method I want to activate. But I want there to be the option to make several different choices and I don’t think there’s a way to selectively Invoke one of the methods in an event.

When adding this class to a dialogue scriptable object it doesn’t work the same way (I think because it’s not using Monobehavior anymore?)

using UnityEngine;

[System.Serializable]
public struct Choice
{
    [TextArea(2, 5)]
    public string text;
    public Conversation conversation; //what conversation the choice should trigger
    public MethodAfterChoice methodAfterChoice; //the method that the choice should trigger
    public int choiceIndex;
}

[CreateAssetMenu(fileName = "New Choice", menuName = "Dialogue/Choice")]
public class ConversationChoice : ScriptableObject
{
    [TextArea(2, 5)]
    public string text;
    public Character speaker;
    public Choice[] choices;
}

I can no longer drag in the objects that worked when I had the MethodAfterChoice placed directly on the NPC, so I can’t add the functions that I want in my dialogue system. This is the same when trying to make MethodAfterChoice a scriptable object and I’m not sure how to get the effect that I want.

If you see a general way to fix this or have a workaround that you know of I’d really appreciate it. I’ve been stuck on this for a few days already.

I didn’t look into your script for details, but my Datasacks module does this in a completely problem-neutral way. You’re welcome to look through it and see some of the stuff it does and how it works. I use it for 100% of my personal projects, mostly for the UI interfacing because I really hate dragging specific parts of scenes into custom scripts: that way of working is SO error prone.

Datasacks uses ScriptableObjects as data containers and anyone can subscribe to them for events related to them changing. It’s intended for interfacing with UI components or just general inter-program loosely connected communication.

Here’s sort of a system diagram:

Datasacks is presently hosted at these locations:

https://bitbucket.org/kurtdekker/datasacks

This is great Thank You For Sharing This! =D

I’ve recently started using Unity Atoms which uses a scriptable object based architecture. It’s proved amazingly flexible for me so far with its GlobalVariables and Events.

1 Like

Do you have a link to it???

Thanks! =)

https://adamramberg.github.io/unity-atoms/

The docs aren’t the best in the world but the basics are easy enough to grasp.
He does have a discord that he checks regularly though to answer questions and offer help.

1 Like

You My Friend Rock!
This Is Very Cool…

Thank You For This!

Thank the guy that created it :stuck_out_tongue: I was just gutted I dropped 60 quid on SODA before realising this is practically the same thing but for free.

Wow…sorry to hear that… ="(
Well,
For what it’s worth thank you I really appreciate you sharing your knowledge of this stuff… =D

1 Like

You’re very welcome! I still don’t understand the full intricacies of Atoms yet myself, partly due to out-of-date docs. But I have achieved some good, and extremely modular, systems so far already with it. It has really streamlined and sped up my current project. I hope you have success with it also; good luck!

Thank you very much my friend!!!

Good Luck On Your Journeys As Well! =)