Jump to content

[Solved] How to update engine fx through a plugin


Recommended Posts

I'm creating an engine switcher, and now I'm looking af switch the effects along with engine stats. Now I've run into en issue where the fx does not update properly from "OnLoad()" event.

Basically what I do is

  1. Load settings from custom module
  2. Identify engine module to change
  3. Use ConfigNode to replace values regarding Fx in the engine
  4. Load the new values to the engine.

I only have one update function, which is called by the OnLoad() event as well as by my switch button in the right click menu. However, when loading (or starting) a flight scene, the animation is just running with the animation from the original part. The issue seems to be triggered when the fx intended is different from the original one.... The behavior is not affected by deactivating the engine... (full code can be accessed here)

Spoiler

public void updateEngineModuleEffects(ModuleEngines moduleEngine, bool calledByPlayer, string callingFunction = "player")
        {
            ConfigNode newEngineEffectNode = new ConfigNode();
            ConfigNode EngineEffectNode = newEngineEffectNode;
            

            if (!runningEffectNameEmpty)
            {

                if (string.IsNullOrEmpty(propList[selectedPropellant].runningEffectName))		//propList is a custom list where the setting values are stored untill needed.
                {
                    EngineEffectNode.RemoveValue("runningEffectName");
                    moduleEngine.DeactivateRunningFX();
                }
                else
                {
                    EngineEffectNode.AddValue("runningEffectName", propList[selectedPropellant].runningEffectName);
                    moduleEngine.ActivateRunningFX();
                }

                
                Debug.Log(
                        "\npropList[selectedPropellant].runningEffectName " + propList[selectedPropellant].runningEffectName +
                        "\nstring.IsNullOrEmpt " + string.IsNullOrEmpty(propList[selectedPropellant].runningEffectName) +
                        "" + "");
            }
            if (!powerEffectNameEmpty)
            {
                if (string.IsNullOrEmpty(propList[selectedPropellant].runningEffectName))
                {
                    EngineEffectNode.RemoveValue("powerEffectName");
                    moduleEngine.DeactivatePowerFX();
                }
                else
                {
                    EngineEffectNode.AddValue("powerEffectName", propList[selectedPropellant].powerEffectName);
                    moduleEngine.ActivatePowerFX();
                }



                
                Debug.Log("propList[selectedPropellant].powerEffectName: " + propList[selectedPropellant].powerEffectName);
            }
            if (!spoolEffectNameEmpty)
            {
                EngineEffectNode.AddValue("spoolEffectName", propList[selectedPropellant].spoolEffectName);
                Debug.Log("propList[selectedPropellant].spoolEffectName: " + propList[selectedPropellant].spoolEffectName);
            }



            moduleEngine.Load(EngineEffectNode);
            moduleEngine.InitializeFX();
            
            //moduleEngine.FXReset();
            //moduleEngine.FXUpdate();
            //moduleEngine.
            //}
        }

 

I guess there is some trick to get the fx changes to take effect, but I just can't figure out what it is. Does anybody out there know what is needed?

Edited by Warezcrawler
Solved
Link to comment
Share on other sites

Some testing have showed that switching from UI work's in current version, however if switching while throttled up, then animation/effects is not switched correctly... It does switch in the engine is not running, or running very slowly (spinning down) after being used).

There must be some trick to have the animation take effect in engines.

So, the challenge of changing effect have been identified when loading the scene, and when switching while the engine is running.

 

Does anybody have an idea of how to attack this?

Link to comment
Share on other sites

Here is my FX updating code that I use for some resizable SRB parts; it is more oriented towards rescaling, but perhaps it contains some useful information applicable to your situation (it does force-reload the FX through the part rather than engine; might need to do both). (note that scaling doesn't work for stock effects.... and that it only works properly for RealPlume configured effects; though that should not matter for the reloading/re-init code)

https://github.com/shadowmage45/SSTULabs/blob/master/Source/Module/SSTUModularBooster.cs#L806-L903

 

Link to comment
Share on other sites

On 14/4/2016 at 10:12 PM, Shadowmage said:

Here is my FX updating code that I use for some resizable SRB parts; it is more oriented towards rescaling, but perhaps it contains some useful information applicable to your situation (it does force-reload the FX through the part rather than engine; might need to do both). (note that scaling doesn't work for stock effects.... and that it only works properly for RealPlume configured effects; though that should not matter for the reloading/re-init code)

https://github.com/shadowmage45/SSTULabs/blob/master/Source/Module/SSTUModularBooster.cs#L806-L903

 

Thanks. I'll have a look at it.

Link to comment
Share on other sites

On 14/4/2016 at 10:12 PM, Shadowmage said:

Here is my FX updating code that I use for some resizable SRB parts; it is more oriented towards rescaling, but perhaps it contains some useful information applicable to your situation (it does force-reload the FX through the part rather than engine; might need to do both). (note that scaling doesn't work for stock effects.... and that it only works properly for RealPlume configured effects; though that should not matter for the reloading/re-init code)

https://github.com/shadowmage45/SSTULabs/blob/master/Source/Module/SSTUModularBooster.cs#L806-L903

 

I tried the Onload method in several ways, but it does not seem to change anything :(

part.Effects.OnLoad(copiedEffectsNode);

I've even tried using confignodes to copy the entire engine module, and reload it into the part..... Maybe I'm doing it wrong, but it basically did nothing for me.

 

To restate my issue:

  • When loading a scene, the FX is just running, disregarding that the engine is of.
    • The FX running is also the parts normal FX and not the one I set in my mod.
    • The FX is reset to the right one if I do manual switch by UI after scene is loaded.
      • The update function is to same one as when the scene is loaded.
  • If changing Fx while engine is running, the Fx does not change as it should.
    • It does change correctly if the engine have had time to spin down first, otherwise it will retain the fx it had...

I somehow need to destroy the fx already running/in the engine, and replace it by new ones.

 

Currently I have (I have cut out "noise" code)

ConfigNode EngineEffectNode = newEngineEffectNode;

EngineEffectNode.AddValue("runningEffectName", propList[selectedPropellant].runningEffectName);
EngineEffectNode.AddValue("powerEffectName", propList[selectedPropellant].powerEffectName);
EngineEffectNode.AddValue("spoolEffectName", propList[selectedPropellant].spoolEffectName);

moduleEngine.Load(EngineEffectNode);

//Where propList[selectedPropellant].XYZ is the new effect

Then CFG nodes/value I'm trying to change are

Spoiler

PART
{
	name = turboFanEngine_modified
	...
	MODULE
	{
		name = ModuleEnginesFX
		...
		runningEffectName = shockDiamond					//Change to running_dry
		powerEffectName = running_thrust					//Change to power_dry
		spoolEffectName = running_turbine					//Change to running_dry
		...
	}
}

//I've added new effects using module manager to the effects node
+PART[turboFanEngine]					//Yes I'm creating a copy which I change
{
	@name = turboFanEngine_modified
	@title = J-X4 "Whiplash" MODIFIED
	@EFFECTS
	{
		
		running_dry
		{
			PREFAB_PARTICLE
			{
				prefabName = fx_smokeTrail_light
				transformName = smokePoint
				emission = 0.0 0.0
				emission = 0.05 0.0
				emission = 0.075 0.25
				emission = 1.0 1.25
				speed = 0.0 0.25
				speed = 1.0 1.0
				localOffset = 0, 0, 1
				localRotation = 1, 0, 0, -90
			}
			AUDIO
			{
				channel = Ship
				clip = sound_jet_low
				volume = 0.0 0.0
				volume = 0.05 0.9
				volume = 1.0 1.0
				pitch = 0.0 0.5
				pitch = 0.05 0.6
				pitch = 0.33 1.0
				pitch = 1.0 1.2
				loop = true
			}
		}
		power_dry
		{
			AUDIO
			{
				channel = Ship
				clip = sound_jet_deep
				volume = 0.0 0.0
				volume = 0.05 0.4
				volume = 1.0 0.9
				pitch = 0.0 0.8
				pitch = 1.0 1.4
				loop = true
			}
		}
	}
}

 

So I'm not changing the EFFECTS node in my plugin, I use MM for that. By I do try to change the engine references using my plugin.

Does anyone notice what I might be doing wrong?

Edited by Warezcrawler
Link to comment
Share on other sites

I found a way. Somehow the effect animations did not reset correctly. I was succesfull in getting the effects to reset by doing two things.

For the animation of effects running af scene load, I used the monobehavior "invoke", and introduced a short delay of 1 sec at scene load.

//I used this
Invoke("updateEngineModule", 1f);

//and it replaced
updateEngineModule();

next issue was when switching between engine configurations, the effect again did not update correctly. Apparently I had to disable 3 Fx methods to achieve that the new effects could take over correctly, and not having to old ones running indefinately.

currentModuleEngine.DeactivateLoopingFX();
currentModuleEngine.DeactivatePowerFX();
currentModuleEngine.DeactivateRunningFX();

Special thanks to Shadowmage and FreeThinker for helping me at this matter.

Happy coding

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