Help with Custom Events
I have three files: FileA stores basic values, from which FileB will modify, but for it to work properly, a value from FileC will have to be imported. FileB also needs to import values, but only from FileA. FileC contains variables set by the end user, basically a dictionary.
My thought process was to make the object for which all of these scripts were created into a container of sorts. FileC would hold the user-set value, then call FileA to gather the default variable information, which would then be sent to FileB for modification. Then I thought to myself, "It'd be nice if I could use some sort of custom event..." So I looked it up and found some information about them, but I have no idea how to actually use it.
So, assuming all else is fine in the scripts, how do I add a custom event for whenever the variable in FileC gets changed?
Answer by OusedGames · Aug 14, 2017 at 11:54 PM
Public delegate void FileCDelegate();
Public static event FileCDelegate OnFileChanged = delegate { };
Void ChangeFileCValues(){ //Inside File C Class
//Change all values
OnFileDelegate();. //This will trigger the event
}
PS: depending on the type of values you are passing you can set then on your delegate definition like...
Public delegate void FileCDelegate(int newValue, float happyFloat);
Public delegate void FileCDelegate(FileCClass cc);
Then call....
OnFileDelegate(15, 36f);
OnFileDelegate(this);
And any subscribed method would receive a call when the values have changed
On Another script Class....
Void Start() {
FileC.OnFileDelegate += MethodThatHandleValueChanges;. //Subscribing to the event
}