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.Dragonfire wrote:The only thing I miss from Visual Basic is the Select Case construct...Mauiby de Fug wrote:One, but by coupling that else with an if, as many as you like!CommonSenseOTB wrote:I have a question: How many "elses" can follow an "if"?
Split: viewDirectionChanged woes
Moderators: winston, another_commander
Re: Split: viewDirectionChanged woes
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
Re: Split: viewDirectionChanged woes
right, i've tried to integrate Svengali's stuff, but i'm getting an error about a missing "}" at teh end of the code
buggered if i can see it, Notepadd++ is showing all the stuff aligned under the vertical lines. can anyone spot it?
ta!
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()
}
}
ta!
Re: Split: viewDirectionChanged woes
I think it's that case statement.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
buggered if i can see it, Notepadd++ is showing all the stuff aligned under the vertical lines. can anyone spot it?Code: Select all
case 2: // or we're at yellow alert { this.viewDirectionChanged = function(to,from) { log(this.name,"viewDirectionChanged: to:"+to+" from:"+from);
ta!
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
Re: Split: viewDirectionChanged woes
why?
it has equal numbers of braces
it has equal numbers of braces
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Split: viewDirectionChanged woes
What will not work is placing the eventhandlers like: within a case statement. There they never will be called. They must be at the main level of the code.
Code: Select all
this.viewDirectionChanged = function(to,from)
UPS-Courier & DeepSpacePirates & others at the box and some older versions
Re: Split: viewDirectionChanged woes
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.
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.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: Split: viewDirectionChanged woes
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.
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.
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
Re: Split: viewDirectionChanged woes
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.
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.
Re: Split: viewDirectionChanged woes
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.
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.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
Re: Split: viewDirectionChanged woes
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...Killer Wolf wrote:why?
it has equal numbers of braces
Code: Select all
player.ship.hud = "phantomGUI.plist";
}
}
}
}
Code: Select all
switch(newCondition)
{
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
Re: Split: viewDirectionChanged woes
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.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.
does that seem feasible?
Re: Split: viewDirectionChanged woes
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.
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.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
Re: Split: viewDirectionChanged woes
reet : getting somewhere. ish
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....
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()
}
}
- 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....
Re: Split: viewDirectionChanged woes
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.
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Killer Wolf
- ---- E L I T E ----
- Posts: 2279
- Joined: Tue Jan 02, 2007 12:38 pm
Re: Split: viewDirectionChanged woes
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.