Designing an Events system

Hi there
I’m making a game in Unity for a while, but I came across serious problem.
I created my event and mission system (event means, some thing in game, not UnityEvent), but I wanted to improve it. However, I messed something and now I’m lost.
I wanted to combine data with functions in missions (for example, data will be time to do it, and function will be initialization). But MonoBehaviour caused that I can’t modify object in inspector.
How can i do it?
This project is important for me, so I need help now.

Hi.

ScriptableObject is your friend, read the below links to get inspired:

You can reference ScriptableObjects with MonoBehaviours and they will act as a Data.

Hope this helps.
Thanks.

Explain the problem in more detail and leave the unneccesary parts away.
Then surely we can help.

What does that mean?
Why cant you modify an object because of MonoBehaviour? Normally ScriptableObject and MonoBehaviour are edittable in the inspector.

Sorry for not being clear.

So, in my idea, mission is a normal object, but derived from MonoBehaviour, because of use of GetComponent etc. for initialization (getting a initial number of buildings, for example).
However, I want to create missions simply by making an array in script like this:

Missions[] missions;

and modify it by using just Inspector, like serializable objects. However, I just have a field with that circle, and the object window shows nothing.
I don’t have any problems with creating classes, but I want a simple solution.

Do you added System.Serializable attribute to Mission class?

But you can achieve the same goal using ScriptableObjects.

Thanks.

You can achive this by using ScriptableObjects, but you also would likely have to write your own custom inspector.
Doing that is really easy, you’ll find a lot about that in the documentation.

Plot twist
I tried to use ScriptableObjects at one point, but I couldn’t use GetComponent in their.
I’ll know the idea.
Also, I’ll have some types of tasks. Making custom inspector for each one would be painful, but maybe there is another way.

There is no need to make custom inspectors, but even if you need the custom inspector, you can make an inspector for a base type and implement that base type to use the custom inspector.

What’s is included in your Mission class? is there any special data the unity can’t display in the inspector?
Why do you need to use GetComponent?

Please provide more info, for example, post the mission class script.

Thanks.

I need to use GetComponent to get data from other scripts. For example, for counting number of buildings by creating list by GetComponents and getting List.Count(maybe there is better methods, but it’s not the subject we’re talking about). Or to acess another script that gives me data about number of citizens.

I’m a Polish user, and I made a huge mistake by giving Polish names. However, I’ll try to recreate script as close as I can.
So, there’s an event that puts an mission into play. It’s like that:

public class Event:ScriptableObject
{
  
    public string eventid;
    public string text;
    public bool isRandom;
    public Sprite picture;
    public Foo[] bar; //extra class, containing enum definition, two enums and integer
}

//and then interhited objects use only basic types

public class MissionExecEvent : Event
{
  public Mission mission;
}

//mission contains only basic types

So, as you stated, you can use ScriptableObjects without any problem.

You need an EventManager that handles events and pushes them to missions.
Also, you need to keep a reference to the active mission to handle events.
Also, you can use GetComponent on MonoBehaviours, so if you pass a MonoBehaviour to ScriptableObject you can handle this.

And in the end, you need a MissionManager that handles global events on missions.

You need to read about Observer Pattern:

Hope this helps.
Thanks.

Thanks for your help. I’m total beginner, so any help is great. :slight_smile:

1 Like