Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

Thank you. My next question: Is there a good way to tell if the player can see asteroids?

Also how can I tell what size class an asteroid belongs to?

The only reliable way to figure out the asteroid size class is by taking the last character of the ModuleAsteroid.prefabBaseURL string: https://github.com/DMagic1/Orbital-Science/blob/master/Source/DMAsteroidScience.cs#L107-L132.

As for seeing asteroids the only way I know how is to search all vessels within loading distance for a ModuleAsteroid PartModule.

Link to comment
Share on other sites

I have a fun vector question.

Does anyone know how to draw a line perpendicular to the direction of travel on the surface of a planet with a given width (doesn't have to actually map the surface, just two points) and its center directly below the vessel. So in an equatorial orbit the line would have be drawn straight north/south, and in a polar orbit would be drawn east/west. Or more to the point, how to calculate the Vector3d end points of said line.

I can get the position under the vessel with:


body.GetSurfaceNVector(lat, lon);

I can think of maybe figuring out a way of converting the width to modifiers for the lat/lon based on vessel inclination (some geometry to give lat/lon +/- values). Then using the same function to get the position at those coordinates. I think calculating new coordinates that way would end up with the line flipping directions at some point, though I might be wrong.

Is there some better way of handling it?

Do asteroids not get generated in a career game before the tracking station is upgraded to be able to see them?

I don't think they do. Asteroids generate some log spam when they are created, so you can probably just watch for that to make sure.

Edited by DMagic
Link to comment
Share on other sites

One of the Vector3 math things would do it, I can't remember the name but it's the left-hand rule one.

Vessel.srfVelocity is one reference vector, Vessel.Up is the other (from center of planet to center of vessel), produce the 3rd vector perpendicular to those two vectors (the command I can't remember) then negative it for the other half.

I think that would work?

D.

Link to comment
Share on other sites

Does anyone know how to draw a line perpendicular to the direction of travel on the surface of a planet with a given width (doesn't have to actually map the surface, just two points) and its center directly below the vessel.
One of the Vector3 math things would do it, I can't remember the name but it's the left-hand rule one.

Direction vector perpendicular to velocity and parallel to the ground but I don't know if that's what he's asking for

There's a chunk of code on the plugin section of the wiki for drawing arrows that would meet your requirements for configurable width. ArrowPointer might function fine if the width isn't too little

Link to comment
Share on other sites

Hi all,

does anyone know how to create the static effect when you first load vessel on the portrait cameras?

I'm sure I stumbled across this a while back.. but now I can't find any reference for it.

I'm sure it's something simple.

T.I.A.

Link to comment
Share on other sites

Excellent, thanks for the help. Some combination and modifications of the above seems to work.

ZRtKvtB.gif


Vector3d center = v.transform.position;
Vector3d up = body.GetSurfaceNVector(lat, lon);

Vector3d srfCenter = body.position + height * up;

Vector3d VelFor = Vector3.ProjectOnPlane(v.srf_velocity, up).normalized;
Vector3d vesselPerp = Vector3d.Cross(VelFor, up).normalized;

Vector3d left = srfCenter + width * vesselPerp;
Vector3d right = srfCenter - width * vesselPerp;

Then I just draw a triangle with center, left, and right as the three points.

Link to comment
Share on other sites

That seems like a roundabout way of doing it since you probably started with a viewing angle. Also, you might want to double check whether you want surface or orbital velocty as your reference (srf is probably correct, but...)

down = -vessel.upAxis
Vector3d VelFor = Vector3.ProjectOnPlane(v.srf_velocity, down).normalized;
// rotate down vector about velocity vector, shift along that direction until level with the surface below the vessel
Vector3d left = vessel.transform.position + (Quaternion.AngleAxis(0.5f * viewingAngle, VelFor) * down) * (vessel.altitude / Math.Sin(0.5f * viewingAngle));
Vector3d right = vessel.transform.position + (Quaternion.AngleAxis(-0.5f * viewingAngle, VelFor) * down) * (vessel.altitude / Math.Sin(0.5f * viewingAngle));

- - - Updated - - -

On another topic, how do you close the astronaut complex overlay.

Edited by Crzyrndm
Link to comment
Share on other sites

Is there a function to get the speed of sound for a position/vessel lying around anywhere? I want to transform an arbitrary speed in m/s to mach and multiplying by the ratio between vessel mach and current srf speed is annoyingly hacky

Link to comment
Share on other sites

help...

I'm just getting to grips with coding a PartModule using Visual Studio Express. I see OnUpdate() getting called but not OnFixedUpdate().

I have 'the same' code in both overridden functions (take some timestamps & a bool 'I've been called'). Another function puts em in a window.

I see OnUpdate() being called with a delta time of 0.039-ish, like 25/sec which fits in with GUI update. The OnFixedUpdate 'I've been called' flag stays false, I made the window also show module.isActiveAndEnabled and I see it's true. My part .cfg had physicsSignificance=1, but I also tried 0 and commented out with no 'improvement' - the .cfg has no other Modules in it.

The doc I've read seems to say both functions should only be called for 'active' Modules, since OnUpdate() is called I'd expect OnFixedUpdate() to get called also.

Any ideas what I'm misunderstanding?

Link to comment
Share on other sites

The following code works, but it cover all of the GUI. How can I make so that it's bellow the GUI?

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class imageOverlay : MonoBehaviour
{

Texture2D textureOverlay = new Texture2D(Screen.width, Screen.width, TextureFormat.ARGB32, true);

protected void Start()
{
string path = KSPUtil.ApplicationRootPath.Replace(@"\", "/") + "/GameData/Textures/textureoverlay.png";
byte[] texture = File.ReadAllBytes(path);
textureOverlay.LoadImage(texture);
RenderingManager.AddToPostDrawQueue(5, new Callback(drawGUI));
}

void drawGUI()
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), textureOverlay);
}
}

Link to comment
Share on other sites

I can see how to get the maxThrust from a ModuleEngines (me.maxThrust), does anyone know how to get 'max thrust at current atmospheric pressure'?

I figure I could use the atmosphere curve - I checked that for LV-T45 ASLThurst = maxThrust * curve@1 / curve@0 - so I guess that's one way to do it. Only issue is that ME has atmCurve and atmosphereCurve - and I was kinda hoping there was some utility that already did the work...

Link to comment
Share on other sites

ModuleEngines e = part.Modules.FirstOrDefault(m => m is ModuleEngines);
if (e != null)
{
float pressureCorrectedThrust = e.maxThrust * e.realISP / e.atmosphereCurve.Evaluate(0); // ratio of current to vacuum thrust is the pressure modifier
float machDensityCorrectedThrust = pressureCorrectedThrust * flowMultiplier; // flowMultiplier is the velCurve * atmCurve result
float currentRequestedThrust = machDensityCorrectedThrust * (thrustPercentage / 100) * e.vessel.ctrlState.mainThrottle; // thrust percentage is the engine % limiter
}

Edited by Crzyrndm
Link to comment
Share on other sites

Just a note: that will only work if the engine module is a ModuleEngines, and it's not null-safe.

Try thins.

ModuleEngines e = null;
for(int i = part.Modules.Count -1; i >= 0; --i)
if((e = part.Modules[i] as ModuleEngines) != null)
break;
if(e != null)
{
// floats etc
}

replace the top line with that, and stick the code in the if block.

Link to comment
Share on other sites

Very specific question: does anyone know how to access the "pos" key values in the TechTree config files? Is it within the RDNode class? I'm seeing that you can access just about any other key from that class, but I'm stumped on position. I need to get the x, y, z values and change them.

Link to comment
Share on other sites

Hi,

i got really frustated trying to add a custom subcategory to the "Filter by Function" Filter. The code is use looks like this:

    [KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class SurfaceStructuresCategory : MonoBehaviour
{
internal string category = "Filter by Function";
internal string subCategory = "Surface Structures";


/**
* Called when the script instance is being loaded
*/
private void Awake()
{
GameEvents.onGUIEditorToolbarReady.Add(KKAOSS_Filter);
}


/**
* The function to add the modules of this mod to a separate category
*/
private void KKAOSS_Filter()
{
//create and the icons
Texture2D icon = new Texture2D(32, 32);
Texture2D icon_selected = new Texture2D(32, 32);


//load the icons
icon.LoadImage(File.ReadAllBytes("GameData/PlanetaryBaseInc/PartList/planetarySurfaceStructures.png"));
icon_selected.LoadImage(File.ReadAllBytes("GameData/PlanetaryBaseInc/PartList/planetarySurfaceStructures_selected.png"));


//create the icon for the filter
RUI.Icons.Selectable.Icon filterIcon = new RUI.Icons.Selectable.Icon("KKAOSS_filter_icon", icon_selected, icon);


//Find the function filter
PartCategorizer.Category functionFilter = PartCategorizer.Instance.filters.Find(f => f.button.categoryName == category);

//Add a new subcategory to the function filter
PartCategorizer.AddCustomSubcategoryFilter(functionFilter, subCategory, filterIcon, p => p.name.Contains("KKAOSS_"));


//Force the icon to show (>_<)
RUIToggleButtonTyped button = functionFilter.button.activeButton;
button.SetFalse(button, RUIToggleButtonTyped.ClickType.FORCED);
button.SetTrue(button, RUIToggleButtonTyped.ClickType.FORCED);
}
}

But the category refuses to be added.

I also tried to add dummy categories and subcategories like in the example of the of the Editor-Toolbar-Modding-Guide :

    [KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class SurfaceStructuresCategory : MonoBehaviour
{
/**
* Called when the script instance is being loaded
*/
private void Awake()
{
GameEvents.onGUIEditorToolbarReady.Add(KKAOSS_Filter);
}


/**
* The function to add the modules of this mod to a separate category
*/
private void KKAOSS_Filter()
{


RUI.Icons.Selectable.Icon myIcon = PartCategorizer.Instance.iconLoader.GetIcon("R&D_node_icon_advconstruction");


//add a custom filter to the toolbar
PartCategorizer.Category myFilter = PartCategorizer.AddCustomFilter("Test Filter", myIcon, Color.yellow);


//add subcategories to the filter you just added
PartCategorizer.AddCustomSubcategoryFilter(myFilter, "Subfilter 1", myIcon, p => p.name.Contains("engine"));
PartCategorizer.AddCustomSubcategoryFilter(myFilter, "Subfilter 2", myIcon, p => p.name.Contains("fuel"));
PartCategorizer.AddCustomSubcategoryFilter(myFilter, "Subfilter 3", myIcon, p => p.name.Contains("bla"));
}
}

But neither of them works...really starting to feel stupid...

Is there anything i have to set additionally in my c# project to make this work?

Ps: I also put the p => p.name.Contains("KKAOSS_") in a separate function for logging purposes and the filter actually runs and finds the right parts it is just not shown

Edited by Nils277
Link to comment
Share on other sites

Very specific question: does anyone know how to access the "pos" key values in the TechTree config files? Is it within the RDNode class? I'm seeing that you can access just about any other key from that class, but I'm stumped on position. I need to get the x, y, z values and change them.

Positions are almost always transform.position (and if not, almost always transform.localPosition), but I haven't done anything with the RnD view so I don't know specifics. Have you looked at the code for the old tech tree mods and seen how they position things?

But neither of them works...really starting to feel stupid...

1) Triple check the log for errors when entering the editor scenes. That callback is very fragile and an error from any of the subscribed functions is enough to bring it to a grinding halt

2) Tried manually refreshing the category UI? Click on Filter by Function to close it and then open it again

Edited by Crzyrndm
Link to comment
Share on other sites

Hi, I'm writing a plugin, part of which requires lists of resource amounts across an indefinite number of vessels in a List<Vessel>, but I'm struggling on how to get around it. I know I can get a total amount of a resource in a vessel by using a foreach() statement to look for parts containing that resource in the vessel and getting the sum of the doubles, but I've been getting some odd results when I've been trying to get numbers for resource amounts across many vessels, I'm guessing this is because of the fact that it's all in one big foreach() statement spanning across multiple vessels or something. I want to get the resource amount totals of each vessel (bearing in mind the List is intended to contain an indefinite, non-restricted, number of vessels) and put those totals into a List so I can then call them by index. Is there some way to do this? Thanks.

Link to comment
Share on other sites

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