Split: viewDirectionChanged woes

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Split: viewDirectionChanged woes

Post by Thargoid »

Dragonfire wrote:
Mauiby de Fug wrote:
CommonSenseOTB wrote:
I have a question: How many "elses" can follow an "if"?
One, but by coupling that else with an if, as many as you like!
The only thing I miss from Visual Basic is the Select Case construct...
If it's based on a single variable, you can use the switch statement. It's note quite the same, but it can do away with a chain of if's in some situations.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2278
Joined: Tue Jan 02, 2007 12:38 pm

Re: Split: viewDirectionChanged woes

Post by Killer Wolf »

right, i've tried to integrate Svengali's stuff, but i'm getting an error about a missing "}" at teh end of the code

Code: Select all

this.name               = "kwPhantomHUDswitch.js";
this.author               = "Thargoid";
this.copyright            = "Do what you want with it";
this.description         = "Switches HUDs based on alert condition and ships energy";
this.version            = "1.0";


this.alertConditionChanged = function(newCondition, oldCondition)
   {
   switch(newCondition)
		{
      case 0: // we're docked
			{
			player.ship.hud = "phantomTradingHUD.plist"; // set the docked HUD
			break;
			}
      case 1: // we're at green alert
      case 2: // or we're at yellow alert
			{
		    this.viewDirectionChanged = function(to,from)
			{
				log(this.name,"viewDirectionChanged: to:"+to+" from:"+from);
				if(from === "VIEW_GUI_DISPLAY" && to !== "VIEW_GUI_DISPLAY")
					{
					if(player.alertHostiles && player.ship.energy > 108 ) 
						{
							player.ship.hud = "phantomNormalHUD.plist"; 
						}
					else if(player.ship.energy < 109)
						{
							player.ship.hud = "phantomSnafuHUD.plist";
						}	
					}
			}
			this.guiScreenChanged = function(to,from)
			{
				log(this.name,"guiScreenChanged: to:"+to+" from:"+from);
				if(to === "GUI_SCREEN_STATUS")
					{
						player.ship.hud = "phantomGUI.plist";
					}
				else if(to === "GUI_SCREEN_SHORT_RANGE_CHART" || "GUI_SCREEN_LONG_RANGE_CHART")
					{
						player.ship.hud = "phantomGUI.plist";
					}		
				else if(to === "GUI_SCREEN_MANIFEST")
					{
						player.ship.hud = "phantomGUI.plist";
					}
				else if(to === "GUI_SCREEN_SYSTEM_DATA")
					{
						player.ship.hud = "phantomGUI.plist";
					}
				else if(to === "GUI_SCREEN_MARKET")
					{
						player.ship.hud = "phantomGUI.plist";
					}
			}
		}

      case 3: // we're at red alert
        {
		    this.viewDirectionChanged = function(to,from)
			{
				log(this.name,"viewDirectionChanged: to:"+to+" from:"+from);
				if(from === "VIEW_GUI_DISPLAY" && to !== "VIEW_GUI_DISPLAY")
					{
					if(player.alertHostiles && player.ship.energy > 108 ) 
						{
							player.ship.hud = "phantomCombatHUD.plist"; // set the combat HUD
						}
					else if(player.ship.energy < 109)
						{
							player.ship.hud = "phantomSnafuHUD.plist";
						}	
					}
			}
			this.guiScreenChanged = function(to,from)
			{
				log(this.name,"guiScreenChanged: to:"+to+" from:"+from);
				if(to === "GUI_SCREEN_STATUS")
					{
						player.ship.hud = "phantomGUI.plist";
					}
				else if(to === "GUI_SCREEN_SHORT_RANGE_CHART" || "GUI_SCREEN_LONG_RANGE_CHART")
					{
						player.ship.hud = "phantomGUI.plist";
					}		
				else if(to === "GUI_SCREEN_MANIFEST")
					{
						player.ship.hud = "phantomGUI.plist";
					}
				else if(to === "GUI_SCREEN_SYSTEM_DATA")
					{
						player.ship.hud = "phantomGUI.plist";
					}
				else if(to === "GUI_SCREEN_MARKET")
					{
						player.ship.hud = "phantomGUI.plist";
					}
			}
		}
		
	}


	
this.shipLaunchedFromStation = function()
   {
   if(this.energyCheckTimer)
      {
      this.energyCheckTimer.start()
      }
   else
      {
      this.energyCheckTimer = new Timer(this, this.energyCheck,0,2)      // use a timer to keep an eye on the HUD state
      }
   }


this.energyCheck = function()
   {
   if(player.ship.docked)
      {
      this.energyCheckTimer.stop();
      }
   else
      {
      if(player.ship.energy < 109 )
         {
         player.ship.hud = "phantomSnafuHUD.plist"; // display the damaged HUD
         return;
         }
      if(player.ship.energy > 108 && player.ship.hud == "phantomSnafuHUD.plist")
         {
         this.alertConditionChanged(player.alertCondition,0); // if energy is >49 and we're displaying the damaged HUD, use other code to repair
         }
      }             
   }

this.shipDied = function()
   {
   if(this.energyCheckTimer)
      {
      this.energyCheckTimer.stop()
      }
   }
buggered if i can see it, Notepadd++ is showing all the stuff aligned under the vertical lines. can anyone spot it?

ta!
JD
Deadly
Deadly
Posts: 182
Joined: Thu Nov 25, 2010 10:42 pm
Location: London, UK

Re: Split: viewDirectionChanged woes

Post by JD »

Killer Wolf wrote:
right, i've tried to integrate Svengali's stuff, but i'm getting an error about a missing "}" at teh end of the code

Code: Select all

      case 2: // or we're at yellow alert
			{
		    this.viewDirectionChanged = function(to,from)
			{
				log(this.name,"viewDirectionChanged: to:"+to+" from:"+from);
buggered if i can see it, Notepadd++ is showing all the stuff aligned under the vertical lines. can anyone spot it?

ta!
I think it's that case statement.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2278
Joined: Tue Jan 02, 2007 12:38 pm

Re: Split: viewDirectionChanged woes

Post by Killer Wolf »

why?
it has equal numbers of braces
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Split: viewDirectionChanged woes

Post by Eric Walch »

What will not work is placing the eventhandlers like:

Code: Select all

this.viewDirectionChanged = function(to,from)
within a case statement. There they never will be called. They must be at the main level of the code.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Split: viewDirectionChanged woes

Post by Thargoid »

this.alertConditionChanged, this.viewDirectionChanged and this.guiScreenChanged are all top-level functions (as are this.shipLaunchedFromStation and this.shipDied lower down, but those are used correctly), which are triggered by game events. You can't nest them, like you have in the scripting above (you've got this.alertConditionChanged as a correct top-level event function, but the other two are within that one at a lower level).

Basically in your scripting you are trying to define new functions (viewDirectionChanged and guiScreenChanged) within alertConditionChanged, which isn't correct. Basically all three should be top-level functions, with either nested if-else commands or switch commands underneath them at the next level down. They should be entirely seperate functions, not mixed up as they are above.
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2453
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia
Contact:

Re: Split: viewDirectionChanged woes

Post by Wildeblood »

That is never going to work, because you have the this.viewDirectionChanged function inside the this.alertConditionChanged one.

I think you might have misconstrued the comments Thargoid left in the script when he first wrote it: "we're at green alert" etc. Things like alertConditionChanged are instantaneous events, not ongoing states. Change those comments to things like, "Switching to green alert right now", "Changing the view direction right now" and it might make it clearer.

You can only deal with one event at a time: either right now the alert condition is changing or the view direction is changing. If you want to consider the other state, use a switch or if statement. But you can't use one event inside the other.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2278
Joined: Tue Jan 02, 2007 12:38 pm

Re: Split: viewDirectionChanged woes

Post by Killer Wolf »

oh.
i nested them cos i had them separate at first, but it was calling the screens incorrectly - when docked, i got the gui screen up instead of the trading HUD. i thought nesting them would get around that, but obviously not.
back to the drawing board. again.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Split: viewDirectionChanged woes

Post by Thargoid »

This is where it's going to get complicated, as you could end up with multiple events firing for some conditions, and so they'll all change the HUD (but only the one that happens last will be the one displayed).

Basically alertConditionChanged fires every time the alert condition changes (including docking - that's the 4th alert condition after green/yellow/red), viewDirectionChanged will trigger whenever you go into a new view direction and guiScreenChanged will trigger whenever you go into a new screen.

For example when you dock, both alertConditionChanged and guiScreenChanged will fire. So I guess you've got actions associated with both, and one is overwriting another.

What you need to do is define exactly what conditional changes you want to trigger which HUD displays and then use only those to keep things simpler. It is possible to merge things (e.g. you can use the player.ship.alertCondition variable to see what alert condition you are at, and then use that as a switch inside viewDirectionChanged to go between (for example a red alert forward view HUD and a yellow alert one) but that's getting more complex again.
JD
Deadly
Deadly
Posts: 182
Joined: Thu Nov 25, 2010 10:42 pm
Location: London, UK

Re: Split: viewDirectionChanged woes

Post by JD »

Killer Wolf wrote:
why?
it has equal numbers of braces
Yes, sorry KW, you're right. But I think that's where the confusion comes from - one of those braces isn't indented, so when you get to the end of the function, the brace that appears to close it actually doesn't. If you put the cursor in front of that final brace...

Code: Select all

						player.ship.hud = "phantomGUI.plist";
					}
			}
		}
		
	}
...then Notepad++ will highlight the corresponding opening brace, which is this one...

Code: Select all

   switch(newCondition)
      {
...and not the one the starts the function.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2278
Joined: Tue Jan 02, 2007 12:38 pm

Re: Split: viewDirectionChanged woes

Post by Killer Wolf »

Thargoid wrote:
This is where it's going to get complicated, as you could end up with multiple events firing for some conditions, and so they'll all change the HUD (but only the one that happens last will be the one displayed).

Basically alertConditionChanged fires every time the alert condition changes (including docking - that's the 4th alert condition after green/yellow/red), viewDirectionChanged will trigger whenever you go into a new view direction and guiScreenChanged will trigger whenever you go into a new screen.

For example when you dock, both alertConditionChanged and guiScreenChanged will fire. So I guess you've got actions associated with both, and one is overwriting another.

What you need to do is define exactly what conditional changes you want to trigger which HUD displays and then use only those to keep things simpler. It is possible to merge things (e.g. you can use the player.ship.alertCondition variable to see what alert condition you are at, and then use that as a switch inside viewDirectionChanged to go between (for example a red alert forward view HUD and a yellow alert one) but that's getting more complex again.
i was just thinking that. i was going to set up some variables, and keep the original script as much as possible, but, eg, in each Case set a particular variable and zeroise the others, then - after that, where i originally had the gui check stuff, set the HUD dependant on a variable value rather than a status check etc.
does that seem feasible?
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Split: viewDirectionChanged woes

Post by Thargoid »

You just need to be careful that two events aren't both trying to set the HUD to something at the same time (e.g. when docked). Svengali's code a page or so back could be useful to test that out, as it will log which events are firing, when and with what parameters being passed.

If you use that to check what's firing when, then optimise your code accordingly to ensure that all the events play nicely together then you should be good to go. I suspect at the moment you've got clashes going on and it's tying your scripting up in knots.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2278
Joined: Tue Jan 02, 2007 12:38 pm

Re: Split: viewDirectionChanged woes

Post by Killer Wolf »

reet : getting somewhere. ish

Code: Select all

var tradhud=0;
var normhud=0;
var comhud=0;
var snafhud=0;


this.alertConditionChanged = function(newCondition, oldCondition)
   {
   switch(newCondition)
      {
      case 0: // we're docked
         {
 			tradhud = 1;
			normhud = 0;
			comhud = 0;
			snafhud = 0;
         player.ship.hud = "phantomTradingHUD.plist"; // set the docked HUD
         break;
         }
      case 1: // we're at green alert
      case 2: // or we're at yellow alert
         {
         if(player.ship.energy > 108) // normal HUD
            {
			tradhud = 0;
			normhud = 1;
			comhud = 0;
			snafhud = 0;
            }
         break;
         }
      case 3: // we're at red alert
         {
         if(player.alertHostiles && player.ship.energy > 108) // and under attack and not using the damaged HUD
            {
			tradhud = 0;
			normhud = 0;
			comhud = 1;
			snafhud = 0;
            }
         break;
         }
      }
   }

   // ************************************************************************
   
	this.viewDirectionChanged = function(to,from)
        {
            log(this.name,"viewDirectionChanged: to:"+to+" from:"+from);
			log(this.name,"tradhud ="+tradhud);
            if(from === "VIEW_GUI_DISPLAY" && to !== "VIEW_GUI_DISPLAY")
               {
				if (normhud === 1)
					{
					player.ship.hud = "phantomNormalHUD.plist"; // set the standard HUD
					}
				else if (comhud === 1)
					{
					player.ship.hud = "phantomCombatHUD.plist"; // set the combat HUD
					}
				else if (snafhud === 1)
					{
		            player.ship.hud = "phantomSnafuHUD.plist"; 
					}
				}
		}
		
    this.guiScreenChanged = function(to,from)
        {
		log(this.name,"guiScreenChanged: to:"+to+" from:"+from);
		if(to === "GUI_SCREEN_STATUS")
        {
	   		if (tradhud == 0)     
			{
   			log(this.name,"status : tradhud ="+tradhud);
            player.ship.hud = "phantomGUI.plist";
            }
			else
			{
			player.ship.hud = "phantomTradingHUD.plist";
			}
		}
        else if(to === "GUI_SCREEN_SHORT_RANGE_CHART" || "GUI_SCREEN_LONG_RANGE_CHART")
        {
	   		if (tradhud == 0)     
			{
   			log(this.name,"charts : tradhud ="+tradhud);
            player.ship.hud = "phantomGUI.plist";
            }      
			else
			{
			player.ship.hud = "phantomTradingHUD.plist";
			}
		}
        else if(to === "GUI_SCREEN_MANIFEST")
        {
   	   		if (tradhud == 0)     
			{
  			log(this.name,"manifest :tradhud ="+tradhud);
            player.ship.hud = "phantomGUI.plist";
            }
			else
			{
			player.ship.hud = "phantomTradingHUD.plist";
			}
		}
        else if(to === "GUI_SCREEN_SYSTEM_DATA")
        {
   	   		if (tradhud == 0)     
			{
            player.ship.hud = "phantomGUI.plist";
            }
			else
			{
			player.ship.hud = "phantomTradingHUD.plist";
			}
		}
        else if(to === "GUI_SCREEN_MARKET")
        {
   	   		if (tradhud == 0)     
			{
            player.ship.hud = "phantomGUI.plist";
			}
			else
			{
		log("bombing here");		
			player.ship.hud = "phantomTradingHUD.plist";
			}
         }

		}

   
this.shipLaunchedFromStation = function()
   {
   tradhud = 0;
   player.ship.hud = "phantomNormalHUD.plist"; 
   if(this.energyCheckTimer)
      {
      this.energyCheckTimer.start()
      }
   else
      {
      this.energyCheckTimer = new Timer(this, this.energyCheck,0,2)      // use a timer to keep an eye on the HUD state
      }
   }


this.energyCheck = function()
   {
   if(player.ship.docked)
      {
      this.energyCheckTimer.stop();
      }
   else
      {
      if(player.ship.energy < 109 )  // SNAFU situation
         {
  			tradhud = 0;
			normhud = 0;
			comhud = 0;
			snafhud = 1;
         return;
         }
      if(player.ship.energy > 108 && player.ship.hud == "phantomSnafuHUD.plist")
         {
         this.alertConditionChanged(player.alertCondition,0); // if energy is >49 and we're still displaying the damaged HUD, use other code to repair
         }
      }             
   }

this.shipDied = function()
   {
   if(this.energyCheckTimer)
      {
      this.energyCheckTimer.stop()
      }
   }
this allows me to toggle between views and GUIs inflight. issues :-
- when i load the Savegame, i get the GUI HUD, no matter what F i press, despite the Shipdata specifying the Trading HUD, and the code above setting it if the condition is "docked". if i launch and dock, i get the Trading HUD correctly.
- in flight, the toggling seems fine, but i've lost the switching ability to combat mode and damage mode (i took my power to zero using the ECM).
had enough of this tonight, so any more thought from fresh eyes again would be welcomed....
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Split: viewDirectionChanged woes

Post by Thargoid »

this.startUp is the function that runs when the script is first loaded (and when you load a save game and when you restart after dying). You should be able to use that to solve the start-up problem.
User avatar
Killer Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 2278
Joined: Tue Jan 02, 2007 12:38 pm

Re: Split: viewDirectionChanged woes

Post by Killer Wolf »

i'll try that. i just had a brain (cough!) wave and set the tradhud variable to 1 at the top of the script, that worked on loading etc. the other issues remain.
Post Reply