I have a beginner question. I run a coroutine during some authentication code that fetches additional data from a server, after which I want to continue on with the additional data. Right now I do this:
private AuthRequestMessage _authRequestMessage;
private NetworkConnection _conn;
public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
{
_authRequestMessage = msg;
_conn = conn;
StartCoroutine(API.GetOTPForUser(msg.user, ReceiveOTPForUser));
}
// Called when we receive the OTP from the API for the user trying to log in.
private void ReceiveOTPForUser(OTP userOTP)
{
var msg = _authRequestMessage;
var conn = _conn;
... code continues
}
You can see I’m saving some state as private variables. I know I could pass these both into the coroutine and receive them back in the callback, but that involves changing the coroutine itself to pass along the all that data, which has little to do with what the coroutine actually does. Is there a better way to get this data from the function starting the coroutine to the callback? How would you handle this?