Array issues

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

Moderators: winston, another_commander

Post Reply
User avatar
byronarn
Dangerous
Dangerous
Posts: 85
Joined: Thu Nov 18, 2010 6:08 am
Location: United States

Array issues

Post 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?
User avatar
Cody
Sharp Shooter Spam Assassin
Sharp Shooter Spam Assassin
Posts: 16081
Joined: Sat Jul 04, 2009 9:31 pm
Location: The Lizard's Claw
Contact:

Re: Array issues

Post 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]
I would advise stilts for the quagmires, and camels for the snowy hills
And any survivors, their debts I will certainly pay. There's always a way!
User avatar
Norby
---- E L I T E ----
---- E L I T E ----
Posts: 2577
Joined: Mon May 20, 2013 9:53 pm
Location: Budapest, Hungary (Mainly Agricultural Democracy, TL10)
Contact:

Re: Array issues

Post by Norby »

Code: Select all

messageText[floor(

Code: Select all

messageText[Math.floor(
?
User avatar
byronarn
Dangerous
Dangerous
Posts: 85
Joined: Thu Nov 18, 2010 6:08 am
Location: United States

Re: Array issues

Post 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! :)
User avatar
Norby
---- E L I T E ----
---- E L I T E ----
Posts: 2577
Joined: Mon May 20, 2013 9:53 pm
Location: Budapest, Hungary (Mainly Agricultural Democracy, TL10)
Contact:

Re: Array issues

Post 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.
User avatar
Cody
Sharp Shooter Spam Assassin
Sharp Shooter Spam Assassin
Posts: 16081
Joined: Sat Jul 04, 2009 9:31 pm
Location: The Lizard's Claw
Contact:

Re: Array issues

Post by Cody »

Code: Select all

if(ship.isBaecon)
<chortles>
I would advise stilts for the quagmires, and camels for the snowy hills
And any survivors, their debts I will certainly pay. There's always a way!
User avatar
byronarn
Dangerous
Dangerous
Posts: 85
Joined: Thu Nov 18, 2010 6:08 am
Location: United States

Re: Array issues

Post 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.
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Array issues

Post 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!
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2407
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia

Re: Array issues

Post 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.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Array issues

Post by cim »

Basic errors like this can often be picked up with "use strict"; - I strongly recommend using it in all OXP scripts.
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Array issues

Post 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!");
}
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2407
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia

Re: Array issues

Post 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.
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Array issues

Post 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.
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Array issues

Post 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.
User avatar
Ranthe
---- E L I T E ----
---- E L I T E ----
Posts: 330
Joined: Sat Oct 13, 2012 7:35 pm
Location: Paraparaumu, New Zealand (TL 8, Rich Agricultural, Multi-Government)

Re: Array issues

Post 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:
Commander Ranthe: Flying the Anaconda-class transport Atomic Annie through Galaxy 2.
Combat Ranking: Dangerous
"Big ships take more booty on your interstellar flights..."
Post Reply