Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Designing an Events system

Discussion in 'Scripting' started by DropboxKenshiro, Oct 20, 2017.

  1. DropboxKenshiro

    DropboxKenshiro

    Joined:
    Jan 19, 2017
    Posts:
    5
    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.
     
  2. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
  3. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    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.
     
  4. DropboxKenshiro

    DropboxKenshiro

    Joined:
    Jan 19, 2017
    Posts:
    5
    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:
    Code (CSharp):
    1. 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.
     
  5. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    Do you added System.Serializable attribute to Mission class?

    But you can achieve the same goal using ScriptableObjects.

    Thanks.
     
  6. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    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.
     
  7. DropboxKenshiro

    DropboxKenshiro

    Joined:
    Jan 19, 2017
    Posts:
    5
    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.
     
  8. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    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.
     
  9. DropboxKenshiro

    DropboxKenshiro

    Joined:
    Jan 19, 2017
    Posts:
    5
    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:
    Code (CSharp):
    1.  
    2. public class Event:ScriptableObject
    3. {
    4.  
    5.     public string eventid;
    6.     public string text;
    7.     public bool isRandom;
    8.     public Sprite picture;
    9.     public Foo[] bar; //extra class, containing enum definition, two enums and integer
    10. }
    11.  
    12. //and then interhited objects use only basic types
    13.  
    14. public class MissionExecEvent : Event
    15. {
    16.   public Mission mission;
    17. }
    18.  
    19. //mission contains only basic types
    20.  
     
  10. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    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.
     
  11. DropboxKenshiro

    DropboxKenshiro

    Joined:
    Jan 19, 2017
    Posts:
    5
    Thanks for your help. I'm total beginner, so any help is great. :)
     
    hasanbayat likes this.