Jump to content

Why You Shouldn't Let Me Write Plugins


Recommended Posts

A picture is worth a thousand words—or in this case, screams: http://imgur.com/a/5AqUY

How? How did you manage this with a part module loader?

You must have had to do some weird voodoo shit. But... wow. That plugin would be very cool.

Could you upload it?

Also, what happens when you change to a ship without the part? Does the mun go back to where it usually is?

Link to comment
Share on other sites

How? How did you manage this with a part module loader?

You must have had to do some weird voodoo shit. But... wow. That plugin would be very cool.

Could you upload it?

Also, what happens when you change to a ship without the part? Does the mun go back to where it usually is?

It depends. If your scene persists (because you still have a ship in it) the Mun will stay on its new orbit. However, if you remove all the ships and then launch one, it resets. I\'m sure there\'s a way around that, but I haven\'t bothered to find it yet.

I just used the part loader to hook onto the onPartActivate event. From there, it\'s actually pretty easy to get access to the celestial bodies. (You can look at what your ship is orbiting, or get a list of all celestial bodies from the FlightGlobals class, etc.) Modifying the orbits is less easy, and I haven\'t yet had any luck adding a new Mun... but I\'ll keep trying!

Link to comment
Share on other sites

It depends. If your scene persists (because you still have a ship in it) the Mun will stay on its new orbit. However, if you remove all the ships and then launch one, it resets. I\'m sure there\'s a way around that, but I haven\'t bothered to find it yet.

I just used the part loader to hook onto the onPartActivate event. From there, it\'s actually pretty easy to get access to the celestial bodies. (You can look at what your ship is orbiting, or get a list of all celestial bodies from the FlightGlobals class, etc.) Modifying the orbits is less easy, and I haven\'t yet had any luck adding a new Mun... but I\'ll keep trying!

Could you post the plugin + source here? I\'d love to learn from it.

Link to comment
Share on other sites

// These are only guaranteed when you\'re in Kerbin orbit (or on the launchpad)
CelestialBody kerbin = FlightGlobals.getMainBody();
CelestialBody mun = kerbin.orbitingBodies[0];
Orbit ko = kerbin.orbit;
Orbit mo = mun.orbit;
kerbin.rotates = false; // You can change rotation speeds, too

/* If the Mun comes too close, it actually sucks you into its SOI
* even if you\'re landed on Kerbin. This value is a bit extreme, but
* it keeps us on the safe side. */
mun.sphereOfInfluence = 0;

// Bring the Mun to us!
mo.semiMajorAxis = kerbin.Radius + mun.Radius + 80000;
mo.semiMinorAxis = kerbin.Radius + mun.Radius + 80000;

/* Use a try/catch block because otherwise this method throws an
* exception and the rest of your plugin can\'t execute. */
try
{
mo.RecalculateOrbit(kerbin);
}
catch { }

[EDIT] This is why it\'s a good reason to keep your orbit at least as high as the radii combined: http://i.imgur.com/k74du.png

Link to comment
Share on other sites

Couldn\'t you theoretically not even make it have to be a vessel? Just by calling it in the 'OnPartLoad' function, you could just perform the necessary actions and abort the function before the 'part' loads, I think.

Link to comment
Share on other sites

Couldn\'t you theoretically not even make it have to be a vessel? Just by calling it in the 'OnPartLoad' function, you could just perform the necessary actions and abort the function before the 'part' loads, I think.

Yes, but like I said, the scene sometimes resets. There\'s probably a way to consistently run some kind of initialization code, but I haven\'t played with it too much yet.

Link to comment
Share on other sites

Well you can flag a game object as persistent in unity so it exists between states. It essentially prevents the automated monobehavior call to OnDestroy. You just need to set the DontDestroyOnLoad(transform.gameObject); method.

Furthermore you can access monobehavior directly, as all unity objects derive from this base.

Specifically the functions you would to override are monobehavior,

Start() // Called before any objects execute their update code

Update() // Called every from after all start commands have completed for the scene.

http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Start.html

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Update.html

Link to comment
Share on other sites

Doooo post code!

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

public class InterplanetaryPod : CommandPod
{
protected override void onPartStart()
{
CelestialBody kerbin = null;
CelestialBody mun = null;
CelestialBody kerbol = null;
Orbit ko = null;
Orbit mo = null;

foreach (CelestialBody body in FlightGlobals.Bodies)
{
if (body.name == 'Kerbin')
{
kerbin = body;
MonoBehaviour.print('Found Kerbin!');
}
}

ko = kerbin.orbit;
mo = mun.orbit;
mun = kerbin.orbitingBodies[0];
kerbol = ko.referenceBody;

mo.referenceBody = kerbol;

mun.tidallyLocked = false;
mun.rotationPeriod = 36000.0f;
mo.referenceBody = kerbol;
mo.semiMajorAxis = 5750000000.0f;

try
{
mo.RecalculateOrbit(kerbol);
}
catch { }

base.onPartStart();
}
}

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