magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 12, 2017 23:03:25 GMT
I'm probably doing something wrong again, but I found another thread that explained this and I did everything it said to do in there, but I'm still unable to actually receive a custom event. I have a class that inherits from the ICgkEventReceiver interface and it has the following code with some debug statements:
void OnEnable()
{
Debug.Log("OnEnable");
RegisterReceiver();
}
void OnDisable()
{
UnregisterReceiver();
}
#region ICgkEventReceiver methods
public virtual void CheckForIllegalCustomEvents()
{
}
public virtual void ReceiveEvent(string customEventName, Vector3 eventOrigin)
{
Debug.Log("received event " + customEventName);
if (customEventName == "RespawnPlayer")
Debug.Log("RespawnPlayer Event");
else if (customEventName == "StageComplete")
Debug.Log("StageComplete Event");
}
public virtual bool SubscribesToEvent(string customEventName)
{
Debug.Log("Checking for subscription to " + customEventName);
return (customEventName == "RespawnPlayer" || customEventName == "StageComplete");
}
public virtual void RegisterReceiver()
{
Debug.Log("RegisterReceiver");
LevelSettings.AddCustomEventReceiver(this, this.transform);
}
public virtual void UnregisterReceiver()
{
LevelSettings.RemoveCustomEventReceiver(this);
}
I see every debug message I have added except the ones in ReceiveEvent. I have the events defined in the LevelWaveSettings object as well as setups for firing them. I have the one for RespawnPlayer setup in Respawn Cust. Events and I have the StageComplete in a Wave's Wave Completion Custom Event. So I can't figure out why the events are not getting through.
|
|
|
Post by DarkTonic Dev on Aug 13, 2017 5:20:40 GMT
I suggest you take a look at the one I posted here. It works, and does despawn, kill or deal X damage. darktonic.freeforums.net/thread/708/despawn-custom-eventYou should be able to find the part that you did wrong. Let me know what it was. Also, you should see a positive number in the Inspector for that Custom Event "Receivers" before you click "Fire". Do you?
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 13, 2017 14:17:24 GMT
Your example doesn't help me so far. I changed my code to be pretty much identical except for the actual custom event names and it still doesn't get a received event. There is a value of 1 in the Inspector for the Custom Event receivers as there should be. You also seem to have a mistake in your sample code. In the ReceiveEvent function why are you switching on a publicly set field eventReceivedBehavior instead of the function parameter customEventName?
public void ReceiveEvent(string customEventName, Vector3 eventOrigin) { switch (eventReceivedBehavior) {
[EDIT] I see what you're doing with the eventReceivedBehavior, but it seems rather hackish. You're hard-coding the event to be received and anytime the ReceiveEvent comes in then you're just switching on the one selected for in the inspector. Does that mean that the listener will only ever get ReceiveEvent called for the exact event/s that it has registered for?
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 13, 2017 15:35:01 GMT
D'oh, it works now. Well, at least for one of the events. I had put the StageComplete event only on the second level final wave complete and not the first. However, I'm still not getting my RespawnPlayer custom event. Everything is the same and I'm using your sample code. I simply switch to the RespawnPlayer event and it's not firing. I definitely have my player prefab Killable set to fire the custom event in Respawn Cust. Events. If I fire the event manually then it fires correctly and receives it. But when the player prefab automatically respawns based on the Respawn Settings & Events, the event does not fire. I'll keep looking, but this is very odd. I needed the StageComplete one more for the moment anyway so at least I got past that hurdle.
|
|
|
Post by DarkTonic Dev on Aug 13, 2017 18:42:19 GMT
I'm not "hard-coding" the event to be received, and it's nothing hacky. It's assignable in the Inspector from a Dropdown. Your script is hard-coding the event names into your if and return statements. i.e. if you want to change which event(s) you are listening to, you have to change the code in 2 places for each event. I just select a different event in Inspector and done.
My code only handles a single event to listen to. If you want to test with 2 events, add 2 instances of that script, with each listening to different events.
Yes, correct on your question above: the receiver has its ReceiveEvent method called only if it listens to that particular event, which is established through the SubscribesToEvent method. I ignore the event name parameter of ReceiveEvent because I already know what it is since we only listen to one event and the method doesn't get called if it's something else.
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 13, 2017 23:24:41 GMT
I'm not "hard-coding" the event to be received, and it's nothing hacky. It's assignable in the Inspector from a Dropdown. Your script is hard-coding the event names into your if and return statements. i.e. if you want to change which event(s) you are listening to, you have to change the code in 2 places for each event. I just select a different event in Inspector and done. Sorry, I didn't mean to upset you. Hard-coding was a bad choice of words. I meant hard-wiring. I mean, once you've selected which one you want you aren't exactly going to change it so it's fixed at what you set. Yes, I was hard-coding my events, but I wasn't making a generic class to receive any selectable events, but rather building the receiver into my game manager component to specifically listen to the events I want to receive. As I said, I figured out what you were doing. No worries. If I ever figure out why the other event doesn't get fired, I'll let you know.
|
|
|
Post by DarkTonic Dev on Aug 14, 2017 0:15:51 GMT
No need to apologize, I took it as you didn't understand what I was doing with the code, so I was clarifying. Not particularly upset, but in general I don't hard-code things. I make them general purpose even if I'm only going to use it for one purpose myself. It's better for maintenance purposes.
And non-DRY (don't repeat yourself) code is one of my biggest pet peeves when I do code reviews at my day job. Such as defining the same "magic string" in 2 places. At the minimum, that should just be a constant so you have a single place to change it if you need to later. Better still, if you have 2 events in the same class, you should still use a public string with that attribute I used so you can select it from the Dropdown in the Inspector, for each. No need to go hunting down code when you can do everything in the IDE.
I told you how you can get that to fire, by using my class twice. You could gut the switch statement out of mine and the variables that are used there and replace it with your own code.
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 14, 2017 0:26:30 GMT
No worries. My example code was just thrown together to get it working. I wouldn't keep it that way for final production code.
As for the event firing, I don't follow. I already tried with your example and it wasn't firing the event. The issue isn't the receiver, it's whatever is supposed to fire the event. If I click on the Fire button at runtime then my receiver gets the event, but for some reason, when my Killable respawns, it doesn't fire the Respawn event even though I have that set up in the Respawn Cust. Events section.
|
|
|
Post by DarkTonic Dev on Aug 14, 2017 0:34:11 GMT
You can verify if it's getting fired or not by turning on "Log Messages" at the top of the LevelWaveSettings game object Inspector.
It will log things like "Firing Custom Event: my event" to the Console. Even when you click the "Fire!" button. If it's not logging from the respawn then it's not firing.
But I just did a quick test in example scene #1 put adding a Custom Event to fire on respawning the player and it does fire the custom event.
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 14, 2017 0:43:06 GMT
I turned on logging from LevelWaveSettings as well as the object that is supposed to fire the event. I don't see the event firing. If I manually click Fire then I see the following log message: Firing Custom Event: RespawnPlayer UnityEngine.Debug:Log(Object) DarkTonic.CoreGameKit.LevelSettings:FireCustomEvent(String, Transform, Boolean) (at Assets/Plugins/DarkTonic/CoreGameKit/Scripts/Level/LevelSettings.cs:1925) LevelSettingsInspector:OnInspectorGUI() (at Assets/Plugins/Editor/DarkTonic/CoreGameKit/Level/LevelSettingsInspector.cs:1915) UnityEditor.DockArea:OnGUI() And here is where I have it set up to fire the custom event on my player object:
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 14, 2017 0:56:35 GMT
OK, I found the problem. In Killable.cs it has the following code:
if (respawnDelay.Value <= 0f) {
if (respawnFireEvents) {
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < respawnCustomEvents.Count; i++) {
var anEvent = respawnCustomEvents[i].CustomEventName;
LevelSettings.FireCustomEventIfValid(anEvent, Trans);
}
}
So, it only fires the custom events if the respawnDelay is <= 0. I have my delay set to 5. So, I have to ask then why it only lets the custom event fire if the delay is <= 0. As soon as I set my delay to 0 then it fires the event.
[EDIT] Or is there some other place that it tries firing the event once the respawn delay is over? I'm searching for that now.
|
|
|
Post by DarkTonic Dev on Aug 14, 2017 3:47:48 GMT
Yep, you found a bug! Happens about once a year, I'm usually pretty good about those with Core GameKit. Thanks for figuring it out! It is checking to make sure respawn timer is zero because if it's > 0 we will need to wait until it respawns to fire the events.
I've fixed this now and the next version will have the fix. If you need the beta without waiting for that (maybe a week) send us an email at support@darktonic.com with your Asset Store invoice PDF.
|
|
magique
Full Member
Posts: 194
Posts: 194
|
Post by magique on Aug 14, 2017 5:33:43 GMT
Thanks. No rush. I can wait for the official release.
|
|
|
Post by DarkTonic Dev on Aug 14, 2017 7:20:29 GMT
Sounds good
|
|