Search Unity

Overriding an IEnumerable

Discussion in 'Scripting' started by Redden44, Mar 19, 2019.

  1. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Hello, got this function from a tutorial and I'm trying to override it in a subclass, the code compiles fine but it doesn't call the base function, only the overriding one. Anyone can help me please? Thanks.


    Base function
    Code (CSharp):
    1.  
    2. #region ContextMenu
    3.  
    4.     // Invia le azioni del Context Menu.
    5.     public virtual IEnumerable<ContextMenuAction> GetContextMenuActions(ContextMenu contextMenu)
    6.     {
    7.         // Accensione e spegnimento.
    8.         if (powerButton == State.On)
    9.         {
    10.             yield return new ContextMenuAction
    11.             {
    12.                 LocalizationKey = LocalizationTable.GetLocalization("turn_machine_off", LocalizationName),
    13.                 RequireCharacterSelected = false,
    14.                 Action = (ca, c) => SetJob_TurnMachineOff()
    15.             };
    16.         }
    17.         else if (powerButton == State.Off)
    18.         {
    19.             yield return new ContextMenuAction
    20.             {
    21.                 LocalizationKey = LocalizationTable.GetLocalization("turn_machine_on", LocalizationName),
    22.                 RequireCharacterSelected = false,
    23.                 Action = (ca, c) => SetJob_TurnMachineOn()
    24.             };
    25.         }
    26.     }
    27.  
    28.     #endregion
    Override
    Code (CSharp):
    1.  
    2. #region ContextMenu
    3.  
    4.     // Invia le azioni del Context Menu.
    5.     public override IEnumerable<ContextMenuAction> GetContextMenuActions(ContextMenu contextMenu)
    6.     {
    7.         base.GetContextMenuActions(contextMenu);
    8.  
    9.         // Ricarica di carburante.
    10.         if (FuelLevel <= 0 && isWaitingForFuel == false)
    11.         {
    12.             yield return new ContextMenuAction
    13.             {
    14.                 LocalizationKey = LocalizationTable.GetLocalization("add_fuel", LocalizationName),
    15.                 RequireCharacterSelected = false,
    16.                 Action = (ca, c) => SetJob_AddFuel()
    17.             };
    18.         }
    19.     }
    20.  
    21.     #endregion
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You override functions to replace base code with overriding code. That's it, override replaces base. The behaviour you're talking about - don not call base function, but call override instead - is by design. You can manually call base code using
    Code (CSharp):
    1. base.GetContextMenuActions(...)
    the 'base' keyword.
     
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    You can use the
    IEnumerable<T>
    type as an iterator in a foreach loop (you probably do that from somewhere outside). You can also store the
    IEnumerable<T>
    type in a variable to operate later on it.

    But if you just call a method with a return type of
    IEnumerable<T>
    without doing something with it, all the returned items are going to nowhere.

    What you have to do is the following:
    Code (CSharp):
    1.     // Invia le azioni del Context Menu.
    2.     public override IEnumerable<ContextMenuAction> GetContextMenuActions(ContextMenu contextMenu)
    3.     {
    4.         IEnumerable<ContextMenuAction> baseItems = base.GetContextMenuActions(contextMenu);
    5.  
    6.         foreach (ContextMenuAction item in baseItems)
    7.         {
    8.             yield return item;
    9.         }
    10.    
    11.         // Ricarica di carburante.
    12.         if (FuelLevel <= 0 && isWaitingForFuel == false)
    13.         {
    14.             yield return new ContextMenuAction
    15.             {
    16.                 LocalizationKey = LocalizationTable.GetLocalization("add_fuel", LocalizationName),
    17.                 RequireCharacterSelected = false,
    18.                 Action = (ca, c) => SetJob_AddFuel()
    19.             };
    20.         }
    21.     }
     
    Redden44 and Antistone like this.
  4. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Thank you very much :)