platinumdalek
platinumdalek
CC#
Created by platinumdalek on 4/16/2023 in #help
❔ Delegate never assigned to (SOLVED!)
I am trying to simplify my code by having a list of delegates, 1 for each player. The game can have up to 4 players. I want to callback only on the player who needs the event. To attempt that I created a switch statement with the players index and then register/invoke events for that player only. However, I am getting a warning that my delegates are never assigned to. What am I doing wrong? Thank you for the help! Notes: * RegisterForMovementAction and RaiseMovementEvent are being called. * "action" is null in both cases. Its only writing to the local variable, not the member field.
public class BombermanPlayerChannel : ScriptableObject {
public delegate void BombermanPlayerMovementAction(InputAction.CallbackContext context);
private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested0; // <--- warning on this line saying "Is never assigned to"
private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested1;

private BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index) {
switch (player_index) {
case 0:
return OnBombermanPlayerMovementRequested0;
case 1:
return OnBombermanPlayerMovementRequested1;
}
throw new System.Exception("Unexpected player index!");
}

public void RegisterForMovementAction(int player_index, BombermanPlayerMovementAction callback) {
GetMovementActionForPlayer(player_index) += callback;

// Also tried this:
BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
action -= callback;
}

public void RaiseMovementEvent(int player_index, InputAction.CallbackContext context) {
BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
if (action != null) {
action.Invoke(context);
}
}
public class BombermanPlayerChannel : ScriptableObject {
public delegate void BombermanPlayerMovementAction(InputAction.CallbackContext context);
private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested0; // <--- warning on this line saying "Is never assigned to"
private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested1;

private BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index) {
switch (player_index) {
case 0:
return OnBombermanPlayerMovementRequested0;
case 1:
return OnBombermanPlayerMovementRequested1;
}
throw new System.Exception("Unexpected player index!");
}

public void RegisterForMovementAction(int player_index, BombermanPlayerMovementAction callback) {
GetMovementActionForPlayer(player_index) += callback;

// Also tried this:
BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
action -= callback;
}

public void RaiseMovementEvent(int player_index, InputAction.CallbackContext context) {
BombermanPlayerMovementAction action = GetMovementActionForPlayer(player_index);
if (action != null) {
action.Invoke(context);
}
}
11 replies