Just a comment on a line of code
Moderators: winston, another_commander
- Massively Locked
- Dangerous
- Posts: 84
- Joined: Tue Nov 20, 2012 12:20 pm
Just a comment on a line of code
The line I wrote is:
if (player.ship.dockedStation.isMainStation)
If you're wondering what's wrong with that line, the answer is: nothing. It works fine... now. But it didn't look that way when I first started. In fact, it looked like a horse of a whole different color. I spent the better part of the afternoon hammering away until Oolite finally stopped saying, "and just what the hell is this??" Sheesh
My hat's off to all you guys skilled in the ancient, mystical art of JavaScripting.
if (player.ship.dockedStation.isMainStation)
If you're wondering what's wrong with that line, the answer is: nothing. It works fine... now. But it didn't look that way when I first started. In fact, it looked like a horse of a whole different color. I spent the better part of the afternoon hammering away until Oolite finally stopped saying, "and just what the hell is this??" Sheesh
My hat's off to all you guys skilled in the ancient, mystical art of JavaScripting.
Re: Just a comment on a line of code
And that line would be improved by actually checking you were docked first, or else it'll throw an error
I wouldn't worry about it, after the first couple of thousand of them you start to get the hang of things...
I wouldn't worry about it, after the first couple of thousand of them you start to get the hang of things...
My OXPs via Boxspace or from my Wiki pages .
Thargoid TV
Dropbox Referral Link
Thargoid TV
Dropbox Referral Link
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- Joined: Mon Apr 06, 2009 12:20 pm
- Location: Aboard the Pitviper S.E. "Blackwidow"
Re: Just a comment on a line of code
And then comes the testing..
Getting Oolite to stop complaining about your code is one thing. Getting the code to actually do what you wanted is quite another.
Unfortunately, despite millions of coders wishing for it, and decades of waiting, and dozens of programming languages, nobody has yet managed to create a
Getting Oolite to stop complaining about your code is one thing. Getting the code to actually do what you wanted is quite another.
Unfortunately, despite millions of coders wishing for it, and decades of waiting, and dozens of programming languages, nobody has yet managed to create a
do (what.I.mean)
function... Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
- Massively Locked
- Dangerous
- Posts: 84
- Joined: Tue Nov 20, 2012 12:20 pm
Re: Just a comment on a line of code
Thargoid-
Yes, actually I had included a check-for-docked. Here's my script in its full glory:
While it may seem ridiculously basic to a lot of you, it took me HOURS (no joke) to put that together.
About what you wrote regarding the learning curve- truer words never spoken.
Diziet-
I totally hear you on that, but I'm nowhere near that stage yet. I just want to write a few lines of code without it blowing up like, well, like my ship just did a little while ago (God damned Fer-de-Lance assassin- he's off my Christmas list next year).
Yes, actually I had included a check-for-docked. Here's my script in its full glory:
Code: Select all
this.shipDockedWithStation = function(station)
{if (player.ship.dockedStation.isMainStation)
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
About what you wrote regarding the learning curve- truer words never spoken.
Diziet-
I totally hear you on that, but I'm nowhere near that stage yet. I just want to write a few lines of code without it blowing up like, well, like my ship just did a little while ago (God damned Fer-de-Lance assassin- he's off my Christmas list next year).
- Diziet Sma
- ---- E L I T E ----
- Posts: 6312
- Joined: Mon Apr 06, 2009 12:20 pm
- Location: Aboard the Pitviper S.E. "Blackwidow"
Re: Just a comment on a line of code
Every step along the way helps.. "ah-hah!" moments come along and the pennies start to drop.. and like everything else in life, we learn more from our mistakes than the things we get right. Before long, you'll be surprising yourself. Even the Devs (Praise Be Unto Them) once stood where you are now..
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: Just a comment on a line of code
You overlooked something:-Massively Locked wrote:Here's my script in its full glory:
Code: Select all
this.shipDockedWithStation = function(station) {if (player.ship.dockedStation.isMainStation) mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"}); }
Code: Select all
this.shipDockedWithStation = function(station) // <----------
{if (station.isMainStation) // <-----------
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
- submersible
- Commodore
- Posts: 264
- Joined: Thu Nov 10, 2011 7:49 am
Re: Just a comment on a line of code
The first one is cuddled on the next line , the inner if is implicit single statement. Reformatted...Wildeblood wrote:You overlooked something:-
Code: Select all
this.shipDockedWithStation = function(station) // <---------- {if (station.isMainStation) // <----------- mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"}); }
Code: Select all
this.ShipDockedWithStation = function(station)
{
if (station.isMainStation)
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
Code: Select all
this.ShipDockedWithStation = function(station)
{
if (station.isMainStation)
{
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
}
Povray Planets - Planet textures for your galaxy
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: Just a comment on a line of code
Huh? I was pointing out the station reference going to waste, not commenting on the formatting.submersible wrote:The first one is cuddled on the next line , the inner if is implicit single statement.Wildeblood wrote:You overlooked something:-
Code: Select all
this.shipDockedWithStation = function(station) // <---------- {if (station.isMainStation) // <----------- mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"}); }
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Just a comment on a line of code
There is just one flaw left in the code:
It does not check if another script already presented a mission screen. In that case this oxp would overwrite that screen. So, it needs an additional check:
To avoid this type of problems, it is strongly advised to use the
Code: Select all
this.ShipDockedWithStation = function(station)
{
if (station.isMainStation)
{
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
}
Code: Select all
this.ShipDockedWithStation = function(station)
{
if (station.isMainStation && guiScreen != "GUI_SCREEN_MISSION")
{
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
}
missionScreenOpportunity
to display missionscreens. When your script gets a missionScreenOpportunity
you can be sure no other script is using the missionscreen. UPS-Courier & DeepSpacePirates & others at the box and some older versions
- submersible
- Commodore
- Posts: 264
- Joined: Thu Nov 10, 2011 7:49 am
Re: Just a comment on a line of code
Ah , totally mis-interpreted your added comments. But why is 'station' going to waste?Wildeblood wrote:Huh? I was pointing out the station reference going to waste, not commenting on the formatting.
Povray Planets - Planet textures for your galaxy
- Massively Locked
- Dangerous
- Posts: 84
- Joined: Tue Nov 20, 2012 12:20 pm
Re: Just a comment on a line of code
Wildeblood- that's great! I did wonder what 'function(station)' was all about since I didn't use it in my subsequent lines. I'm also pretty sure that I tried 'station.isMainStation', but it didn't work probably because my first line originally ended with "function()". It's all so clear now.
submersible- good advice on braces & formatting. The one thing I know about parens, braces and brackets is always close what you open. Beyond that, I'm hazy on when and where they're best used. As for why the station ref was going to waste, I just found another way to get the 'If' condition working and went with that.
Eric- I'm definitely gonna add and study your addition. After I finally got my script working, I did come across 'missionScreenOpportunity', and it seemed to be what I need to use. That will take me some time figure out.
It's funny that after I thought I was finished with my little script, I wasn't actually finished with it. I wasn't expecting the tips and advice, and it's much appreciated. As Diziet said above, you really do learn more when things go wrong than when they go right.
submersible- good advice on braces & formatting. The one thing I know about parens, braces and brackets is always close what you open. Beyond that, I'm hazy on when and where they're best used. As for why the station ref was going to waste, I just found another way to get the 'If' condition working and went with that.
Eric- I'm definitely gonna add and study your addition. After I finally got my script working, I did come across 'missionScreenOpportunity', and it seemed to be what I need to use. That will take me some time figure out.
It's funny that after I thought I was finished with my little script, I wasn't actually finished with it. I wasn't expecting the tips and advice, and it's much appreciated. As Diziet said above, you really do learn more when things go wrong than when they go right.
Last edited by Massively Locked on Mon Dec 31, 2012 10:29 am, edited 1 time in total.
- Smivs
- Retired Assassin
- Posts: 8408
- Joined: Tue Feb 09, 2010 11:31 am
- Location: Lost in space
- Contact:
Re: Just a comment on a line of code
You also learn a lot from the good folks around this board The wealth of knowledge here is impressive, and always so freely given.
Yes, it is a steep curve, and learning js can be frustrating as much as rewarding, but it's definitely worth the effort, and the possibilities js gives us seem endless. The biggest limit is in the imagination of the coder - if you can imagine something, one way or another you can probably make it work.
Yes, it is a steep curve, and learning js can be frustrating as much as rewarding, but it's definitely worth the effort, and the possibilities js gives us seem endless. The biggest limit is in the imagination of the coder - if you can imagine something, one way or another you can probably make it work.
Commander Smivs, the friendliest Gourd this side of Riedquat.
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: Just a comment on a line of code
Most of the writing time is not going into what you want to do with the script, but in handling all the exceptions were your script should do nothing.....Massively Locked wrote:It's funny that after I thought I was finished with my little script, I wasn't actually finished with it.
UPS-Courier & DeepSpacePirates & others at the box and some older versions
- Tricky
- ---- E L I T E ----
- Posts: 821
- Joined: Sun May 13, 2012 11:12 pm
- Location: Bradford, UK. (Anarchic)
Re: Just a comment on a line of code
Also read other OXP scripts for examples on how to handle things.
@Eric: Reminds me of some code I wrote, here's the comment...
In theory it could be executed so I coded it to handle it.
Edit: Comment your code so that when you come back to it after a few months you know what you were trying to do.
@Eric: Reminds me of some code I wrote, here's the comment...
Code: Select all
/* START OF CODE THAT SHOULD NEVER BE REACHED.
* This is here purely for error checking sake.
* If the base is destroyed it will set the patrol ships fully launched variable,
* therefore this code block shouldn't be reached.
*/
Edit: Comment your code so that when you come back to it after a few months you know what you were trying to do.
- Commander McLane
- ---- E L I T E ----
- Posts: 9520
- Joined: Thu Dec 14, 2006 9:08 am
- Location: a Hacker Outpost in a moderately remote area
- Contact:
Re: Just a comment on a line of code
Which means that the better way of doing it would be:Eric Walch wrote:There is just one flaw left in the code:It does not check if another script already presented a mission screen. In that case this oxp would overwrite that screen. So, it needs an additional check:Code: Select all
this.ShipDockedWithStation = function(station) { if (station.isMainStation) { mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"}); } }
To avoid this type of problems, it is strongly advised to use theCode: Select all
this.ShipDockedWithStation = function(station) { if (station.isMainStation && guiScreen != "GUI_SCREEN_MISSION") { mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"}); } }
missionScreenOpportunity
to display missionscreens. When your script gets amissionScreenOpportunity
you can be sure no other script is using the missionscreen.
Code: Select all
this.missionScreenOpportunity = function()
{
if (player.ship.isDocked && player.ship.dockedStation.isMainStation)
{
mission.runScreen({title: "Automated GalCop Station Greeting", message: "Welcome, Commander"});
}
}
1) You notice that
player.ship.dockedStation
is back again. That's because the missionScreenOpportunity
-handler does not have the handy station
-parameter that can be used inside the function.And 2) Thargoid's first comment is accommodated by adding a first check for whether the player is actually still docked at this point. This may seem superfluous at first glance, but experience has shown that it isn't, because there could be another script active that could have force-launched the player in-between calling the
missionScreenOpportunity
-handler and checking your condition.(And by the way: Eric in his code transports an error introduced by submersible, so there were actually two flaws. Can you spot it? Answer in the fine print.)
All reserved names in JS begin with a lowercase. Thus
ShipDockedWithStation
must be [color=#FF0000]s[/color]hipDockedWithStation