regex Posted October 1, 2013 Share Posted October 1, 2013 Is there a "hello world" style tutorial example of a mod out there somewhere that walks a new person through the steps?Best thing to do is comb through other's code. That's one of the best reasons for having the open code policy on this site (of course, be careful about copy-pasta and sharing, respect licenses). If you want to check out a quick and dirty mod my source can be found at the link below. You can probably puzzle out how to display "Hello World" in a window from that code.Then there's this: http://docs.unity3d.com/Documentation/ScriptReference/ , which is quite helpful. Specifically check out the MonoBehaviour because all plugins (not parts, not sure how to do those...) extend that class. There's an IRC chat channel #kspmodders, or something, I don't chat. There's also a forum that goes along with it: http://www.kspmodders.com/forum/ but there aren't many people there just yet. Oh, and there's this: https://github.com/Anatid/XML-Documentation-for-the-KSP-API , which has personally been an excellent reference but might be out of date, not sure. Quote Link to comment Share on other sites More sharing options...
Dunbaratu Posted October 1, 2013 Share Posted October 1, 2013 (edited) Best thing to do is comb through other's code. That's one of the best reasons for having the open code policy on this site (of course, be careful about copy-pasta and sharing, respect licenses). If you want to check out a quick and dirty mod my source can be found at the link below. You can probably puzzle out how to display "Hello World" in a window from that code.Then there's this: http://docs.unity3d.com/Documentation/ScriptReference/ , which is quite helpful. Specifically check out the MonoBehaviour because all plugins (not parts, not sure how to do those...) extend that class. There's an IRC chat channel #kspmodders, or something, I don't chat. There's also a forum that goes along with it: http://www.kspmodders.com/forum/ but there aren't many people there just yet. Oh, and there's this: https://github.com/Anatid/XML-Documentation-for-the-KSP-API , which has personally been an excellent reference but might be out of date, not sure.All those links are for what to do after it's installed. I don't even know what to install.Ah finally: After a rather long time googling and finding other links that all assumed I already had everything set up I finally found what I was looking for here:http://wiki.kerbalspaceprogram.com/wiki/Creating_your_first_module Edited October 1, 2013 by Steven Mading Quote Link to comment Share on other sites More sharing options...
Matrixmage Posted October 1, 2013 Share Posted October 1, 2013 I tried to post a dedicated thread, but didn't get the response I needed, so here's to trying it on this thread.What exactly do you do with a class that extended part. I know a class that extends part module is a part module, but I need to use something in part, and I just don't really know how.Thanks for any help,Matrixmage Quote Link to comment Share on other sites More sharing options...
Okreeks Posted October 1, 2013 Share Posted October 1, 2013 Hi guys, could some one help me out.I cant get it to work. Dont have the access to the script atm...But this is what I'm trying to do (roughly):import "all the right stuffs to get it to work";class test extends partmodule (or something){public contructor(){//do some constructor stuff//This show up in the debug/alt F2 so the main set-up works print("welcome"); public CelestialBody celestial = new CelestialBody(); public string name;//try one print(celestial.name/title());//try two name = celestial.getTitle/Name(); print(name);}}As seen above I'm trying to get the properties from a planet.But it seems im not doing it right. The prints show that the values are either '0' or 'unnamed'.My goal is to get the values from a planet and/or change these.More specific the values from either the current planet I'm on or just Kerbin.Don't have much experiance but I enjoy doing this, so a gentle shuff or pointer into the right direction would be enough.Ill try to supply the code when available to me again.Thanks in advance and kind regards,Okreeks Quote Link to comment Share on other sites More sharing options...
Crabsticks Posted October 1, 2013 Share Posted October 1, 2013 (edited) Hi all,As an exercise to learn C# (among other things, eg, modeling & texturing) I've been working on making a working stargate in the evenings while watching stargate .While I've got it mostly figured out and coded I've run into a problem; I have no idea how to get the vector/position of a part.part.transform.localPosition;part.rigidbody.position;part.transform.position;part.localRoot.transform.position;part.GetReferenceTransform().positionThe above all seem to provide the position of the vessel, not the part. So my puddle jumpers and gate travelers all jump to the centre of the gate ship, not the gate, which causes....problems...So what am I missing? How do I find the position of the part? Thanks in advance.@Okreeks: I might be able to help. Looking at your example code you seem to be creating a new Celestial body but not properly intialising it and giving it a name, so your print statements are correct when they return 0 or Unnamed.Try printing the following, it'll get the name of the celestial body whose SOI your vessle is currently in: vessel.mainBody.nameHope that works and you can work it out from there. Edited October 1, 2013 by Crabsticks Quote Link to comment Share on other sites More sharing options...
Faark Posted October 1, 2013 Share Posted October 1, 2013 (edited) [..]You want to call a function in Part or change a variable there? All part-modules have a reference to their part. Just use (partModule or this).part.what_ever_you_want_to_do();[..]Your script creates a new planet object and prints name & title of this newly created object. You did not change those properties, so ofc they are still the default values of a newly created object. You don't want to create a new object but instead get a reference to an existing planet. Check out the anatid doc linked few posts above, especially the part about FlightGlobals.[..]Are you sure you ruled out part.transform.position? From my understanding this should be the local offset from the parent game object. So you should be able to somehow calculate the actual pos with it. Unity should also offer helpers for that, but I'm definitively no unity expert^^ Edited October 1, 2013 by Faark Quote Link to comment Share on other sites More sharing options...
Okreeks Posted October 1, 2013 Share Posted October 1, 2013 (edited) Thank you guys. Looking back its quite obvious. xDI'm trying to make a part that gives me a window with information on the planets. And chnge the gravity and atmosphere. Any hints towards that?EDIT: I have to acces and change: CelestialBody: "public double gMagnitudeAtCenter;"Some one can tell me how?Again, thnx! Edited October 1, 2013 by Okreeks Quote Link to comment Share on other sites More sharing options...
Crabsticks Posted October 1, 2013 Share Posted October 1, 2013 Thanks Faark, I goofed; the vessel I was using to test on had the desired part as the root part which explains why the part and vessel position were the same.@Okreeks: I've no experience with Celestial bodies but have you tried the following?CelestialBody planet = vessel.mainBody;planet.gMagnitudeAtCenter = x;planet.CBUpdate ();I have no idea if it'll work Quote Link to comment Share on other sites More sharing options...
Vulkan Posted October 1, 2013 Share Posted October 1, 2013 Afternoon all,I'm getting a bit confused about polling for connected resources, specifically intake air. I've written a module that should be displaying the available intake air as a prelude to using this value elsewhere. In OnUpdate I have: var resources = new List<PartResource>(); this.part.GetConnectedResources(IntakeAirID, resources); availableAir=resources[0].amount; Air = string.Format("{0:0.000}",availableAir);No if I put one air intake anywhere on a rocket and a part to display the 'Air' string in a window then the value in the window tracks the IntakeAir value in the top right. However, if I have more than one air intake then it only gets the value for the first intake placed. This means that a rocket with four radial intakes will have 1.6 IntakeAir on the launchpad according to the resources readout but only 0.4 is being found by GetConnectedResources.So how do I get the true value for the IntakeAir that is present onthe craft? Quote Link to comment Share on other sites More sharing options...
xEvilReeperx Posted October 1, 2013 Share Posted October 1, 2013 Afternoon all, var resources = new List<PartResource>(); this.part.GetConnectedResources(IntakeAirID, resources); [B]availableAir=[SIZE=4]resources[0].amount[/SIZE];[/B] Air = string.Format("{0:0.000}",availableAir);You're only retrieving the first result in this snippet. If this is the code you're using, then you've forgotten to add all the other results in resources Quote Link to comment Share on other sites More sharing options...
Vulkan Posted October 1, 2013 Share Posted October 1, 2013 (edited) You're only retrieving the first result in this snippet. If this is the code you're using, then you've forgotten to add all the other results in resourcesThat's it. Many thanks. For some reason I was thinking that the list was if I wanted to poll more than one resource.Working code for anyone in the future:var resources = new List<PartResource>(); this.part.GetConnectedResources(IntakeAirID, resources); availableAir=resources.Sum(r => r.amount); Edited October 1, 2013 by Vulkan forgot the code Quote Link to comment Share on other sites More sharing options...
UbioZur Posted October 2, 2013 Share Posted October 2, 2013 (edited) Btw, someone might find this interesting for setting up a development environment. It skips the main menu and automatically loads a specific quicksave instead. All you have to do is to add it to your mod.dll (do not forget to remove it for shipping^^) and change the SaveFolder = "...", to match your save. [KSPAddon(KSPAddon.Startup.MainMenu, false)] public class Debug_AutoLoadQuicksaveOnStartup: UnityEngine.MonoBehaviour { public static bool first = true; public void Start() { if (first) { first = false; HighLogic.SaveFolder = "Dev"; var game = GamePersistence.LoadGame("quicksave", HighLogic.SaveFolder, true, false); if (game != null && game.flightState != null && game.compatible) { FlightDriver.StartAndFocusVessel(game, game.flightState.activeVesselIdx); } CheatOptions.InfiniteFuel = true; } } }Thanks for the info Faark,Is there a way to not have it start in the SPH/VAB or even just the space center? instead of an active vessel?If found a wayI used HighLogic.LoadScene(GameScenes.EDITOR); instead of the FlightDriver line. Edited October 2, 2013 by UbioZur answer my own question Quote Link to comment Share on other sites More sharing options...
Razara Posted October 4, 2013 Share Posted October 4, 2013 I'm trying to write a plugin that will send and receive data to KSP via a serial connection. Whenever I initialize the SerialPort though the plugin does not seem to load. If I remove the serialPort line, the debug window spews out "test" as expected. I know people have done this before, but I'm not sure how.[KSPAddon(KSPAddon.Startup.Flight,false)] public class PluginTutorial : MonoBehaviour { private SerialPort serialPort; public void Awake() { serialPort = new SerialPort("COM4", 9600); } public void Start() { } public void Update() { print("test"); }} Quote Link to comment Share on other sites More sharing options...
Faark Posted October 4, 2013 Share Posted October 4, 2013 A SerialPort class is not included in KSPs runtime. Jop, that sadly is no standard .net 3.5 runtime. You should be able to verify this in your KSP.log, pretty near to the beginning. I stumbled about this a few times as well, especially when using 3rd party libraries. The most promising approach is to include the source yourself, for example by extracting it from mono. Its a bunch of copy-paste work, though. You might also find other 3rd party libs... but make sure they don't need sth KSP doesn't have as well... Quote Link to comment Share on other sites More sharing options...
Ashtoruin Posted October 4, 2013 Share Posted October 4, 2013 (edited) Okay, I'm a Comp Sci major so I know coding, but I've never actually worked on a mod for a game before, That being said I'm taking baby steps and figuring things out one at a time. I've been looking at how ships are saved from the VAB and noticed that they save the parts which doesn't include the fuel levels. So if I wanted to have a GUI with a slider for a tank to set it to half full, I would have to take the value from the slider and save it to be called when the rocket hits the launch pad at which point it could load the fuel level %number and edit the tank to the appropriate fuel levels. From creeping on other mods source code I think that this would be done with [KSPFIELD]public float fuel_adjust_percent = fuel_adjust_slider_value;And then some on load function I still have to figure out.So, I guess any pointers would be awesome or even just let me know if that sounds somewhat correct. Edited October 4, 2013 by Ashtoruin Quote Link to comment Share on other sites More sharing options...
Diazo Posted October 5, 2013 Share Posted October 5, 2013 Not something I've done myself, but take a look at the TAC Fuel Balancer mod for code relating to controlling the level of fuel in fuel tanks to get you started.The Spaceport link for the mod here.D. Quote Link to comment Share on other sites More sharing options...
TaranisElsu Posted October 5, 2013 Share Posted October 5, 2013 Okay, I'm a Comp Sci major so I know coding, but I've never actually worked on a mod for a game before, That being said I'm taking baby steps and figuring things out one at a time. I've been looking at how ships are saved from the VAB and noticed that they save the parts which doesn't include the fuel levels. So if I wanted to have a GUI with a slider for a tank to set it to half full, I would have to take the value from the slider and save it to be called when the rocket hits the launch pad at which point it could load the fuel level %number and edit the tank to the appropriate fuel levels. From creeping on other mods source code I think that this would be done with [KSPFIELD]public float fuel_adjust_percent = fuel_adjust_slider_value;And then some on load function I still have to figure out.So, I guess any pointers would be awesome or even just let me know if that sounds somewhat correct.Not something I've done myself, but take a look at the TAC Fuel Balancer mod for code relating to controlling the level of fuel in fuel tanks to get you started.The Spaceport link for the mod here.D.I allow setting the amount of fuel (while still prelaunch) in a part here:https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/MainWindow.cs#L359And here is an example of where I change it during flight:https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/FuelBalanceController.cs#L404vessel.parts gives you a List<Part>, part.Resources gives you a List<PartResource>, and PartResource.amount is the current amount. Also take a look at: https://github.com/Anatid/XML-Documentation-for-the-KSP-API/blob/master/Assembly-CSharp.xml#L1005 . Quote Link to comment Share on other sites More sharing options...
Ashtoruin Posted October 5, 2013 Share Posted October 5, 2013 I allow setting the amount of fuel (while still prelaunch) in a part here:https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/MainWindow.cs#L359And here is an example of where I change it during flight:https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/FuelBalanceController.cs#L404vessel.parts gives you a List<Part>, part.Resources gives you a List<PartResource>, and PartResource.amount is the current amount. Also take a look at: https://github.com/Anatid/XML-Documentation-for-the-KSP-API/blob/master/Assembly-CSharp.xml#L1005 .Alright, I get what that's doing I think.Is it possible to test if a resource is enabled? ie Say I want to add Kethane Support, can I check to see if Kethane is a Resource and have it return a true/false? I've been looking through the documentation, but sometimes its hard to understand what exactly its saying :/ Quote Link to comment Share on other sites More sharing options...
TaranisElsu Posted October 5, 2013 Share Posted October 5, 2013 (edited) Alright, I get what that's doing I think.Is it possible to test if a resource is enabled? ie Say I want to add Kethane Support, can I check to see if Kethane is a Resource and have it return a true/false? I've been looking through the documentation, but sometimes its hard to understand what exactly its saying :/There is a PartResourceLibrary that you can ask for the ID# of a resource. You pass the name as a string, i.e. "Kethane". I forget what it does if it cannot find the resource, but I think it returns -1 instead of throwing an exception. That would tell you if the resource has been defined.See: https://github.com/Anatid/XML-Documentation-for-the-KSP-API/blob/master/Assembly-CSharp.xml#L1746Example: https://github.com/taraniselsu/TacLifeSupport/blob/master/Source/Settings.cs#L67Edit: Scratch that. It is even simpler. You can get access to the PartResourceDefinitionList, which has a Contains() method:PartResourceLibrary.Instance.resourceDefinitions.Contains("Kethane")Not sure why it doesn't show up in the XML documentation. Edited October 5, 2013 by TaranisElsu Too easy Quote Link to comment Share on other sites More sharing options...
Razara Posted October 9, 2013 Share Posted October 9, 2013 A SerialPort class is not included in KSPs runtime. Jop, that sadly is no standard .net 3.5 runtime. You should be able to verify this in your KSP.log, pretty near to the beginning. I stumbled about this a few times as well, especially when using 3rd party libraries. The most promising approach is to include the source yourself, for example by extracting it from mono. Its a bunch of copy-paste work, though. You might also find other 3rd party libs... but make sure they don't need sth KSP doesn't have as well...I did figure out that was the problem after moving the initialization of the SerialPort into the update function, but I have not been able to resolve it. Any chance you could give me a pointer on how I would go about getting the source from mono? Am I to basically copy the SerialPort class into my own program? Quote Link to comment Share on other sites More sharing options...
Faark Posted October 9, 2013 Share Posted October 9, 2013 Am I to basically copy the SerialPort class into my own program?I don't see another way... so either that or you find a third 3rd library that does not use System.IO.Ports. Here is the SerialPort source and where you will find other missing dependencies. This appears to be their license, btw. Quote Link to comment Share on other sites More sharing options...
NathanKell Posted October 10, 2013 Share Posted October 10, 2013 So, before I headdesk any more about this--has anyone had success in programmatically overriding a part's FX?I've looked at part.SaveEffects(ConfigNode node) [hoping then to change the configs, and then call part.LoadEffects(node)] but that didn't seem to work right--it started playing all effects.I'd really rather not have to manually create emitters, I just want to change a part's existing FX defines.(Disclosure: this is so engine FX can be changed based on their fuels) Quote Link to comment Share on other sites More sharing options...
UbioZur Posted October 11, 2013 Share Posted October 11, 2013 Hey Guys,in the cfg of a part there is that line:module = Partcan be Part, ControlSurface, Strut, WingletIs there a why I can access that value in game?I couldn't find it in the stored config file from GameDatabase.Instance.GetConfigsor any variable of Parts or AvailablePartIt looks like it's from pre .15. Quote Link to comment Share on other sites More sharing options...
Chrison Posted October 12, 2013 Share Posted October 12, 2013 Any idea of how to hack into the ship editor? I was thinking about making some kind of economy mod (like actually add cash to the game and make use of the cost attribute of parts) but I have no idea on how to start, as there is no documentation on the VAB/SPH functionality nor does there seem to be any official api on that part of ksp. Quote Link to comment Share on other sites More sharing options...
Faark Posted October 12, 2013 Share Posted October 12, 2013 [..]The part class contains a "List<FXGroup> fxGroups" field. Have you checked it out? You should be able to edit simply edit it for the prefab via PartLoader.LoadedPartsList[some_index].partPrefab.fxGroupsIt looks like it's from pre .15.Yes, back from the time when there wasn't any PartModules and part / its derived types contained all the actual behavior. It by the way looks like all those old & "dead" types are still in the released 0.21 assembly. Though those few parts you mentioned appear to still use a custom type derived from Part. So using c#'s primitive GetType() on Part should help you.[..]Some ideas:- Search for editor mods and inspect their source. There are a bunch of Part Filters, for example.- Get into KSP's internals like us others. The object browser does have quite some hits for "Editor"- The KSP API doc does in fact contain an EditorLogic class that has an "public ShipConstruct ship;" that might be useful for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.