Page 1 of 1

Array issues

Posted: Sun Jul 27, 2014 11:54 pm
by byronarn
I'm working on an OXP that allows your passenger to send you messages. I am having issues with a segment of the code. I've isolated it to the following portion of code. Can anyone see why the message isn't displaying? (x is the randomly selected passenger.)

Code: Select all

(...)
else if (player.alertCondition === 1) {
		var messageText = ["I'm bored. Are we there yet?", "I can't wait to get there. I have important business to do, you know.",
					   "Just think, in your father's day, a trip like this would have taken twice as long!",
					   "I have heard many stories about this system. Let's hope half of them aren't true!",
					   "Does this ship have bathroom facilities?", "I like pie.", "You are fully licensed and bonded, right?",
					   "Sir, your in-flight magazines are wildly out of date!", "You call yourself a pilot? You fly like a pregnant yak!"];
		player.consoleMessage(player.ship.passengers[x].name + " (Going to " + player.ship.passengers[x].destinationName + "): " + messageText[floor(Math.random()*messageText.length)]);
	}
(...)
The code works in all the other conditions (in a fire fight, the temperature too hot, mass locked, etc.), but not in this one. What did I do wrong?

Re: Array issues

Posted: Mon Jul 28, 2014 12:12 am
by Cody
[dumb pilot mode] something about this line bothers me - an unwanted ] perhaps?

Code: Select all

player.ship.passengers[x].destinationName + "): " + messageText[floor(Math.random()*messageText.length)]);
Probably way off base, but hey... [/dumb pilot mode]

Re: Array issues

Posted: Mon Jul 28, 2014 12:13 am
by Norby

Code: Select all

messageText[floor(

Code: Select all

messageText[Math.floor(
?

Re: Array issues

Posted: Mon Jul 28, 2014 12:21 am
by byronarn
Norby wrote:

Code: Select all

messageText[floor(

Code: Select all

messageText[Math.floor(
?
Omg, I feel so stupid now. Of course that was it.

Thank you for pointing out my stupidity to me! :)

Re: Array issues

Posted: Mon Jul 28, 2014 12:48 am
by Norby
My pleasure. Some of these bugs can be very time consuming, here is one which I hunted once in a whole day:

Code: Select all

if(ship.isBaecon)

Code: Select all

if(ship.isBeacon)
A non-existent property is not generate any warning in js but considered silently to false, so the mistyped condition was never true. This was a special treat when I finally found it.

Re: Array issues

Posted: Mon Jul 28, 2014 12:53 am
by Cody

Code: Select all

if(ship.isBaecon)
<chortles>

Re: Array issues

Posted: Mon Jul 28, 2014 1:11 am
by byronarn
Norby wrote:
My pleasure. Some of these bugs can be very time consuming, here is one which I hunted once in a whole day:

Code: Select all

if(ship.isBaecon)

Code: Select all

if(ship.isBeacon)
A non-existent property is not generate any warning in js but considered silently to false, so the mistyped condition was never true. This was a special treat when I finally found it.
Shortly after I solved the issue of this thread, I came across another bug. It took me an hour to realize what was wrong with this:

Code: Select all

if (Math.random < 0.25)
I was mad at myself when I realized it. Lol.

Re: Array issues

Posted: Mon Jul 28, 2014 1:38 am
by Switeck
Norby wrote:
My pleasure. Some of these bugs can be very time consuming, here is one which I hunted once in a whole day:

Code: Select all

if(ship.isBaecon)

Code: Select all

if(ship.isBeacon)
A non-existent property is not generate any warning in js but considered silently to false, so the mistyped condition was never true. This was a special treat when I finally found it.
Sucks when you can't find the Bacon!

Re: Array issues

Posted: Mon Jul 28, 2014 3:20 am
by Wildeblood
Norby wrote:
My pleasure. Some of these bugs can be very time consuming, here is one which I hunted once in a whole day:

Code: Select all

if(ship.isBaecon)

Code: Select all

if(ship.isBeacon)
A non-existent property is not generate any warning in js but considered silently to false, so the mistyped condition was never true. This was a special treat when I finally found it.
Ship is bacon!? Smivs has admitted to ship lunching from station, in response to my ship lurching from station.

Re: Array issues

Posted: Mon Jul 28, 2014 6:03 am
by cim
Basic errors like this can often be picked up with "use strict"; - I strongly recommend using it in all OXP scripts.

Re: Array issues

Posted: Mon Jul 28, 2014 7:41 am
by Smivs
You could also simplify the scripting by removing all the messages from the script using 'expandDescription' and moving the text to a descriptions.plist.

Code: Select all

else if (player.alertCondition === 1) {
      var messageText = (expandDescription["passengerChatterDocked"]);
      player.consoleMessage(player.ship.passengers[x].name + " (Going to " + player.ship.passengers[x].destinationName + "): " + messageText[math.floor(Math.random()*messageText.length)]);
   }
then in descriptions.plist

Code: Select all

{
"passengerChatterDocked" = ("I'm bored. Are we there yet?",  "I can't wait to get there. I have important business to do, you know.",  "Just think, in your father's day, a trip like this would have taken twice as long!", "I have heard many stories about this system. Let's hope half of them aren't true!", "Does this ship have bathroom facilities?",  "I like pie.",  "You are fully licensed and bonded, right?",  "Sir, your in-flight magazines are wildly out of date!",  "You call yourself a pilot? You fly like a pregnant yak!");
}

Re: Array issues

Posted: Mon Jul 28, 2014 9:20 am
by Wildeblood
Smivs wrote:
You could also simplify the scripting by removing all the messages from the script using 'expandDescription' and moving the text to a descriptions.plist.
I think you meant expandMissionText() and missiontext.plist? And now that the subject has come up, is there any difference at all in the way descriptions.plist and missiontext.plist function?

I've never been sure whether the entries in missiontext were globally available or restricted to that OXP? When I re-wrote Explorers' Club, I moved all the strings to a missiontext file, but I gave all the entries an explorerClub_ prefix, on the assumption they are all merged into one pool.

Re: Array issues

Posted: Mon Jul 28, 2014 10:01 am
by Smivs
I tend to use missionText for mission screens etc, and Descriptions for console messages etc. I'm not sure if there is actually any convention or recommendations regarding this.

Re: Array issues

Posted: Mon Jul 28, 2014 3:46 pm
by cim
Wildeblood wrote:
And now that the subject has come up, is there any difference at all in the way descriptions.plist and missiontext.plist function?
Very little, nowadays. The expandDescription function takes a string which may contain some descriptions entries in [], giving a warning for unrecognised entries, while the expandMissionText function takes a string which is expected to be the name of a key in missiontext.plist, silently returning null if it doesn't exist.

The contents of missiontext.plist are easier to access directly from a mission screen if you don't need to modify them (or can do all the modification with simple expansions such as %H or mission variables)

The contents of descriptions.plist are more suited to randomisation using arrays.

Both files are merged, so you do need to prefix your entries.

Re: Array issues

Posted: Tue Jul 29, 2014 7:50 am
by Ranthe
Norby wrote:
My pleasure. Some of these bugs can be very time consuming, here is one which I hunted once in a whole day:

Code: Select all

if(ship.isBaecon)

Code: Select all

if(ship.isBeacon)
A non-existent property is not generate any warning in js but considered silently to false, so the mistyped condition was never true. This was a special treat when I finally found it.

Code: Select all

if((ship.isKevinBaecon) or (ship.isWithinSixDegreesOfKevinBaecon))
Fixed :mrgreen: