Jump to content

How To Update Part Appearance?


Recommended Posts

So... in a PartModule during the flight scene, the onLoad is the first method that fires that includes the config data for the PartModule; before this event, the KSPFields are newly initialized. However, the onLoad method fires after the part's models and textures have already been drawn.

On the other hand, the first time onAwake fires is before the part is drawn, but there is no config data or fields available during the first fire because they have not loaded yet.

----

Does anyone know a way around this issue or if there is another method somewhere to force the part to redraw?

Link to comment
Share on other sites

Maybe try checking out some of the code of Kerbal Crash System? 

It does part deformation and probably model changing as well. 

EDIT: Reading through the code, it looks like Enzo used https://github.com/EnzoMeertens/KerbalKrashSystem/blob/master/Source/KerbalKrashSystem/KerbalKrashSystem/KerbalKrashSystem.cs#L401 to change the shape of the part, and a similar function (called a couple lines above) to change the collider. 

Edited by Benjamin Kerman
Link to comment
Share on other sites

On 11/17/2018 at 6:48 PM, Benjamin Kerman said:

Maybe try checking out some of the code of Kerbal Crash System? 

It does part deformation and probably model changing as well. 

EDIT: Reading through the code, it looks like Enzo used https://github.com/EnzoMeertens/KerbalKrashSystem/blob/master/Source/KerbalKrashSystem/KerbalKrashSystem/KerbalKrashSystem.cs#L401 to change the shape of the part, and a similar function (called a couple lines above) to change the collider. 

Thanks, but I'm aware that you can directly mess with meshes and transforms; what I need is to actually run through the default stock code as if it were in the part's config from the beginning.

Oddly enough this works fine when you revert to the Editor, but the flight mode runs PartModule methods in a different order.

Link to comment
Share on other sites

Can you be more specific about what you're trying to do? There isn't any code related to the part's appearance itself before PartModules start getting loaded. The part prefab is instantiated as is and then PartModules do the thing, one of which may be to modify the part's appearance.

Why can't you modify the part inside OnLoad? I'm fairly sure that fires in the same frame as when the part itself is created, so changing part appearance there is fine and the user will never notice

Link to comment
Share on other sites

23 hours ago, Electrocutor said:

Oddly enough this works fine when you revert to the Editor, but the flight mode runs PartModule methods in a different order.

In all of my texture-set manipulation code, I have to run it both during OnStart, and OnLoad (usually with a quick 'isInitialized' to only run it once, from whichever method is actually called first).

Sometimes one is called, but not the other.  Good luck figuring out when each one is called -- its a big mixed up mess depending on how the part is instantiated (and if is a revert, or a reload).

Link to comment
Share on other sites

14 hours ago, xEvilReeperx said:

Can you be more specific about what you're trying to do? There isn't any code related to the part's appearance itself before PartModules start getting loaded. The part prefab is instantiated as is and then PartModules do the thing, one of which may be to modify the part's appearance.

Why can't you modify the part inside OnLoad? I'm fairly sure that fires in the same frame as when the part itself is created, so changing part appearance there is fine and the user will never notice

During flight, onLoad fires AFTER the part has already been read-in and drawn (reading in mesh, model, rescaleFactor, scale, attach nodes, etc). I need to adjust things before it has been created and set. The reason is because modifying something after the fact by forcibly changing transform sizes, positions, or other things (like Tweakscale) is very prone to breaking between version when they change something and unexpected behavior. If you modify those things before it is loaded, then the game just sees it having been that way from the start and everything works normally no matter what was changed.

 

7 minutes ago, Shadowmage said:

In all of my texture-set manipulation code, I have to run it both during OnStart, and OnLoad (usually with a quick 'isInitialized' to only run it once, from whichever method is actually called first).

Sometimes one is called, but not the other.  Good luck figuring out when each one is called -- its a big mixed up mess depending on how the part is instantiated (and if is a revert, or a reload).

onStart fires long after onLoad. Oddly enough, onInitialize also fire after onLoad. The only option I can think is to manually pull data out from the persistence file myself during onAwake: which is horribly nasty/ugly.

Link to comment
Share on other sites

1 hour ago, Electrocutor said:

During flight, onLoad fires AFTER the part has already been read-in and drawn

I have no problem applying textures/etc in flight scene using the OnLoad method.  It does not result in any flickering/etc; everything appears to be applied before the first Update is called / before the first frame is rendered.  This is how I restore persistent textures/user settings/etc in TexturesUnlimited.

What precisely are you trying to change that isn't working / what issues are you running into when using OnLoad/OnStart?

 

 

Link to comment
Share on other sites

4 minutes ago, Shadowmage said:

I have no problem applying textures/etc in flight scene using the OnLoad method.  It does not result in any flickering/etc; everything appears to be applied before the first Update is called / before the first frame is rendered.  This is how I restore persistent textures/user settings/etc in TexturesUnlimited.

What precisely are you trying to change that isn't working / what issues are you running into when using OnLoad/OnStart?

 

 

Many things: the current is to set rescaleFactor and scaleFactor. These both work onLoad when reverting to the editor, but not when starting flight. If I force set them to some number in onAwake (before KSP has set the PartModule fields), it works in all scenarios, but this means I would have to find and load my persistent values myself because KSP hasn't set them yet.

Link to comment
Share on other sites

4 minutes ago, Electrocutor said:

Many things: the current is to set rescaleFactor and scaleFactor. These both work onLoad when reverting to the editor, but not when starting flight. If I force set them to some number in onAwake (before KSP has set the PartModule fields), it works in all scenarios, but this means I would have to find and load my persistent values myself because KSP hasn't set them yet.

Ahh, yep, that kind of stuff.  Can certainly see the problem there.  I think the issues is less that 'things have already been rendered' (as they have not yet been rendered, until at least after Update() has been called), and more that 'stock code will not update model scales after model is loaded into the part, which happens prior to any PartModule code running' combined with 'rescaleFactor is not a persistent value and has to be set prior to stock code loading the model so can't fix it in the prefab or persistent data for a craft'.

Hmmm... definitely a problem there.

IIRC the way that Tweakscale/etc works around this issue is to go back in and manually adjust the model scale and node positions during the OnLoad/OnStart calls, by manipulating the 'localScale' of the model transforms directly.  This is basically what the stock code does as it loads the models for the part.  Might be a solution for your immediate problem, but isn't really a general answer on how to work around stock code order-of-loading quirks.

Link to comment
Share on other sites

3 minutes ago, Shadowmage said:

Ahh, yep, that kind of stuff.  Can certainly see the problem there.  I think the issues is less that 'things have already been rendered' (as they have not yet been rendered, until at least after Update() has been called), and more that 'stock code will not update model scales after model is loaded into the part, which happens prior to any PartModule code running' combined with 'rescaleFactor is not a persistent value and has to be set prior to stock code loading the model so can't fix it in the prefab or persistent data for a craft'.

Hmmm... definitely a problem there.

IIRC the way that Tweakscale/etc works around this issue is to go back in and manually adjust the model scale and node positions during the OnLoad/OnStart calls, by manipulating the 'localScale' of the model transforms directly.  This is basically what the stock code does as it loads the models for the part.  Might be a solution for your immediate problem, but isn't really a general answer on how to work around stock code order-of-loading quirks.

Like I said, setting rescaleFactor and scaleFactor during the onAwake of the PartModule works fine. So it just becomes a matter of me going out to get my persistent values myself. I had just wondered if there was universal "re-make this part' method call: as obviously it is re-read when you revert from flight to editor.

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