Jump to content

[C# illiterate] Can't run slightly customized experiment


Recommended Posts

This code is for Solar Science, which as of now doesn't actually do science because it refuses to run the experiment. Instead of calling ModuleScienceExperiment, my parts call this instead. It functioned properly in at least .24 through at least 1.0, but now simply fails. Asked for help from #kspmodders IRC and all we determined is that "public new void" should work just fine. Also determined that public new void DeployExperiment() and public new void DeployAction(KSPActionParam p) weren't firing.

//Make sure we're using all available stuff
using System;
using UnityEngine;

namespace PraiseTheSun
{
    //Inherit ModuleScienceExperiment stuff
    public class SolarExperiment : ModuleScienceExperiment{

        // Check if you're around the Sun and height from the surface, and if false post the message
        public bool checkBody() {
            if (vessel.mainBody.name == "Sun" && vessel.mainBody.GetAltitude(vessel.CoM) <= 10000000000d)
                return true;
                ScreenMessages.PostScreenMessage("This experiment only operates closely around Kerbol (the Sun) !", 2, ScreenMessageStyle.UPPER_CENTER);
                    return false;
            }
        // If deploying an Experiment, check the boolean and act accordingly
        public new void DeployExperiment() {
            if (checkBody())
                base.DeployExperiment();
        }
        // If doing an action, check the boolean and act accordingly
        public new void DeployAction(KSPActionParam p) {
            if (checkBody())
                base.DeployAction(p);
        }

    }
}

Anybody have any ideas?

SOLVED: Silly me, I forgot to use .NET 3.5. Still haven't understood why the previous script (which was built using 3.5) stopped working as it was the exact same code, however I'm not going to look a gift horse in the mouth. Thank you everybody!

Edited by Snoopy20111
Link to comment
Share on other sites

Fairly new to C# so I may be way off here but have you tried using override instead of new? Not sure how C# responds to calling a function that's been hidden where override should slip the overridden function in in place of the old function.

Link to comment
Share on other sites

9 minutes ago, wasml said:

Fairly new to C# so I may be way off here but have you tried using override instead of new? Not sure how C# responds to calling a function that's been hidden where override should slip the overridden function in in place of the old function.

That's what I meant by "public new void" working just fine. Putting override in place of new just produced errors, and Visual Studio claimed it couldn't be overriden due to the inherited function not being marked as virtual, abstract, or override.

I should also add that KSP doesn't complain at all either; until the experiment fails to occur, there's no indication anything's wrong.

Link to comment
Share on other sites

My suspicion is that the new/base is not behaving as you expect.

If you add a Debug.Log(base.GetType()); line just before your base.DeployExperiment() line, what type does KSP think the 'base' class is? (It should print either SolarExperiment or ModuleScienceExperiment).

The second check is to go into your part.cfg file and change the SOLAREXPERIMENT line to MODULESCIENCEEXPERIMENT and make sure everything else woks to isolate the issue as actually being where you think it is.

 

D.

Edited by Diazo
Link to comment
Share on other sites

1 hour ago, Diazo said:

My suspicion is that the new/base is not behaving as you expect.

If you add a Debug.Log(base.GetType()); line just before your base.DeployExperiment() line, what type does KSP think the 'base' class is? (It should print either SolarExperiment or ModuleScienceExperiment).

D.

I'm not 100% sure what it's telling me here, but I did as you said, rebuilt, and ran KSP. Loaded up my test craft and pressed the button to execute the experiment a few times (which did nothing), then closed the game. Found this at the very end of the logs. Am I correct in assuming this means KSP simply can't find the script?

[LOG 11:23:51.171] Unpacking Solar Science Test
[EXC 11:23:54.634] FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
	BaseEvent.Invoke ()
	UIPartActionButton.OnClick ()
	UnityEngine.Events.InvokableCall.Invoke (System.Object[] args)
	UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEvent.Invoke ()
	UnityEngine.UI.Button.Press ()
	UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor)
	UnityEngine.EventSystems.EventSystem:Update()
[EXC 11:23:56.449] FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
	BaseEvent.Invoke ()
	UIPartActionButton.OnClick ()
	UnityEngine.Events.InvokableCall.Invoke (System.Object[] args)
	UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEvent.Invoke ()
	UnityEngine.UI.Button.Press ()
	UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor)
	UnityEngine.EventSystems.EventSystem:Update()
[EXC 11:23:59.713] FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
	BaseEvent.Invoke ()
	UIPartActionButton.OnClick ()
	UnityEngine.Events.InvokableCall.Invoke (System.Object[] args)
	UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEvent.Invoke ()
	UnityEngine.UI.Button.Press ()
	UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor)
	UnityEngine.EventSystems.EventSystem:Update()
[EXC 11:23:59.965] FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
	BaseEvent.Invoke ()
	UIPartActionButton.OnClick ()
	UnityEngine.Events.InvokableCall.Invoke (System.Object[] args)
	UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters)
	UnityEngine.Events.UnityEvent.Invoke ()
	UnityEngine.UI.Button.Press ()
	UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData)
	UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor)
	UnityEngine.EventSystems.EventSystem:Update()
[EXC 11:24:05.425] InvalidOperationException: Steamworks is not initialized.
	Steamworks.InteropHelp.TestIfAvailableClient ()
	Steamworks.SteamController.Shutdown ()
	SteamController.KSPSteamController.OnDestroy ()
	SteamController.KSPSteamController.OnApplicationQuit ()
[LOG 11:24:05.469] KbApp.OnDestroy Vessel Crew
[LOG 11:24:05.472] KbApp.OnDestroy Planet Resources
[LOG 11:24:05.479] [UIApp] OnDestroy: KSPedia
[EXC 11:24:05.486] InvalidOperationException: Steamworks is not initialized.
	Steamworks.InteropHelp.TestIfAvailableClient ()
	Steamworks.SteamController.Shutdown ()
	SteamController.KSPSteamController.OnDestroy ()

 

Edited by Snoopy20111
Link to comment
Share on other sites

1 hour ago, Thomas P. said:

You need to compile against .NET 3.5, because Unity only implements a subset of the .NET Framework, and 4.0 isn't supported by the engine.

 

1 hour ago, Padishar said:

Make sure that your project is building against .NET 3.5, not any later version...

Doh!

D'oh is right! :blush: I even read multiple things saying to make sure it used .NET 3.5 instead of 4.0, but never bothered to check...after building it properly it works fine.

What confuses me though is that after working fine up until or past 1.0 without rebuilding, it suddenly didn't like the script (which, if I'm correct, was indeed originally built in .NET 3.5). Oh well.

Link to comment
Share on other sites

18 hours ago, Snoopy20111 said:

D'oh is right! :blush:

That wasn't addressed to you, it was a "Doh! I've been ninja'd by Thomas"... :wink:

It's a very easy mistake to make, especially when using VS as there appears to be a bug (or possibly a misunderstood/badly documented feature) that occasionally causes this option to change without you realising... :(

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...