❔ 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.
8 Replies
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 typeWhy not make them events?
I tried returning 'ref' to the return type of GetMovementActionForPlayer. It didn't change anything.
I swear I tried that like 5 times and it didn't work... However, that is working now...
thank you!
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
since I don't store it and assign new value straight away, I did not do that
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.
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.