Jump to content

Adding an resource cost to modules that don't have them


Recommended Posts

I understand the basic premise, (this is using module manager, but that should be irrelevant as far as scope is concerned)


@PART[mk1pod]
{
@MODULE[ModuleCommand]
{
RESOURCE
{
name = ElectricCharge
rate = 0.007
}
}
}

Works as expected, but that's apparently only because it is defined within that module, a la for probes? I tried adding a passive electricity cost to SAS,



@MODULE[ModuleSAS]
{
RESOURCE
{
name = ElectricCharge
rate = 0.005
}
}

But as someone may already know, it had no effect. SAS doesn't have/use a charge. Is this the sort of change that requires a plug-in (or have I made a gaff)? And a follow-up question, is there a plug-in that has this functionality? The basic premise is simply I'd like SAS to consume a charge when on, and none when off. The scope of this question doesn't include a how to write plug-ins, but if you have some basic suggestions as to where to start, I won't turn it down.

Edited by Hyomoto
Link to comment
Share on other sites

Cfg files are settings files. They only tap into what the code already supports. If the module doesn't support "adding a resource cost by adding a confignode" then adding a confignode won't even be noticed by the module.

Also, plugins don't really work like that. You could make your own SAS module that has that feature, or you could make a separate module that detects when SAS is on and draws electricity itself, but you can't really make plugins to modify other classes on the fly.

There are some *great* tutorials stickied in this very forum btw; to get started you need a development environment (say, Visual C# Express if you're using Windows) and you need to set it up for KSP (guide in this forum and/or on the KSP wiki). Then you need to, well, learn enough C# to get by--there are tons of "getting started in C#" tutorials on the internet. Finally, to get started in KSP C# writing, you can look over TaranisElsu's wonderful examples in this forum.

Link to comment
Share on other sites

...but you can't really make plugins to modify other classes on the fly.

Well, this is not quite true. I think you could unload a stock class, inject some IL code into it and then reload it. But don't do it. It's a Bad Thing.

Link to comment
Share on other sites

Well, this is not quite true. I think you could unload a stock class, inject some IL code into it and then reload it. But don't do it. It's a Bad Thing.

IL injection works statically (i.e. injection happens before KSP is launched) but as far as I know, you can't inject IL into an existing class definition for a PartModule. However, you could probably make a new class definition with injected code and swap out existing type references for the new one.

Link to comment
Share on other sites

ModuleSAS doesn't do anything interesting since 0.20, I believe. You want ModuleReactionWheel.

It should be possible to derive a class from ModuleReactionWheel and just tack the resource consumption functionality on, though, then do a global ModuleManager replacement. It doesn't have an OnUpdate, so you could in theory do your stuff in there, though you would need to do your own time tracking -- that would allow you to get away relatively lightly.

It's probably more future-proof and compatibility-proof to create a module that seeks out the ModuleReactionWheel on the same pod it's attached to, checks if it's active (thatReactionWheelModule.State == ModuleReactionWheel.WheelState.Active) and if it is, consume a resource (part.RequestResource(whatever,amount)), then attach it with ModuleManager to everything that has ModuleReactionWheel.

Link to comment
Share on other sites

Thanks Mihara, but unfortunately SAS is exactly what I'm interested in. But I think I understand what you are saying, in Ruby you could simply alias your method and then call the alias to essentially 'stack' modifications to a basic method. That's one of the most interesting languages I've tinkered around with, but I am just a hobbyist programmer and C# makes sense, but its a bit complex. I liked QBasic where it was either a string or a number :P Trying to decide if a number is supposed to be a float or a double or a decimal makes me realize I need to take a class.

On the topic material though, the reaction wheels don't power the RCS or the ailerons. I just looking to tinker, so as I see it, Squad made a gaff when they basically combined SAS and the reaction wheels. In doing so, they basically ensured the player will always enjoy a handicap. I understand that KSP, at its core, has a steep learning curve and they have no desire to pile additional difficulty onto the player. However, in doing so they have created a reliance on a system. I would like to separate them for personal use, and luckily the two are still separate internally, and so it's exceedingly easy to do. All I have to do is strip the reaction wheels out of the command pods. The end. The reaction wheels already show up later in the science tree, so it's not really all that big of a change to make. However, I would also like to take SAS out of the command pod. And for that reason, I was wondering if there was a way to make it require a resource. It may not do anything specifically on its own, but it does govern areas of the craft and can be turned on and off.

Without getting too much into design theories, RCS is a finite resource, but electricity is infinite (near). Therefore based on cost and weight, it is inherently more productive to include reaction wheels over RCS. The reaction wheels have the added benefit of not causing and unintended changes in velocity. They are really the most 'arcade' part of the game. I'm not against them, I just think they should be unlocked like RCS and not simply given to the player to create a reliance on first thing. In this case, not given to me. So suffice it to say that I feel a good way to handle some of the problem of infinite electricity is simply to allow the player to spend it on more things and therefore create a situation where the player is interested in RCS because it doesn't cost electricity.

Anyways, thanks for the replies. Mihara, I think you are right. It would be easiest is to make a module that checks if SAS is on and applies a resource drain. I would be surprised if KSP's stock modules use separate code, it should be possible to hook into something like that, right?

Edited by Hyomoto
Link to comment
Share on other sites

Thanks Mihara, but unfortunately SAS is exactly what I'm interested in.

It literally doesn't do anything and has no on/off setting. If you want to consume power while the "SAS" lamp is lit up, your job is actually simpler, because you don't need to locate the SAS module, but instead want to ask the game about the state of the global SAS toggle:


class MySASPowerConsumer: PartModule
{

private int sasGroupNumber;
[KSPField]
public string resourceName;
[KSPField]
public float resourceAmount;

public void Start()
{
sasGroupNumber = BaseAction.GetGroupIndex(KSPActionGroup.SAS);
}

public override void OnFixedUpdate()
{
if (FlightGlobals.ActiveVessel.ActionGroups.groups[sasGroupNumber]) {
part.RequestResource(resourceName,resourceAmount);
}
}
}

I didn't compile it, but I think that's all it would take really. :)

However, I would also like to take SAS out of the command pod. And for that reason, I was wondering if there was a way to make it require a resource. It may not do anything specifically on its own, but it does govern areas of the craft and can be turned on and off.

I.e. you want to take the PID controller out? Well, you could also have your module just FlightGlobals.ActiveVessel.ActionGroups.groups[sasGroupNumber] = false; instead and SAS would just immediately turn off when anything enabled it. :) But I don't recommend it, because:

Without getting too much into design theories, RCS is a finite resource, but electricity is infinite (near).

ModuleReactionWheels can consume any resource, including monopropellant. Why don't you make it do that instead?

Link to comment
Share on other sites

So then I have to ask this, if it does nothing, when did that start? I've been under the impression of two things, one that it controls the amount of force being used by all controls to maintain a heading, and two, that it can be turned off and on. If you are saying that is untrue, I have to point out that you can turn off the reaction wheels in your pod and still use SAS to control, for instance, ailerons. Now, I'll admit there is no command pod that has SAS and no reaction wheels, it is possible they do the controlling but I'd have to test it out myself before I could really believe it. I'll try ripping SAS out of a pod and leaving the wheels to see for myself.

Also, thanks for the example code. I'll give your plugin a try, and yes, I don't see why it wouldn't work. It should be a very simple operation. :) Right now I'm also working on that all-IVA mission to the Mun featuring your monitors, so I'll let you know how it turns out as it comes up!

Edited by Hyomoto
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...