Jump to content

Is there way to create/manage multiple parts with one class?


Recommended Posts

I need multiple similar parts which would have simple dependency on precence of mods/resources/etc.

I expect that there would be too much code duplication to have class for each part, which makes me want to create and manage multiplie parts in one class.

But it seems that part should be created through PartModule, which seems to be loaded separately and adds single part with the property described in the class.

Is this mean that what I want is basically impossible? Or is PartModule just for easy part addition, and is there a way to do this?

Link to comment
Share on other sites

  • Base class (inheriting part module) with specialty subclasses (you dont have to directly inherit from Part Module) sharing common logic
  • Configurable parameters (mod name, resource, etc.)?
Edited by Crzyrndm
Link to comment
Share on other sites

18 minutes ago, Crzyrndm said:
  • Base class (inheriting part module) with specialty subclasses (you dont have to directly inherit from Part Module) sharing common logic
  • Configurable parameters (mod name, resource, etc.)?

Thanks, but I want to determine whether or not load certain parts depend on mods/resources/etc.

Also I think subclassing still causes too much code duplication, where just instances with certain properties will work.

Link to comment
Share on other sites

Parts are not created through classes/code/plugins.  They are created through text based config files.

If you need mod/resource dependent parts (only present when the mod/resource is in the game) use ModuleManager with various NEEDS / HAS block setups.

 

PartModules are created through code, and added to parts through their config files.

Link to comment
Share on other sites

6 hours ago, Shadowmage said:

Parts are not created through classes/code/plugins.  They are created through text based config files.

If you need mod/resource dependent parts (only present when the mod/resource is in the game) use ModuleManager with various NEEDS / HAS block setups.

 

PartModules are created through code, and added to parts through their config files.

Thanks, I didn't know that.

Link to comment
Share on other sites

14 hours ago, Abastro said:

Then, is there a way to have code-defined variations of models? I think that's what I need actually.

Is this related with Unity? I'd like to know!

Kind of, but its complicated.  I have lots of parts in my mods that switch models while in game, but there is a ton of custom plugin code making it all work.

The repo/code examples/downloads can be found at: https://github.com/shadowmage45/SSTULabs

 

Perhaps if you gave more concise examples of what you are trying to accomplish I could give more useful details on how to make it work.  What kind of parts are you trying to make, why/when do they switch models, and what kind of models are they switching between?

Link to comment
Share on other sites

7 hours ago, Shadowmage said:

Kind of, but its complicated.  I have lots of parts in my mods that switch models while in game, but there is a ton of custom plugin code making it all work.

The repo/code examples/downloads can be found at: https://github.com/shadowmage45/SSTULabs

 

Perhaps if you gave more concise examples of what you are trying to accomplish I could give more useful details on how to make it work.  What kind of parts are you trying to make, why/when do they switch models, and what kind of models are they switching between?

Thanks! Basically I'd like to change models depend on the conditions specified in ?

Your code is really helpful, but more explanation on the manipulation part would be great!

Also can I manipulate model via connection with neighboring modules?

Link to comment
Share on other sites

17 hours ago, Abastro said:

Thanks! Basically I'd like to change models depend on the conditions specified in ?

 

What is the ? condition that you are trying to use to determine when to modify the models?

Are you wanting to modify the models in the editor based on user interaction, based on craft composition, during flight based on specific values, or some other information?  Are you wanting to change part functionality, or merely the models of the part?

Need to know when/what you are trying to do before I could give any further pointers.

 

Link to comment
Share on other sites

7 hours ago, Shadowmage said:

What is the ? condition that you are trying to use to determine when to modify the models?

Are you wanting to modify the models in the editor based on user interaction, based on craft composition, during flight based on specific values, or some other information?  Are you wanting to change part functionality, or merely the models of the part?

Need to know when/what you are trying to do before I could give any further pointers.

 

Oh, I apparently forgot some words there. I want to modify model depend on

1. Part Settings (or Tweakables?)

2. Connected Parts

 

The function would change too, but I think I can implement the functions on my own.

Link to comment
Share on other sites

10 hours ago, Abastro said:

Oh, I apparently forgot some words there. I want to modify model depend on

1. Part Settings (or Tweakables?)

2. Connected Parts

 

The function would change too, but I think I can implement the functions on my own.

For #1 the code I posted has many examples of that.  Create a public void method and add a [KSPEvent] attribute to it, filling in the stuff as you want it (gui name, enabled in editor, enabled in flight); this creates a single GUI button for that action.  Put your code in that method to do the manipulation.  You can swap models, alter variables, etc.   More advanced GUI widgets are available for floating point, string-lists, and boolean toggles; lots of examples on those are available (both online and in the code I linked).

For #2... it gets a LOT more complicated trying to interact with multiple parts.  Almost to the point of impossible for some uses.  Mostly it comes down to order-of-initialization of the parts and when to link things back up / re-check for your parts.  Make a 'public void Start(){}' method and check for neighboring/connected parts in that step to initialize to the desired state.  From there you would need to subscribe to some of the editor-part-changed and editor-vessel-changed events to re-check the connected state and update your parts internal state.  Flight scene is generally a bit easier because you should be able to use the cached state that was determined in the editor, but may still need to check for vessel alteration (docking, decoupling).


Changing the -function- of the part is much more difficult.  Stock does not support run-time addition/removal of PartModules, which is where all functions come from.  So a part is mostly stuck with whatever function it is created with in its config file.  There is a little bit that can be done with the modules (examples of that exist in my linked code as well), but I would recommend waiting/holding off on attempting any of that until you are much more familiar with KSP modding and the workings of the stock KSP code (it has some quirks!).

Link to comment
Share on other sites

43 minutes ago, Shadowmage said:

For #1 the code I posted has many examples of that.  Create a public void method and add a [KSPEvent] attribute to it, filling in the stuff as you want it (gui name, enabled in editor, enabled in flight); this creates a single GUI button for that action.  Put your code in that method to do the manipulation.  You can swap models, alter variables, etc.   More advanced GUI widgets are available for floating point, string-lists, and boolean toggles; lots of examples on those are available (both online and in the code I linked).

For #2... it gets a LOT more complicated trying to interact with multiple parts.  Almost to the point of impossible for some uses.  Mostly it comes down to order-of-initialization of the parts and when to link things back up / re-check for your parts.  Make a 'public void Start(){}' method and check for neighboring/connected parts in that step to initialize to the desired state.  From there you would need to subscribe to some of the editor-part-changed and editor-vessel-changed events to re-check the connected state and update your parts internal state.  Flight scene is generally a bit easier because you should be able to use the cached state that was determined in the editor, but may still need to check for vessel alteration (docking, decoupling).


Changing the -function- of the part is much more difficult.  Stock does not support run-time addition/removal of PartModules, which is where all functions come from.  So a part is mostly stuck with whatever function it is created with in its config file.  There is a little bit that can be done with the modules (examples of that exist in my linked code as well), but I would recommend waiting/holding off on attempting any of that until you are much more familiar with KSP modding and the workings of the stock KSP code (it has some quirks!).

Thanks for so helpful information!

Also, isn't PartModules allowed to access vessel's current state? Or is the instance more or less singleton? Then I'd like to know if there would be storage for vessel-specific data.

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