Author Topic: Proper use of undoObject in EventDelegate?  (Read 4327 times)

sendatsu_yoshimitsu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Proper use of undoObject in EventDelegate?
« on: August 16, 2014, 02:40:37 AM »
I'm futzing around with the item inspector from the inventory example to try and adapt it to work for my game's specs. I'm trying to add an EventDelegate field for each base item, so item-specific skill functions can be attached as delegates in the inventory inspector. I think I have both item classes configured correctly, I make a new EventDelegate in baseitem and have a getter in gameitem:
  1. public List<EventDelegate> useItem = new List<EventDelegate>();
  2.  
  3.  
  4.         public List<EventDelegate> useItem{
  5.                 get
  6.                 {
  7.                         if (baseItem == null) return null;
  8.                         return useItem;
  9.                 }
  10.  

Where I'm running into problems is adding a field to the database inspector to set the delegate in the inspector. I originally tried treating it like an object field:

            
  1. GUILayout.BeginHorizontal();
  2.                                 List<EventDelegate> itemSkill = EventDelegateEditor.Field("Item skill", item.useItem);
  3.                                 if (drawIcon) GUILayout.Space(iconSize);
  4.                                 GUILayout.EndHorizontal();

However, this throws an overloaded method match error, and checking the documentation I see that instead of a string, .Field wants an object undoObject. What I can't figure out from the documentation is, what exactly is it looking for with undoObject?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #1 on: August 16, 2014, 10:13:49 AM »
UndoObject is the object that owns this field -- the MonoBehaviour that has this field, in other words. If you're inside an editor class you can just pass 'target' to it.

sendatsu_yoshimitsu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #2 on: August 16, 2014, 02:44:44 PM »
Ooh thank you! Sorry for the follow-up, since I'm quite certain it's my own error, I'm still getting an error, cannot convert from void to List<EventDelegate>. I have the list's declaration in BaseItem and a getter in GameItem, am I missing a reference somewhere? I thought as long as the getter was present and pointed at the right thing, the database should be able to see it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #3 on: August 17, 2014, 08:50:43 AM »
You can't use it with properties. Only fields.

sendatsu_yoshimitsu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #4 on: August 17, 2014, 11:06:43 PM »
Oh okay, that makes sense- I know this isn't part of NGUI, so I appreciate your taking the time to help :)

sendatsu_yoshimitsu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #5 on: August 18, 2014, 04:34:15 AM »
Is there something special I need to do in order to make an EventDelegate writeable? I have it exposed in the inventory inspector, but dragging & dropping a monobehavior onto it doesn't work, and trying to manually assign an asset with the select button doesn't return any of the monobehaviors in the asset folder.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #6 on: August 18, 2014, 11:59:15 AM »
Have a look at NGUIEditorTools.DrawEvents. This is what should be used to draw your properties. You also need to use Unity's functionality before and after your editable properties:
  1. serializedObject.Update();
  2. ...all your properties go here...
  3. serializedObject.ApplyModifiedProperties();
Consult Unity's docs for more info or just look at any of NGUI's editor classes, such as UIGridEditor.cs.

sendatsu_yoshimitsu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Proper use of undoObject in EventDelegate?
« Reply #7 on: August 18, 2014, 12:10:07 PM »
Perfect, that helps tremendously- thank you so much for pointing me in the right direction  :)