Binding Values With ItemBinding Through Code

AlexM

New member
Hi there!

I've been trying to use the ItemBinding script on my weapons for a custom script that helps process damages. Setting up bindings in the Inspector works fine, but I'm trying to set up automatic bindings to save time. I was wondering how I would go about this.

I've tried going through the list of all bindings in the weapon's ItemBinding script, comparing names to get the specific attributes I want, then setting the binding's BoundComponent to the custom script and the PropertyPath to the name of the specific property. It seems to work, because I can see that it's bound in the Inspector, but the bound value isn't actually being copied to the desired property. What exactly am I missing here?

I'll attach the script below. It's still unfinished, so not all of the bindings are being set up. I have one bound using my method and one bound in the Inspector to test it.

I would put this in the UCC integration forum, since it uses both UCC and UIS, but the problem I'm having is specifically with UIS's ItemBinding. Any help would be great! Thank you!
 

Attachments

  • DamageBridge.cs
    8.6 KB · Views: 1
If you are going to do it in code, instead of using the ItemBinding script I'd recommend simply creating the binding directly in that custom script.
That's what I do for AmmoData in the ShootableWeaponAmmoBinding script in the integration.

So instead of binding the property directly to the attribute, I bind it first to a property on that custom class and then choose how it modfies the weapon. The important part is this:

Code:
/// <summary>
/// The ammo data is used by the bound attribute to update the shootable weapon or attribute values.
/// </summary>
public AmmoData AmmoData {
    get
    {
        IItemIdentifier consumableItemIdentifier = m_ShootableWeapon.GetConsumableItemIdentifier();
        if (consumableItemIdentifier == null) {
            Debug.Log("consumableItemIdentifier is null");
            return m_AmmoData;
        }
        
        m_AmmoData = new AmmoData(
            m_ShootableWeapon.GetConsumableItemIdentifier()?.GetItemDefinition(),
            m_ShootableWeapon.ClipSize,
            m_ShootableWeapon.GetConsumableItemIdentifierAmount()
            );
        
        return m_AmmoData;
    }
    set
    {
        m_AmmoData = value;
        SetAmmoToShootableWeapon(m_AmmoData);
    }
}

Then check how I create the AttributeBinding and initialize it in the Awake function. And how I bind and unbind it when setting a new item.
I'm using an event invoked by the ItemObject component "EventNames.c_ItemObject_OnItemChanged" which tells me if there are any changes on the item within that ItemObject.

you custom class will give you a lot more control, and it will be easier then trying to use the ItemBinding component which is very generic and is meant to be used for simple use cases.

I hope that makes sense
 
Ok, I think I see what you're saying. It would be better to get the attributes straight from the item itself, then update the values on the OnItemChanged event? I'll try this out, thank you!

Edit: So, I'm kinda new to using events in code. How would I listen for when the ItemObject component invokes the OnItemChanged event?
 
Last edited:
We invoke that event using our custom event handler system check out the documentation here:

I would highly recommend you read through the "ShootableWeaponAmmoBinding " script in detail, make sure you understand how it works, it is the best example for what you are trying to do.
You'll see that we are listening to the event in the Awake function and stop listening on Destroy.
And notice that we are using the AttributeBinding object to bind the attribute to the property.
Binding an attribute means that both the property and the attribute will always have the same value until unbinded. We use a few tricks to do this.
 
Top