Jump to content

Changing GuiUnits based on ConfigNode.


Recommended Posts

I've been trying to adapt the Beamed Power Standalone mod to manage a resource defined in a cfg file. I have this code: 

namespace BeamedPowerStandalone
{
    public class WirelessReceiver : PartModule
    {
        static ConfigNode setting = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/BeamedPowerStandalone/Settings.cfg");
        private static string ManagedResource = setting.GetValue("BPManagedResource");
        private static string ManagedResourceUnits = ManagedResource + "/s";
      OTHER STUFF HERE
      [KSPField(guiName = "Received Power", isPersistant = true, guiActive = true, guiActiveEditor = false, guiUnits = ManagedResourceUnits)]
	  public float ReceivedPower;
      OTHER STUFF HERE
    }
}

The important part is guiUnits = ManagedResourceUnits. It throws this error: 

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error	CS0182	An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type	

How do I fix this?

 

Code in this repository, VariableResourceBranch, if needed: https://github.com/ProgrammerFailure/BeamedPowerStandalone/tree/VariableResourceBranch

Link to comment
Share on other sites

Posted (edited)
4 minutes ago, HebaruSan said:

What is ManagedResourceUnits?

Its supposed to be "whatever the resource being beamed is" followed by /s. So eg, if its electric charge, it would be electric charge/s

 

The idea is that the PAW of a part receiveing a resource will show: (transfer rate) (resource name) (/s)

Edited by KspNoobUsernameTaken
Link to comment
Share on other sites

Just now, HebaruSan said:

No, how is it defined? What is it in the code?

The first instance of it shown above is the initialization; that is, ManagedResourceUnits is a private static string in the WirelessReceiver class. See here:

59 minutes ago, KspNoobUsernameTaken said:
private static string ManagedResourceUnits = ManagedResource + "/s";

So hopefully: 

Load configNode settings from file

set ManagedResource to Get value BPManagedResource

Set ManagedResourceUnits to ManagedResource + /s

Link to comment
Share on other sites

Posted (edited)
3 minutes ago, HebaruSan said:

Try making it a const instead of static. A value passed to an attribute needs to be known at compile time.

I tried it as const before and it worked, but thats an issue because I need to get the ManagedResource from a cfg file... I was dreading this would be the case. So there is actually no way to make it "dynamic"?

 

This is the same solution as: https://stackoverflow.com/questions/25859094/an-attribute-argument-must-be-a-constant-expression-create-an-attribute-of

Edited by KspNoobUsernameTaken
Link to comment
Share on other sites

You're not going to be able to pass a value from a config file into an [Attribute()], that just can't work based on how C# works. Attributes are a compile-time feature.

You could conceivably load the config file and then acquire a reference to the KSPField object and modify it at runtime, via  the Fields["ReceivedPower"] property on a given part module.

https://www.kerbalspaceprogram.com/ksp/api/class_k_s_p_field.html

Link to comment
Share on other sites

Just now, HebaruSan said:

You're not going to be able to pass a value from a config file into an [Attribute()], that just can't work based on how C# works. Attributes are a compile-time feature.

You could conceivably load the config file and then acquire a reference to the KSPField object and modify it at runtime, via  the Fields["ReceivedPower"] property on a given part module.

https://www.kerbalspaceprogram.com/ksp/api/class_k_s_p_field.html

I'll look into that later; for now I'll just make guiUnits = "/s", which will display (transfer rate) (/s). Thanks for the clarification. C# is really complicated for someone starting a week ago.

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