Jump to content

ok what is wrong with my code?


Recommended Posts

OK so I'm a noob. I started developing plugins for KSP about 2 hours ago. I'm using VS 2010. I'm not sure what I'm doing wrong, but I'm getting a "an Object Reference is required for the non-static field method or property Vessel.<anything I try to use in the vessel class>

Here's my code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityThreading;


namespace KSPEngines
{
public class EngineControls
{
protected override void onFlightStart()
{
startGUI();
}

public void startGUI()
{
if (Vessel.active)
{
Guid vesselID = Vessel.id;
}
GUI.Box(new Rect(25,25,100,100), "Engine Controls");
}
}
}

Link to comment
Share on other sites

Simple: you're using Vessel, not "vessel" (no caps).

Vessel is the class, vessel (or this.vessel) is the instance of the class.

Thanks for the reply.

Can you give an example of how it would be used? I'm coming from java, so maybe there's some subtle difference I'm not getting.

In java if I wanted to use something from the math class, I would say

double randomNum = Math.random(); //random being a component of the math libraries

So in this if I understand what you're saying, If i want to get the ID of the vessel, I say this:

int vesselID = id.vessel();

I know that I'm not understanding the class structure completely, so if someone could shed some light, that would be appreciated.

Link to comment
Share on other sites

Thanks for the reply.

Can you give an example of how it would be used? I'm coming from java, so maybe there's some subtle difference I'm not getting.

In java if I wanted to use something from the math class, I would say

double randomNum = Math.random(); //random being a component of the math libraries

So in this if I understand what you're saying, If i want to get the ID of the vessel, I say this:

int vesselID = id.vessel();

I know that I'm not understanding the class structure completely, so if someone could shed some light, that would be appreciated.

No no, it's just like Java; classes attributes are accessed the "object.attribute", like vessel.id.

There are a few things I noticed though:

- your EngineControls class does not inherit from Part or PartModule; that's necessary for a part (and it's important to get the part's vessel).

- the Part class has a few attributes you inherit: "parent" (another Part), "vessel" (a Vessel instance, containing a list of parts), and other stuff. When I said to use "vessel.active" instead of "Vessel.active", it means using the instance attribute "this.vessel.active" (is the vessel in which my part is right now active?) instead of the static attribute Vessel.active (which doesn't know which instance is calling).

Class vs. instance is the same in Java; one is the definition (Class, Caps "title"), the other is the instantiated object from this class (instance, no caps, linked to your object).

Link to comment
Share on other sites

Thanks again for the reply.

VS still does not like me using the this.vessel on anything.

For example:

foreach (Part p in this.vessel.parts)

{

}

does not work

I understand what you're saying, and I'm aware that I need to reference the currently selected vessel. But I really really need to see a code example to understand what's going on. I don't have a strong background in object oriented programming, so this is very new to me.

I understand WHAT I need to do, so don't bother trying to explain that, what I'm lost on is the HOW to do it.

"this." was a pretty helpful tip, but how does that relate to "vessel" which is a member of Parts, and how does that relate to Vessel who has parts as a relative??

Link to comment
Share on other sites

Also, I'm not trying to create a part here. I'm trying to do something else, so of course my engine contols class wouldn't inherit from part.

But like I said initially, code examples please. Would be much more helpful.

There are a few there: http://kspwiki.nexisonline.net/wiki/Module_code_examples

But for the "not a Part" thing, I'm not sure it's possible to do right now.

What was new in 0.15 is the PartModule class (lots of details here: http://kerbalspaceprogram.com/forum/showthread.php/10296-0-15-code-update-PartModule-KSPField-KSPEvent-ConfigNode-and-PartResource), but even with this, you still need to add your code via a part (or edit the .cfg of an existing one).

From what I got from the code above, you're trying to control engines; those are the parts you should extend (or create your own).

As for this.vessel, well, if your class is not inherited from Part, and doesn't have "Vessel vessel = (code to define the vessel)" in it, sure, this.vessel doesn't work, since it's not defined in your class.

The keyword "this" means that it's the current instance of a class; you could have a Part A (decoupler) and a Part B (engine) at the same time, both parts, but if your code says "this.vessel", A and B will each look their own value for vessel.

Anyway, I'm pretty sure you'll have to use Part or PartModule; even MechJeb still uses a part (and it's one of the biggest plug-in out there).

Link to comment
Share on other sites

OK so I'm a noob. I started developing plugins for KSP about 2 hours ago. I'm using VS 2010. I'm not sure what I'm doing wrong, but I'm getting a "an Object Reference is required for the non-static field method or property Vessel.<anything I try to use in the vessel class>

Here's my code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityThreading;


namespace KSPEngines
{
public class EngineControls
{
protected override void onFlightStart()
{
startGUI();
}

public void startGUI()
{
if (Vessel.active)
{
Guid vesselID = Vessel.id;
}
GUI.Box(new Rect(25,25,100,100), "Engine Controls");
}
}
}

Extend your class as a Part and add OnPartStart or something like that (search the .dll). Now even if you don't wan't to do a part (just engine control as far as i can see), you should add a part that will trigger the engine control. That's how things work, you shouldn't do things in the GUI section because physics are not being simulated while you have your GUI open.

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