i want to start coding my event system but without really knowing anything about it i have a couple of problems with most tutorials
first some of them are explaining with an specific scenario that is hard for me to abstract from
other tutorials put all the code in one big script so i dont know what parts to put where in my project
so first what i want to do:
having a main script for interaction
raycast
busy state
sending events
…
basically the main functions that are used for all interactions
and then specific scripts for each object to interact with
things like animating rotating …
reacting to events
at first i used the Physics.Raycaster from unitys dll at the camera and interfaces like IPointerEnterHandler
at the specific script and could use
public void OnPointerEnter(){ do whatever }
without the need to add that script as a listener or care about any other part of the event system
but as i need my own events none the less i thought replacing that event by my own would be a good place to start
so what parts of all that event stuff need to go in the main script and what in the interface?
and one last question - i need to access the busy state ( bool) from the main script within the specific scripts
im aware of 2 possibilities to acces that
public variable in specfic script and drag and dropping the main script in there
using specific name/tag and getting it with find/findwithTag
is there a 3. way ? something like a using directive so i can just use MainScript.GetBusy without the need of any other variable or something similar?
i guess the problem is that ii cant acces the specifc busy variable of exact that script with a using directive?
put the delegate/event as statics in an EventHandler script, in this script you receive conditions to fire the events.
do subscribing, unsubscribing and sending conditions to fire events in Interfaces.
hmm im not really sure how to start
i think i need more explanation on a more basic level
so i have one script TestEventHandler
using UnityEngine;
using System.Collections;
public class TestEventHandler : MonoBehaviour {
public delegate void TestEventHandler();
public static event TestEventHandler OnTestTrigger;
public void OnTest()
{
if (OnTestTrigger != null)
OnTestTrigger();
}
}
ok then an interface
public interface ITestEventHandler {
void OnTest();
}
(un)subscribiing would be done in OnEnable / OnDisable
as i cant put definitions in the interface how do i do (un)subscribing in there?
and for all following events i would have to create both of these scripts what would make quite a lot of scripts - so i guess i shoould rename TestEventHandler to just EventHandler and put all Events inside and juste create a new Interface for each event? or willl there be any problems i dont see?
you would make a class that inherits the Interface, thus this class should implement the interface functions and properties. So you would call OnTest() in the class instance when you fire the event.
public class TestClass: Monobehaviour, ITestEventHandler
{
void OnEnable()
{
TestEventHandler.OnTestTrigger += OnTest;
}
void OnDisable()
{
TestEventHandler.OnTestTrigger -= OnTest;
}
public void OnTest()
{
Debug.Log("Testing event");
}
}
the class doesn’t HAVE TO inherit from monobehaviour, you can also do this with serialized classes.
hmm yes the obvious way - for a real event the test class would be replaced with whatever class i want to listen to that event right?
i think im stuck with the way the “built in” events work but i guess that would be quite complex to rebuild?
i.e. for the OnPointerEnter event all i have to do in the listening class is inherit the interface and implement the OnPointerEnter function - no caring about the listening itself
so is there an easy way to achieve that or is it better to just add the Enable/Disable part to any class i want to listen to events?
Edit:
ok so it sort of works but i got another problem
the script listening to the event is attached to multiple gameobjects and only one is reacting and its not the right one so i need to pass arguments when sending the event
in this case at least the object that triggered the event so i can check in the listening script if its the same object as the one the script is on and only react if thats the case
so big question now how do i do that?
you could do the subscription in a constructor and let the eventhandler be a factory that instantiates an instance of the class with the interface you need.
if you want to rebuild the unity eventsystem, then you can study how it’s build by accessing it’s assembly. you can do this in monodevelop by typing IPointerEnterHandler in your script, right click on it and choose go to declaration.
rebiulding isnt necessary and beyond my knowledge (i already tried to understand it^^)
i just like the way its working - once you have all events you need its really easy and almost no work to use them in other scripts
maybe someday i try to get there but its really low priority
first thing is getting everything to work
so as i wrote in the edit of my last post next thing i need is arguments
i guess for you it is explaining some very basic things - so if you have a link or something to a good tutorial that explains it as modular as possible that would be great too!
Edit: ok that argument part was much easier than i thought - it works!
thank you very much!
i think with one working event every following event willl be no problem