Cephei

The official unoffical "help a fellow plugin developer" thread

Recommended Posts

I've been having some issues with FlightCtrlState, specifically I can't get my script to compile because there's no apparent way for me to define a variable (flightControls) as the FlightCtrlState (is there some gameObject I need to find?). I also can't use GetComponent since there is no FlightCtrlState component, yet it requires a reference for me to be able to get the throttle level... :huh:

Here's the compile error I get:

Error CS0120: An object reference is required to access non-static member `FlightCtrlState.mainThrottle' (CS0120) (PuffinTech)

Is there any way for me to actually assign FlightCtrlState to a variable?

-------------------------------------------------------------------------------------------------

One way to solve the issue of non-moving attached parts is to add a section to the plugin that moves the joint anchor points to the correct position relative to the pusher plate.

Here's the Unity reference for joints in general. All you need to do is a foreach(Joint eachJoint in Joints) and then change the y-position of the joint anchor to be the correct position as the part animates.

http://docs.unity3d.com/Documentation/ScriptReference/Joint.html

Edited by OrbitusII

Share this post


Link to post
Share on other sites
yet it requires a reference for me to be able to get the throttle level... :huh:

vessel.ctrlState or FlightInputHandler.state

That's a C# compiler error, it refers to an Object in the C# sense (an instantiated class) rather than a unity GameObject. FlightCtrlState doesn't extend Component or one of it's children (Monobehaviour) so it wouldn't be possible to attach it as a component even if you wanted to.

One way to solve the issue of non-moving attached parts is to add a section to the plugin that moves the joint anchor points to the correct position relative to the pusher plate.

Yeah, I did consider that but I was thinking you'd end up with offset rigidbodies with wierd centres of mass and whatnot but actually you can move the whole part (part.transform) first so if you do that before changing the joint attach point then it may work ok.

Share this post


Link to post
Share on other sites

Hi, stupid newbie with the Orion here.

I'm having a problem with the jolt of the nuclear bombs exceeding the structure of the vessel.

The mod models the Project Orion nuclear pulse rocket. Due to its nature, the plugin bypasses the standard KSP propulsion module. Instead, the bomb impulse is directly applied to the vessel.

I am building on a code fragment graciously supplied by NovaSilisko. The delta V is gently and magically applied to the vessel by

this.vessel.ChangeWorldVelocity(base.transform.up * (aNukeRound.bombImpulse / totalVesselMass));

The kick-in-the-pants is brutally applied to the vessel by

base.rigidbody.AddRelativeForce(new Vector3(0f, aNukeRound.bombImpulse, 0f), ForceMode.Force);

where "base" is the Orion engine part. The idea is that poorly constructed vessels with get shaken apart.

This works fine with the smaller bombs, where aNukeRound.bombImpulse equals 2000 or 3500 kiloNewtons. But it does not work very well with the larger bombs, of 80,000 or 400,000 kiloNewtons. On the launch pad, the first bomb makes the test cockpit and ASA placed on top of the engine shoot off like an atomic powered champagne cork. Even though I tied them down with 16 struts. They made an altitude of 5,000 meters before they fell back.

Trying to cool things down, I mistakenly tried using this code:

base.rigidbody.AddRelativeForce(new Vector3(0f, aNukeRound.bombImpulse, 0f), ForceMode.Impulse);

Bad move. The cockpit detached and made the jump to lightspeed. Apogee of 92,000 freaking meters. I've never seen a ship develop a plasma sheath in the atmosphere while moving up.

I'm not sure what my best options are, and I'd enjoy any input the forum would care to offer.

I could just remove the impulse kick, but that's no fun.

I could tone down the amount of impulse, though with the largest bomb I'd have to divide it by 100 or so.

I could make the engine grab anything stacked on it in a manner similar to a Quantum Strut. Trouble with that is I figure the part directly touching the engine will stay connected, but all the remaining parts will still be kicked off.

Or maybe there are other options I have not learned about yet.

Thanks!

Share this post


Link to post
Share on other sites
where "base" is the Orion engine part.

base is a reserved keyword in the C# language that refers to the superclass of the object running the code. In this case probably either PartModule.

Given they've all got aliases to the correct thing it generally doesn't matter from a functionality perspective. I'd usually use part.rigidbody to make it clear where the rigidbody was located but base.rigidbody, this.rigidbody or simply rigidbody will all refer to the same thing.

Anyway, back to the question. Firstly I'd remove the ChangeWorldVelocity call when testing forces. If it's perfectly smooth I'd guess it's adding acceleration outside the physics system which may be causing the funny results you're seeing (i.e. stuff not exploding). I don't know how much experimenting Nova did or his knowledge of the game when he wrote it but I'd personally attempt to propel the vessel purely through physics forces first and only resort to changing velocity if I couldn't get it smooth enough.

It always helps to define what you'd consider to be correct behaviour. What should happen if you apply massive forces to a lightweight vessel? I don't know if unity/KSP simulates explosions from excessive force or whether it's just the joints breaking and the colliders colliding.

p.s. Have you had a play around with RigidBody.AddExplosionForce?

Share this post


Link to post
Share on other sites
base is a reserved keyword in the C# language that refers to the superclass of the object running the code. In this case probably either PartModule.

Yes, I was trying to make it clear that this was in the nuclear engine code, and not somewhere else

Anyway, back to the question. Firstly I'd remove the ChangeWorldVelocity call when testing forces. If it's perfectly smooth I'd guess it's adding acceleration outside the physics system which may be causing the funny results you're seeing (i.e. stuff not exploding). I don't know how much experimenting Nova did or his knowledge of the game when he wrote it but I'd personally attempt to propel the vessel purely through physics forces first and only resort to changing velocity if I couldn't get it smooth enough.

It always helps to define what you'd consider to be correct behaviour. What should happen if you apply massive forces to a lightweight vessel? I don't know if unity/KSP simulates explosions from excessive force or whether it's just the joints breaking and the colliders colliding.

I'm sorry, I did not make myself clear. The problem is that stuff is exploding, proper behavior is not exploding.

Currently the code works perfectly with a 3500 kiloNewton bomb. The bomb goes off, the pusher plate moves, the entire vessel undergoes acceleration, and the various parts composing the ship might rattle a bit. The acceleration is from ChangeWorldVelocity, the rattling is from AddRelativeForce.

The incorrect behavior occurs with the 80,000 kN and the 400,000 kN bombs. The bomb goes off, the pusher plate moves, the entire ship undergoes acceleration, the cockpit detaches from the Orion engine and shoots for the sky like a bat out of hell, reaching an altitude of 92,000 meters before plummeting to the ground, the Orion engine makes it to about 500 meters before falling to the ground.

Apparently the AddRelativeForce is adding so much force to the engine that it impacts on the base of the cockpit perched on its nose with enough force to snap the connection and send the cockpit into the sky.

p.s. Have you had a play around with RigidBody.AddExplosionForce?

Yes, I have. I use it for the collateral damage the bombs inflict on other vessels in the vicinity. Works well, other ships on the grounds around the launch site reacted as expected. Ones that are far away are shoved around. Ones that are near the explosion get blown over the horizon.

However, this is not suited for the bomb propulsion proper. The pulse units are not so much bomb as they are nuclear shaped charges. They really do not inflict force in a bomb-like fashion, they are focused enough so that the reduction due to distance does not really happen.

Share this post


Link to post
Share on other sites

Does anyone know how to add an fx_exhaustFlame_yellow effect to a part at run-time?

i.e., when a part reaches 90% of its maximum temperature, I'd like for it to start spewing flames.

Share this post


Link to post
Share on other sites
Does anyone know how to add an fx_exhaustFlame_yellow effect to a part at run-time?

i.e., when a part reaches 90% of its maximum temperature, I'd like for it to start spewing flames.

GameObject flame = (GameObject)UnityEngine.Object.Instantiate(UnityEngine.Resources.Load("Effects/fx_exhaustFlame_yellow));

Share this post


Link to post
Share on other sites
GameObject flame = (GameObject)UnityEngine.Object.Instantiate(UnityEngine.Resources.Load("Effects/fx_exhaustFlame_yellow));

ROCKIN'. Thank you so much.

Share this post


Link to post
Share on other sites

ALMOST THERE! One last question: once the effect's transform is parented to the part's, how do I get the effect to point in the direction of the part's velocity?

Share this post


Link to post
Share on other sites
ALMOST THERE! One last question: once the effect's transform is parented to the part's, how do I get the effect to point in the direction of the part's velocity?

Something like part.rigidbody.velocity may work but you're running into krakensbane territory there so I'm not sure what the correct velocity is.

Share this post


Link to post
Share on other sites

I believe the correct velocity is (part.Rigidbody.GetPointVelocity(part.transform.position) + Krakensbane.GetFrameVelocityV3f() - Krakensbane.GetLastCorrection() * TimeWarp.fixedDeltaTime), but can I just do myFX.lookAt(velocity)?;

Share this post


Link to post
Share on other sites
can I just do myFX.lookAt(velocity)?;

Oh sorry, velocity is a directional vector while LookAt requires a world position so you'd do something like:

myFX.LookAt(myFX.transform.position + velocityVector)

Share this post


Link to post
Share on other sites
Oh sorry, velocity is a directional vector while LookAt requires a world position so you'd do something like:

myFX.LookAt(myFX.transform.position + velocityVector)

Awesome! As it turns out, the fx is 90 degrees rotated from that, so the actual code is:


velocity = (part.Rigidbody.GetPointVelocity(part.transform.po sition) + Krakensbane.GetFrameVelocityV3f() - Krakensbane.GetLastCorrection() * TimeWarp.fixedDeltaTime);
fx.transform.LookAt (part.transform.position + velocity);
fx.transform.Rotate (90, 0, 0);

Whew. Two more questions (hopefully):

1. I try to do:


new EventReport (FlightEvents.CUSTOM, part, part.partInfo.title, "g-force", 0, " exceeded g-force tolerance."));

but instead, it just says "<Part> crashed into g-force". If I don't include the second string, it just says "<Part> crashed into unknown object."

How do I add a custom line to the event log that says "<Part> exceeded g-force tolerance."?

2. I need to know whether a parachute is deployed or packed, or whether a shielded docking clamp is open or closed. How do I determine this from the ModuleAnimateGeneric or ModuleParachute?

Share this post


Link to post
Share on other sites

Is there an easy fix to modules that don't save when you focus on another craft?

For example, a module with an animated door. You open it, it stays open. You go to a satellite you have to alter its course or whatever and when you return to the module the door is closed...only if you right click on the module, it claims it's open so you have to close and open it again.

I hope that made sense. :-)

Share this post


Link to post
Share on other sites

Assuming the Module in question exposes both it's state and functions for changing it then you should be able to save it yourself easily enough. The modules on a part are listed in part.Modules.

Share this post


Link to post
Share on other sites

Stupid newbie here again with another silly question.

Plugins like MechJeb can calculate mission trajectories because it can discover the maxThrust and the specific impulse from atmosphereCurve from the vessel's parts that include ModuleEngines. The MechJeb FuelFlowSimulation.cs module does this in its FuelNode constructor.

MechJeb and other plugins will not work with my Orion Drive mod, because it is not a ModuleEngines. The rocket fuel system used in KSP does not work very well with the nuclear bomb "fuel" used by Orion. Therefore MechJeb will examine an Orion equipped vessel and fail to find any ModuleEngines.

But I do not want to add to the Orion's part.cfg file a fake entry like

MODULE
{
name = ModuleEngines
minThrust = 0
maxThrust = 60
atmosphereCurve
{
key = 0 800
key = 1 220
}

}

just to feed the proper values to MechJeb. As far as I know, if you include the module, all of its code comes along with it. I'm sure it will interfere with the Orion code.

So my question is: how can I spoof MechJeb and other similar plugins?

The only way I can see is to include the ModuleEngines module, stuff my Orion values into its variables, and somehow permanently disable the ModuleEngines code. But I have no idea how to do that.

Edited by nyrath

Share this post


Link to post
Share on other sites
Stupid newbie here again with another silly question.

Plugins like MechJeb can calculate mission trajectories because it can discover the maxThrust and the specific impulse from atmosphereCurve from the vessel's parts that include ModuleEngines. The MechJeb FuelFlowSimulation.cs module does this in its FuelNode constructor.

MechJeb and other plugins will not work with my Orion Drive mod, because it is not a ModuleEngines. The rocket fuel system used in KSP does not work very well with the nuclear bomb "fuel" used by Orion. Therefore MechJeb will examine an Orion equipped vessel and fail to find any ModuleEngines.

But I do not want to add to the Orion's part.cfg file a fake entry like

MODULE
{
name = ModuleEngines
minThrust = 0
maxThrust = 60
atmosphereCurve
{
key = 0 800
key = 1 220
}

}

just to feed the proper values to MechJeb. As far as I know, if you include the module, all of its code comes along with it. I'm sure it will interfere with the Orion code.

So my question is: how can I spoof MechJeb and other similar plugins?

The only way I can see is to include the ModuleEngines module, stuff my Orion values into its variables, and somehow permanently disable the ModuleEngines code. But I have no idea how to do that.

Easy!

On the launchpad, when you do OnStart, do the following check:


if(part.Modules.Contains("ModuleEngines"))
part.Modules.Remove(part.Modules["ModuleEngines"]);

Bam! ModuleEngines is gone on the launchpad, but still exists in the VAB.

Share this post


Link to post
Share on other sites
Easy!

On the launchpad, when you do OnStart, do the following check:


if(part.Modules.Contains("ModuleEngines"))
part.Modules.Remove(part.Modules["ModuleEngines"]);

Bam! ModuleEngines is gone on the launchpad, but still exists in the VAB.

Ummmm, but during the mission will my module still show up when MechJeb goes looking for ModuleEngines?

If so, that will be a most elegant solution.

Share this post


Link to post
Share on other sites

Does anyone know whether it is possible via GameDB, PartLoader or any other way to actually get the mod folder in /GameData/ (the name of the mod) of certain part name not having an instanced part available?

Tried digging through the API for some hours now but I can't find any way to get even close to those folder paths as all properties of the AvailablePart in the PartLoader are useless for this purpose.

Hardcoding stock part names etc would be kinda annoying.

Many thanks in advance!

Share this post


Link to post
Share on other sites
Does anyone know whether it is possible via GameDB, PartLoader or any other way to actually get the mod folder in /GameData/ (the name of the mod) of certain part name not having an instanced part available?

AvailablePart.partUrl or partPath don't contain the path?

Share this post


Link to post
Share on other sites
AvailablePart.partUrl or partPath don't contain the path?

Sadly neither the available parts list in the PartLoader nor getting the parts by name through the PartLoader fills any fields beside the name. I'm doing this in the editor scene btw.

Share this post


Link to post
Share on other sites
Override is just a way to ensure the virtual function you are trying to override exists. C/C++ doesn\'t have this declaration as it\'s more to for 'safety' reasons than anything else (there\'s been some modifications in C++11, only thing that comes to top of my head is 'final' to ensure a virutal function doesn\'t get overridden after that class). Although C# still needs the override key word otherwise you might get a warning or error?

Override has specific functionality with regard to polymorphism. You can only override virtual functions in the base class. An overridden function will be called on any instance of the derived class despite the type of the instance variable. If you don't override (and you're supposed to use the keyword "new" if you don't), the type of the instance variable will used to determine which version of the method you call:

[COLOR="#0000FF"]class[/COLOR] Base
{
[COLOR="#0000FF"]public virtual void[/COLOR] F() {}
}
[COLOR="#0000FF"]class[/COLOR] DeriveAndHide: Base
{
[COLOR="#0000FF"]public new void[/COLOR] F() {}
}
[COLOR="#0000FF"]class[/COLOR] DeriveAndOverride: Base
{
[COLOR="#0000FF"]public override void[/COLOR] F() {}
}

[COLOR="#0000FF"]class[/COLOR] Tester
{
[COLOR="#0000FF"]public void[/COLOR] Main()
{
[COLOR="#0000FF"]public[/COLOR] Base Test1 = [COLOR="#0000FF"]new[/COLOR] DeriveAndHide();
[COLOR="#0000FF"]public[/COLOR] DeriveAndHide Test2 = [COLOR="#0000FF"]new[/COLOR] DeriveAndHide();
[COLOR="#0000FF"]public[/COLOR] Base Test3 = [COLOR="#0000FF"]new[/COLOR] DeriveAndOverride();
Test1.F(); [COLOR="#008000"]// calls Base.F()[/COLOR]
Test2.F(); [COLOR="#008000"]// calls DeriveAndHide.F()[/COLOR]
Test3.F(); [COLOR="#008000"]// calls DeriveAndOverride.F(;[/COLOR]
}
}

Edited by Mr Shifty

Share this post


Link to post
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.