wkitty42 wrote in Tue Mar 03, 2020 6:04 am:make sure you are counting properly... remember programmers start counting at zero so your "10" would actually be 9 in the index... if that's the problem you're having, that is...
Right, that's very important - I also believe this to be the issue here. However, I haven't really looked or execute said code.
That being said, I noticed that you are using multi-dimensional arrays - personally, I believe it's a bad idea to use them until you fully understand stand-alone (single-dimensional) arrays/vectors.
The error you posted, implies an "invalid access to an array", specifically saying that the index you passed into the array access is not valid.
Therefore, you need to look at all statements that access an array using an index, i.e. array[index].
The next step is to assure that all indexes are indeed correct - to do that, put a print() statement before the statement to log all index numbers to the console.
Almost certainly, you are going to see where the problem lies - and I suppose, it will also almost certainly point to your multi-dimensional vector.
Besides, when you do post code here, it is a good idea to always post a self-contained piece of code, i.e. something that people can run via the Nasal console - I am saying that, because I am fairly sure the problem is due to the way you set up your data structure (the multi-dimensional vector)
Finally, the way you are using Nasal vectors here is a bit unusual - most people would use a Nasal hash instead, so that they don't have to deal with indexes, but can use identifiers to look up elements from their hash. I am saying that because I do remember you previously mixing up Nasal and Lua related concepts in another topic, right ?
Again, regardless of what the issue is here, it is more important to develop the mindset to properly troubleshoot this - e.g. by removing index lookups from the loop body, replacing those with constant strings - and if that solves the problem, you know that it's related to your array accesses inside the loop body, so that the next step would be logging your index numbers to the console.
At that point, you will probably also want to review the setup of your data structure.
By using a vector of hashes, you can have something like this:
- Code: Select all
button[0].label = "Hello World";
button[1].label = "Hello Forum";
button[2].label = "Hello FlightGear";
i.e. much better readability, compared to dealing with index numbers.
If you do want to use an index, simply introduce an alias:
- Code: Select all
var label = 0;
button[0][label] = "Hello World";
example taken from the wiki:
http://wiki.flightgear.org/Howto:Start_ ... ed_version- Code: Select all
# these are used self-explanatory indices - so that you don't need to remember each field's position:
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[0][DISTANCE]) # prints 1000
print(waypoints[1][ALTITUDE]) # prints 2
print(waypoints[1][DISTANCE]) # prints 1500