Help with Events pls.

JohnG

Member
Hi,

I am struggling with your event documentation. I have got events working with UCC (OnImpact), but cant figure out if you do things differently in UIS, and not even sure if this is still something that needs to be done through UCC events.

I want an event that fires when a item is equiped, can you please provide complete example as I am completely stuck. This is what I have and nothing is working.

C#:
    private void Awake()
    {
        
        EventHandler.RegisterEvent<Item , int>(gameObject, "Equipper_OnChange", Equipper_OnChange);       
        EventHandler.RegisterEvent<Item , int>(gameObject, "OnInventoryEquipItem", OnInventoryEquipItem);       
    }
    

    private void OnInventoryEquipItem(Item itemIdentifier, int slotID)
    {
        Debug.Log("The inventory used test " + itemIdentifier + ", " + slotID + " slot#.");
    }
    
    private void Equipper_OnChange(Item itemIdentifier, int slotID)
    {
        Debug.Log("The inventory used " + itemIdentifier + ", " + slotID + " slot#.");
    }

I dont get any debugs so I assume something is not working.

The above code is attached to a gameobject in the scene, so not sure if that's correct.
 
1610956798178.png
The first parameter when registering an event is the "target" of that event. Depending on the event that could be anything, but most of the times it is the player gameobject or the relevant component.

Then the second parameter is the event name.
In this case you are trying with "Equipper_OnChange" and "OnInventoryEquipItem"

Equipper_OnChange is an event used on the Equipper component, which in the case of the UCC+UIS integration isn't used at all since the UCC/UIS bridge takes care of equipping items the UCC way. So you can remove that

Then OnInventoryEquipItem is the even you should be using. You need to be careful that the types between the <> match exactly the types where the event was executed.

An easy way to do this is use your IDE, press Ctrl+Shift+F to search the entire project and paste the event Name (including the quotation marks). This will find everywhere in the code where the event is being registered and executed. This is a good way to know what the target is and what the parameter types are. In this case you are correct it is <Item, int> (UCC Item type not UIS Item).

Note that for Equipper_OnChange you were wrong, it does not have any parameter types so there shouldn't have been an "<item,int>".

So long story short this should help:
Code:
EventHandler.RegisterEvent<Item, int>(m_Character, "OnInventoryEquipItem", OnEquipItem);
EventHandler.RegisterEvent<Item, int>(m_Character, "OnInventoryUnequipItem", OnUnequipItem);
 
View attachment 4860
The first parameter when registering an event is the "target" of that event. Depending on the event that could be anything, but most of the times it is the player gameobject or the relevant component.

Then the second parameter is the event name.
In this case you are trying with "Equipper_OnChange" and "OnInventoryEquipItem"

Equipper_OnChange is an event used on the Equipper component, which in the case of the UCC+UIS integration isn't used at all since the UCC/UIS bridge takes care of equipping items the UCC way. So you can remove that

Then OnInventoryEquipItem is the even you should be using. You need to be careful that the types between the <> match exactly the types where the event was executed.

An easy way to do this is use your IDE, press Ctrl+Shift+F to search the entire project and paste the event Name (including the quotation marks). This will find everywhere in the code where the event is being registered and executed. This is a good way to know what the target is and what the parameter types are. In this case you are correct it is <Item, int> (UCC Item type not UIS Item).

Note that for Equipper_OnChange you were wrong, it does not have any parameter types so there shouldn't have been an "<item,int>".

So long story short this should help:
Code:
EventHandler.RegisterEvent<Item, int>(m_Character, "OnInventoryEquipItem", OnEquipItem);
EventHandler.RegisterEvent<Item, int>(m_Character, "OnInventoryUnequipItem", OnUnequipItem);
Thanks that helps me understand. I did however get the question partially wrong because I copied the wrong code example here. I was going to ask about EventNames.c_Equipper_OnChange, but just realized this morning that I dont use the c_Equipper_OnChange part from eventnames.cs, but the part you have in quotes "Equipper_OnChange".

I also notice that some of them have (eventnames.cs) have hints in the name about what parameters to use - e.g. Inventory_OnAdd_ItemInfo_ItemStack

How would I determine that the xxxxx in "EventHandler.RegisterEvent<Item, int>(xxxxxx," can be? I think some are obvious due to the naming e.g. Inventory_OnAdd_ItemInfo_ItemStack, but some I am not sure about e.g. ItemObject_OnItemChanged

Thanks again.
 
In the case of UIS I named the events such that the first word before the underscore is a hint to the type of the target.
So as you expected Inventory_OnAdd_ItemInfo_ItemStack targets an Inventory component
ItemObject_OnItemChanged targets an ItemObject component

As for UCC, Justin does not give any hints to the target of the parameter types in the event name. You have to use the Ctrl+Shift+F trick I mentioned above to find all the places it is used an work out what the target and parameter types are.
I tried convincing him to make that change but it would affect and most likely break a lot of existing projects, so we're waiting for a major release to do that.
 
Top