❔ 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);
}
}
8 Replies
phaseshift
phaseshift2y ago
a delegate is a 'function pointer' private BombermanPlayerMovementAction OnBombermanPlayerMovementRequested1; is a variable for a 'function pointer'. It points no where - you didn't assign any value If you want to use += to register something for it via a method return value, I think you will need to also use ref. private ref BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index) maybe? I've never had need for a ref return type
Honza K.
Honza K.2y ago
Why not make them events?
platinumdalek
platinumdalekOP2y ago
I tried returning 'ref' to the return type of GetMovementActionForPlayer. It didn't change anything.
Honza K.
Honza K.2y ago
private ref BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index)
{
switch (player_index)
{
case 0:
return ref OnBombermanPlayerMovementRequested0;
case 1:
return ref OnBombermanPlayerMovementRequested1;
}
throw new System.Exception("Unexpected player index!");
}

public void RegisterForMovementAction(int player_index, BombermanPlayerMovementAction callback)
{
// Also tried this:
GetMovementActionForPlayer(player_index) = callback;
}
private ref BombermanPlayerMovementAction GetMovementActionForPlayer(int player_index)
{
switch (player_index)
{
case 0:
return ref OnBombermanPlayerMovementRequested0;
case 1:
return ref OnBombermanPlayerMovementRequested1;
}
throw new System.Exception("Unexpected player index!");
}

public void RegisterForMovementAction(int player_index, BombermanPlayerMovementAction callback)
{
// Also tried this:
GetMovementActionForPlayer(player_index) = callback;
}
platinumdalek
platinumdalekOP2y ago
I swear I tried that like 5 times and it didn't work... However, that is working now... thank you!
Honza K.
Honza K.2y ago
well, there are a few things, if you want to return by ref, the return type must be ref Type (private ref BombermanPlayerMovementAction) and you also must return by ref (return ref OnBombermanPlayerMovementRequested0;), if you want to store the reference in a variable, the variable must also be declared with ref keyword
ref BombermanPlayerMovementAction action = ref GetMovementActionForPlayer(player_index);
ref BombermanPlayerMovementAction action = ref GetMovementActionForPlayer(player_index);
since I don't store it and assign new value straight away, I did not do that
platinumdalek
platinumdalekOP2y ago
I suspect what was happening is that I had syntax errors lower in the file (based on changing to ref) which was preventing VS Studio to stop showing me the warning.
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server