Jump to content

simple if/then code in part cfgs


Recommended Posts

A friend and I were looking at the ResGen module (http://forum.kerbalspaceprogram.com/threads/28895-0-20-2-Resource-Generation-Module-(0-28)), and it's so very close to something I would love: mainly a way to do real if/then evaluations in a module.

Is anyone working something similar, or maybe working toward allowing lua or javascript in a part's cfg (via a special module). It would much easier to write necessary code in a part's cfg than to roll custom dlls

Edited by seanth
Link to comment
Share on other sites

Perhaps an example of what you are looking for?

Taking a quick look at the ResGen module you linked, that does not implement if/then evaluations in the part.cfg that I can see.

The ResGen module looks at the part.cfg to get the resource time and consumption/production rates yes, but all the logic is calculated by the ResGen .dll, not in the part.cfg.

Which makes sense to me. The part.cfg describes the properties of the associated part. A part's intrinsic properties don't change.

A solar panel generates X amount of electricity per lumen of light that it is exposed to, this value is in the part.cfg (Intrinsic value)

The game then calculates how many lumens of light are hitting the solar panel based on its distance from the sun and produces the appropriate amount of electricity. (Logic not in the part.cfg)

At least that's how I see things.

D.

Link to comment
Share on other sites

Perhaps an example of what you are looking for?

An example.... Hmmm. Well, let's imagine a new sort of Air intake. Let's further assume the % of oxygen on Laythe is different than on Kerbin. I think it would be neat to do something like:

MODULE{

name = magicalCodeAndLogicThingy

INPUT{

name = IntakeAir

rate = 1.0

}

OUTPUT{

if(planet==Kerbin){

name = Oxygen

rate = 0.21

}

if(planet==Laythe){

name = Oxygen

rate = 0.15

}

}

}

Just as an example. Or maybe a way to nest Modules. So, for example, you could use different powercurves for ModuleDeployableSolarPanel on a per planet basis to reflect how cloudy the atmosphere is.

These are just examples off the top of my head, mind you.

Link to comment
Share on other sites

To my awareness, the config loader really isn't set up to have this sort of functionality added. It could be done by specifying some sort of custom scripting language for use in a particular field within a module, and then using a plugin to parse that field and take actions as appropriate, e.g.:


MODULE
{
name = magicalCodeAndLogicThingy
INPUT
{
name = IntakeAir
rate = 1.0
}
OUTPUT
{
contingency = if(mainBody==Kerbin)
name = Oxygen
rate = 0.21
}
OUTPUT
{
contingency = if(mainBody==Laythe)
name = Oxygen
rate = 0.15
}
}

But, implementing something like that with broad re-usability is going to involve a lot of eval calls, or be very laborious on your part. If you have a specific application like this example in mind, it would be easier to do something like this:


MODULE
{
name = magicalOxygenIntake
INPUT
{
name = IntakeAir
rate = 1.0
}
OUTPUT
{
name = Oxygen
PLANET
{
name = Kerbin
rate = 0.15
}
PLANET
{
name = Laythe
rate = 0.15
}
}
}

Then in your magicalOxygenIntake OnLoad override, you scan the PLANET nodes (using the existing ConfigNode tools in the KSP API) to define specific behavior based on those blocks.

So, the real question to be asking is: what are you trying to do? If you're trying to write a scriptable language for cfg files that allows for broad reuse, you might start by researching what ModuleManager has done with loading its config files, and then start specifying a language. If you're looking to make a magical oxygen intake, what you really want to do is work inside the tools already in KSP's API to build a PartModule that does what you're looking for.

Link to comment
Share on other sites

So, the real question to be asking is: what are you trying to do? If you're trying to write a scriptable language for cfg files that allows for broad reuse, you might start by researching what ModuleManager has done with loading its config files, and then start specifying a language. If you're looking to make a magical oxygen intake, what you really want to do is work inside the tools already in KSP's API to build a PartModule that does what you're looking for.

What I am interested in is the former.

Given what I have read, I am going to assume that the answer to my initial question is that no one is working on such a thing. Or at least no one has chimed in to say they are working on such a thing.

Link to comment
Share on other sites

What I am interested in is the former.

Given what I have read, I am going to assume that the answer to my initial question is that no one is working on such a thing. Or at least no one has chimed in to say they are working on such a thing.

The closest that I'm aware of is ModuleManager. That's not where the focus is, but there are at least similarities in that it offers a small scripting language in cfg-format files geared to a specific purpose.

Link to comment
Share on other sites

  • 3 weeks later...

You could do something similar to this, however, I am not at my computer so this exact code might not work. Just put this code in a class extending partmodule in the OnUpdate function and put the module in your part, no code in the config required!

float ResourceRate;

If(vessel.mainBody.name = "Kerbin")

ResourceRate = 0.21;

If(vessel.main body.name = "Laythe")

ResourceRate = 0.15;

Part.RequestResource("Oxygen", ResourceRate);

[\CODE]

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