Jump to content

Suggestion for a little module plugin (ModuleMultiLight)


Recommended Posts

Hi

I am sure you are aware that the stock ModuleLight accepts only a single object with light component on it. How ever, we see many mods with multiple light objects on a single part. In many cases, there is a need for many lights being turned on/off/blink simultaneously suing a single button in PAW.

I am not familiar with plugin development for ksp at all, but, went on and looked into the code for module light..... On the Start method, We have :

this.lights = new List<Light>((IEnumerable<Light>)this.part.FindModelComponents<Light>(this.lightName));

As you see here, the module obviously has a list for lights, this is nice how ever, it only looks into a single model name, and tries to capture as many light components it has!

Changing this behavior shouldn't be that hard, you just need to observe a list of objects from part file separated with comma, loop through them, and look for the light components on each one of them and add it to list, how ever, if there was only one object named, not only you would capture the light on the object, but also should loop through it's children and see if there are any lights there and capture them as well. Up until this point, I could sit down and write the code in 5 minutes my self how ever... few lines bellow that... we have this :

light.color = this.lightColor;

This is happening inside a loop that is looping through the above generated list. I thought to myself, ok,... let's inherit the class for ModuleLight, Override the OnStart, call the base first, regenerate the list and duplicate the loop (there is no other section on the OnStart that works with this list other than this loop). Problem is.... this.lightColor on the ModuleLight, is a private field ! I am not that familiar with Reflection but I think we would need an instance of the object in memory to be able using reflection and get the private filed value?!

 

SO, long story short, I hit a wall that passing it is beyond my knowledge of C#. How ever, I think, this module should not be that complicated to write. The end goal is clear, in part file, where we had :

MODULE
	{
		name = ModuleLight
		lightName = OBJECT_NAME
		useAnimationDim = True
			animationName = ANIMATION_NAME
			lightBrightenSpeed = 2.5
			lightDimSpeed = 2.5
		disableColorPicker = False
			toggleInEditor = True
			toggleInFlight = True
		canBlink = True
			blinkMin = 0.2
			blinkMax = 2.0
			blinkRate = 0.5
			isBlinking = False
		useResources = True
			resourceName = ElectricCharge
			resourceAmount = 0.003
	}

We should have something like this :

MODULE
	{
		name = ModuleMultiLight
		lightName = OBJECT_NAME1,OBJECT_NAME2,OBJECT_NAME3,....
		useAnimationDim = True
			animationName = ANIMATION_NAME
			lightBrightenSpeed = 2.5
			lightDimSpeed = 2.5
		disableColorPicker = False
			toggleInEditor = True
			toggleInFlight = True
		canBlink = True
			blinkMin = 0.2
			blinkMax = 2.0
			blinkRate = 0.5
			isBlinking = False
		useResources = True
			resourceName = ElectricCharge
			resourceAmount = 0.003
	}

and if we have only one name, then we would look for the children of that object. If we had many names like the code above, then no need to look for children. After that, if we properly generated the mentioned list, we should be able to handle ALL those lights with one on/off or blink button in PAW?!

One extra feature that would be nice is the ability to change the light intensity from part file module parameter. This should affect all the lights in the list at the same time and set all of them to a single value.

So, please, I beg someone with enough knowledge on coding for KSP to do this little module plugin.

Edited by Jiraiyah
Link to comment
Share on other sites

I put together some code, didn't compile it, and didn't test if it's working or not, I just did it to give you guys a rough idea of what I'm looking for and a possible starting point :

using System.Collections.Generic;
using UnityEngine;

public class ModuleMultiLight : ModuleLight
{
    [KSPField]
    public float lightIntensity = 60;

    public override void OnStart(PartModule.StartState state)
    {
        base.OnStart(state);
        lights = new List<Light>();
        string[] temp = lightName.Replace(" ", "").Split(',');

        foreach (var name in temp)
            this.lights.AddRange(part.FindModelComponents<Light>(name));

        foreach (var light in lights)
        {
            light.intensity = lightIntensity;
            brightnessLevels.Add(lightIntensity);
            // Not sure if the reflection bellow will work or not!
            light.color = (Color) typeof(ModuleLight).GetField("lightColor").GetValue(this);
        }
    }
}

If this code works, I had covered the multiple names, how ever, I was a little confused on how to look for children case. If this was pure unity game development, I would know what to do, how ever, this is an API for KSP and I am not familiar with this API at all.

If someone is familiar with this, please take a look at this piece of code and if possible, add the ability to look for children of objects and find lights to add to list, and, please confirm if the reflection code is correct or not!

thanks

Link to comment
Share on other sites

ok, one little thing, when I was talking about light intensity, I was thinking about intensity AND light range being tweakable from module code in part file, I just wrote the intensity, because I didn't know if light range was available or not and didn't have enough time to dig into the code. Any one has any idea about the whole code and possible solution for multiple children under a single parent?

Link to comment
Share on other sites

Hm, tested the code and it works, published the mod

But there is no child search functionality, just the simple code above being compiled, tested and published. If anyone can add the code for searching the children and adding them to the list, I would like pull requests.

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