Jump to content

How can I access values given in Mission Recovery Dialog when a vessel is recovered?


Recommended Posts

I've been messing around in a little cs project of mine and I want to be able to access and store the values (funds, rep and science) that are given when in the mission recovery dialog after you recover a vessel. Seems fairly straightforward, problem is I'm getting "An object reference is required for the non-static field, method, or property 'MissionRecoveryDialog.fundsEarned'". I'm also getting this error for the same lines I have written for reputation and science.

My code looks like this:

float fundsToStore = MissionRecoveryDialog.fundsEarned;
float repToStore = MissionRecoveryDialog.reputationEarned;
float sciToStore = MissionRecoveryDialog.scienceEarned;

I've attempted to troubleshoot the problem by moving the float declarations in several different parts of my file, and attempted to use static and non-static functions (could potentially be an issue according to threads I've read from stackoverflow, however because I can't alter those files directly I cannot do anything about it).

If it helps when troubleshooting, the code-assist writer (the menu that pops up with matching text when you type) does not show any variable names under MissionRecoveryDialog.

Sorry if it's a little ambiguous and if it's an obvious issue but I'm new to C# and modding in general. Thanks.

Link to comment
Share on other sites

"An object reference is required for the non-static field, method, or property 'MissionRecoveryDialog.fundsEarned'

I can't help you with the Mission Recovery Dialog directly, but this is a common generic programming mistake. You need an instance of the object, but you are trying to use the static definition instead. Hmm.. how can I explain it. Really best thing is Google Instance versus Class.

A Class defines the template for an object, an instance of that class is the working live data formed from that template. You are trying to access DATA on a DEFINITION but the data doesn't exist. You need an instance of it.

Link to comment
Share on other sites

A Class defines the template for an object, an instance of that class is the working live data formed from that template. You are trying to access DATA on a DEFINITION but the data doesn't exist. You need an instance of it.

I think I get what you mean. So theoretically I could have my code look something like this (could I instead use an event listener to wait for the value to change?)

float fundsToStore = MissionRecoveryDialog.fundsEarned;

public void FixedUpdate()
{
if (fundsToStore == null)
{
fundsToStore = 0; //so it creates a real value that can be altered later
}
else
{
fundsToStore = MissionRecoveryDialog.fundsEarned; //it changes the value to what the game's output for this variable is
}
}

Link to comment
Share on other sites

I think I get what you mean. So theoretically I could have my code look something like this (could I instead use an event listener to wait for the value to change?)
float fundsToStore = MissionRecoveryDialog.fundsEarned;

public void FixedUpdate()
{
if (fundsToStore == null)
{
fundsToStore = 0; //so it creates a real value that can be altered later
}
else
{
fundsToStore = MissionRecoveryDialog.fundsEarned; //it changes the value to what the game's output for this variable is
}
}

This code is no different than the previous. You are still accessing the static class, not an instance.

MissionRecoveryDialog is a class. MissionRecoveryDialog dialog = new MissionRecoveryDialog() is an instance. But you dont' want just any instance of it you want *the* instance of it. As Blizzy says above, the game creates a new instance each time the dialog is displayed so you would need to find a way to get a hold of *that* instance.

Link to comment
Share on other sites

There's a GameEvent that gets fired when the dialog appears:

class YourAddon : MonoBehaviour
{
private void Start()
{
GameEvents.onVesselRecoveryProcessing.Add(RecoveryProcessingCallback);
}

private void OnDestroy()
{
GameEvents.onVesselRecoveryProcessing.Remove(RecoveryProcessingCallback);
}

void RecoveryProcessingCallback(ProtoVessel pv, MissionRecoveryDialog dlg, float recovery)
{
// access dlg here
}
}

Link to comment
Share on other sites

onVesselRecoveryProcessing fires before the dialog appears.

onVesselRecovered fires after the dialog appears and its values have been calculated. I think it also passes a reference to the MissionRecoveryDialog object.

Edit: Or not. onVesselRecovered only passes the ProtoVessel reference, so I guess you have to use onVesselRecoveryProcessing.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...