Unable to find error in my script?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Unable to find error in my script?

Post by calmrr3 » Thu Feb 05, 2015 12:23 pm

Hello,

I've spent the last couple of days trying to debug my script but I have had no luck and can see no reason why it doesn't work. So I thought a fresh set of eyes might be able to spot something...

So the first card script is:

Code: Select all

global tLatitudeListA
global tLatitudeList
global tLatitudeMax
global tLatitudeMin

global tLongitudeListA
global tLongitudeList
global tLongitudeMax
global tLongitudeMin

global tAltitudeListA
global tAltitudeList
global tAltitudeMax
global tAltitudeMin

global counterVar
global itemCount

on openCard
   put EMPTY into fld "Latitude" of this card
   put EMPTY into fld "Longitude" of this card
   put EMPTY into fld "Altitude" of this card
   put EMPTY into fld "countField" of this card
end openCard

on locationChanged pLatitude, pLongitude, pAltitude
   put pAltitude & cr after fld "Altitude" of this card
   put pLatitude & cr after fld "Latitude" of this card
   put pLongitude & cr after fld "Longitude" of this card
   add 1 to counterVar
   put counterVar into fld "countField" of this card
end locationChanged

on findMinMaxLat
   put fld "Latitude" of this card into tLatitudeList
   put tLatitudeList into tLatitudeListA
   replace cr with comma in tLatitudeList
   put max(tLatitudeList) into tLatitudeMax
   put min(tLatitudeList) into tLatitudeMin
end findMinMaxLat
   
on findMinMaxLon
   put fld "Longitude" of this card into tLongitudeList
   put tLongitudeList into tLongitudeListA
   replace cr with comma in tLongitudeList
   put max(tLongitudeList) into tLongitudeMax
   put min(tLongitudeList) into tLongitudeMin
end findMinMaxLon

on findMinMaxAlt
   put fld "Altitude" of this card into tAltitudeList
   put tAltitudeList into tAltitudeListA
   replace cr with comma in tAltitudeList
   put max(tAltitudeList) into tAltitudeMax
   put min(tAltitudeList) into tAltitudeMin
end findMinMaxAlt

on errorDialog pExecutionError, pParseError
   answer "An error occurred on line: " & item 2 of line 1 of pExecutionError & cr & pExecutionError
end errorDialog
On this card there are 3 fields which record the latitude, longitude and altitude at each location change.

There is a start button with this script :

Code: Select all

global counterVar

on mouseUp
   put 0 into counterVar 
 if the environment is "mobile" then
      if mobileCanTrackLocation() is true then
         mobileStartTrackingSensor "location"
      else
         answer "Can't find your location"
      end if
   end if
end mouseUp
A stop button with this script:

Code: Select all

on mouseUp
   mobileStopTrackingSensor "location"
   
   findMinMaxLat
   findMinMaxLon
   findMinMaxAlt
end mouseUp
And a button to go to the next card. THIS IS WHERE THE ERROR IS: When the next card is opened an error message pops up. I have tested all the variables by putting them into fields and it seems like they can all be accessed and I cant see a error in the openCard script.. Here it is:

Code: Select all

global tLatitudeListA
global tLatitudeList
global tLatitudeMax
global tLatitudeMin

global tLongitudeListA
global tLongitudeList
global tLongitudeMax
global tLongitudeMin

global tAltitudeListA
global tAltitudeList
global tAltitudeMax
global tAltitudeMin
global itemCountA

local xLocA
local zLocA
local yLocA

local yLocNewA
local xLocNewA
local zLocNewA
local G

on openCard
   lock screen
   
   repeat with G = 1 to the number of graphics of this card
      if the short name of graphic G begins with "grcLoc" then
         delete graphic G
      end if
   end repeat
   
   set the opaque of the templateGraphic to true
   
   repeat with itemCountA = 1 to the number of lines of tLatitudeListA
      put line itemCountA of tLatitudeListA into xLocA
      put line itemCountA of tLongitudeListA into yLocA
      put line itemCountA of tAltitudeListA into zLocA 
      put (((xLocA - tLatitudeMin) * (60 - 320)) / (tLatitudeMax - tLatitudeMin)) + 320 into yLocNewA
      put (((yLocA - tLongitudeMin) * (60 - 320)) / (tLongitudeMax - tLongitudeMin)) + 320 into xLocNewA
      put (((zLocA - tAltitudeMin) * (3 - 1)) / (tAltitudeMax - tAltitudeMin)) + 3 into  zLocNewA
      put fnSnapNumbers(yLocNewA,20,10) into yLocNewA
      put fnSnapNumbers(xLocNewA,20,10) into xLocNewA
      put xLocNewA & comma & yLocNewA into tSquareLoc   
      add 1 to tLocsArr[(tSquareLoc)]
   end repeat
      
   put the keys of tLocsArr into tKeys
   
   repeat for each line tKey in tKeys
      create graphic
      put it into tNewGrcIDa
      set the name of the last graphic to ("grcLoc" & tNewGrcIDa)
      set the loc of graphic ("grcLoc" & tNewGrcIDa) to (tKey)
      set the style of the last grc to oval
      set the height of the last grc to 10
      set the width of the last grc to 10
      set the backgroundColor of the last grc to black
      set the blendlevel of the last grc to 0
      
      if tLocsArr[(tKey)] < 20 then
         set the blendLevel of graphic ("grcLoc" & tNewGrcIDa) to 70-tLocsArr[(tKey)]
         set the textColor of the last grc to 36,203,185
      else if tLocsArr[(tKey)] < 40 then
         set the blendLevel of graphic ("grcLoc" & tNewGrcIDa) to 70-tLocsArr[(tKey)]
         set the textColor of the last grc to 245,54,16
      else if tLocsArr[(tKey)] < 60 then
         set the blendLevel of graphic ("grcLoc" & tNewGrcIDa) to 70-tLocsArr[(tKey)]
         set the textColor of the last grc to 253,50,81
      else if tLocsArr[(tKey)] < 80 then
         set the blendLevel of graphic ("grcLoc" & tNewGrcIDa) to 70-tLocsArr[(tKey)]
         set the textColor of the last grc to 147,16,123
      else if tLocsArr[(tKey)] < 100 then
         set the blendLevel of graphic ("grcLoc" & tNewGrcIDa) to 70-tLocsArr[(tKey)]
         set the textColor of the last grc to 18,67,97
      else 
         set the blendLevel of graphic ("grcLoc" & tNewGrcIDa) to 70-tLocsArr[(tKey)]
         set the textColor of the last grc to 49,138,225
      end if
      
      set the width of graphic ("grcLoc" & tNewGrcIDa) to 8+ (tLocsArr[(tKey)] / 30)
      set the height of graphic ("grcLoc" & tNewGrcIDa) to 8 + (tLocsArr[(tKey)]/30)
      set the lineSize of graphic ("grcLoc" & tNewGrcIDa) to 2+ (tLocsArr[(tKey)] / 40)
   end repeat
   unlock screen
   reset the templateGraphic
end openCard


on closeCard
   lock screen
   put the number of graphics into theGraphsCount
   repeat with currentGraphic = theGraphsCount down to 1
      if there is a graphic currentGraphic then
         delete graphic currentGraphic   
      end if
   end repeat
end closeCard


function fnSnapNumbers pNum,pSnapGap,pSnapOffset
   put pNum mod pSnapGap into tM
   put max(0,round(tM-pSnapOffset - 9,-1)) into tM
   put trunc((pNum-1) / pSnapGap) * pSnapGap into tN
   return tN + tM + pSnapOffset
end fnSnapNumbers
The objective of this is to plot the GPS points so that they look like this the image below - I made this using manual entered coordinates and basically the same script as above so I can't figure out why it won't work now.

Thanks!
Last edited by calmrr3 on Thu Feb 05, 2015 4:04 pm, edited 1 time in total.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Unable to find error in my script?

Post by Mark » Thu Feb 05, 2015 3:29 pm

What is the exact text of the error message?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Unable to find error in my script?

Post by calmrr3 » Thu Feb 05, 2015 4:06 pm

Mark wrote:What is the exact text of the error message?

Mark

Hi, When run on the xcode simulator it says :

Image

Thanks

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Unable to find error in my script?

Post by Mark » Thu Feb 05, 2015 4:22 pm

Hi,

It looks like the error is caused by

Code: Select all

mobileStopTrackingSensor "location"
I'm not sure you can use location services in the emulator. Please, try a real device. Additionally, check that Location Services are set to Required in the application settings.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Unable to find error in my script?

Post by calmrr3 » Thu Feb 05, 2015 4:31 pm

Hi Mark,

Thanks for your reply,

I have tested this on my device and it is giving the same message. Location services and GPS are enabled as well.

Thanks

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Unable to find error in my script?

Post by Mark » Thu Feb 05, 2015 4:38 pm

Hi,

That is strange. To make sure that the error is caused by this statement, you could put a try-catch control structure around it and see if it catches an error. Are you sure that mobileStartTrackingSensor was executed successfully first?

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Unable to find error in my script?

Post by calmrr3 » Thu Feb 05, 2015 5:33 pm

Mark wrote:Hi,

That is strange. To make sure that the error is caused by this statement, you could put a try-catch control structure around it and see if it catches an error. Are you sure that mobileStartTrackingSensor was executed successfully first?

Kind regards,

Mark
Hi Mark, So the error is something to do with "yLocA"

Code: Select all

answer yLocA
returns 'tLongitudeListA' rather than the expected numeric value.

xLocA and zLocA both work fine - strange because when I follow them back, they all seem to be set up in the exact same way.

Thanks

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Unable to find error in my script?

Post by calmrr3 » Thu Feb 05, 2015 6:14 pm

SOLVED, rewrote the line and it now seems to work.. strange.

Thanks anyway

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Unable to find error in my script?

Post by jacque » Thu Feb 05, 2015 10:16 pm

You may eventually have trouble with this too:

Code: Select all

   repeat with G = 1 to the number of graphics of this card
      if the short name of graphic G begins with "grcLoc" then
         delete graphic G
      end if
   end repeat
When deleting objects, always delete them in reverse order:

Code: Select all

   repeat with G = the number of graphics of this card down to 1 -- reverse order
      if the short name of graphic G begins with "grcLoc" then
         delete graphic G
      end if
   end repeat
If you delete objects starting with 1, there will be fewer and fewer objects as the loop progresses. By the time it gets to the number of objects that were on the card when the loop started, there will not be that many objects any more and the script will error.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply